Addition of Two Numbers in Python
While performing addition in python then we can do two ways:The first way is just to assign values to the variable and then add that number and assign to the same variable or third variable.Example:
variable1 = 10 variable2 = 20 variable3 = variable1 + variable2 print('Total output is: ',variable3)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py Total output is: 30 Process finished with exit code 0The second way is that take a user input and assign in some variable and then add that two variable and assign in any variable. But by default python take string input so we have to convert it into an integer.
Example:
variable1 = input('Enter your first number:') # By defult input is srint varible1 = int(variable1) #String conveted into integer. variable2 = input('Enter your second number:') varible1 = int(variable2) #String conveted into integer. variable3 = variable1 + variable2 print('Total output is: ',variable3)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py Enter your first number:10 Enter your second number:20 Total output is: 30 Process finished with exit code 0
How to convert each character of String as integer and then Sum it
user will enter the value in the string but we are converting each string into an integer and then adding all
Example:
num=input("enter digit to sum it: ") total=int(num[0])+int(num[1])+int(num[2])+int(num[3]) print("total sum is:",total)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py enter digit to sum it: 1234 total sum is: 10 Process finished with exit code 0
How to convert each character of String as integer and then Sum it using while loop
Here also user will enter string value as input than in while loop find the length of input and then converting input into integer then performing an addition operation
Example:
number = input("enter number:") totals=0 i=0 while i < len(number): totals+=int(number[i]) i+=1 print("Total sum is:",totals)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py enter number:1234 Total sum is: 10 Process finished with exit code 0
Recommended Post:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number