Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python adding digits

def sum_digits(num: int) -> int:
	
	#base case when your "positive" integer get to 0
    if num == 0: 
        return 0
    #base case when your "negative" integer get is between -10 and 0
    if num > -10 and num < 0:
        return num
	
	# recursion for positive integer
    elif num > 0:
        return (num % 10) + sum_digits(num//10)
	
	# recursion for negative integer
    elif num < 0:
        return -(abs(num) % 10) + sum_digits(-(abs(num)//10))
      
sum_digits(123) # returns 6
sum_digits(-123) # returns -6
Comment

adding numbers in python

number1 = 22
number2 = 16


total_number = number1 + number2
print(total_number)
#the result should be 38  :)

number3 = 12

the_total_nmbr = total_number + 12
print(the_total_nmbr) #result should be 50 now
Comment

PREVIOUS NEXT
Code Example
Python :: how to save a pickle file 
Python :: Python Removing Directory or File 
Python :: shutil move file 
Python :: how to add mouse button in pygame 
Python :: create and populate dictionary python 
Python :: convert numpy array to tensor 
Python :: Read text file line by line using the readline() function 
Python :: sort a series pandas 
Python :: fibonacci number in python 
Python :: python turtle commands 
Python :: python function as parameter 
Python :: python tkinter change color of main window 
Python :: find where df series is null and print 
Python :: python remove string from string 
Python :: pyhton mahalanobis distance 
Python :: pandas front fill 
Python :: # invert a dictionary 
Python :: how to delete a file in python 
Python :: count occurrences of a character in a string python 
Python :: sqlite query in python 
Python :: # time delay in python script 
Python :: list python virtual environments 
Python :: python check if character is letter 
Python :: keyboard press pyautogui 
Python :: python talib install windows 
Python :: how to add header in csv file in python 
Python :: how to install python 3.6.0 on debian 
Python :: append vs insert python 
Python :: Send GIF in Embed discord.py 
Python :: ping with python 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =