Header Ads

Add Two numbers using phython

To add two numbers in Python, you can use the + operator. For example:

# Assign two numbers to variables num1 = 10 num2 = 20 # Add the numbers and store the result in a variable result = num1 + num2 # Print the result print(result) # Output: 30

You can also use the += operator to add a number to a variable and assign the result back to the same variable:

num1 = 10 # Add 5 to num1 and assign the result back to num1 num1 += 5 print(num1) # Output: 15

You can also use the sum() function to add a list of numbers:

numbers = [1, 2, 3, 4, 5]

result = sum(numbers)

print(result) # Output: 15


Execute the above codes below








No comments

Powered by Blogger.