Advertisement
AyanUpadhaya

Add Multiple Numbers in Python

Jul 25th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. # Add multiple numbers
  2. # A small beginner python program
  3. # The program uses list as data structure and
  4. # sum() function to add multiple number from list
  5.  
  6.  
  7. """
  8. Example-
  9. python3 calc.py
  10. Enter number separated via comma:1,2,3
  11. output: 6
  12.  
  13. """
  14. user_input = input('Enter number separated via comma:')
  15.  
  16.  
  17. str_numbers= user_input.split(',') # splitting string numbers by comma and creating a list
  18.  
  19. int_numbers=[]
  20.  
  21. for items in str_numbers:
  22.     #converting all the string nums to int nums
  23.     int_numbers.append(int(items))
  24.  
  25. print(sum(int_numbers))
  26.  
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement