YouTip LogoYouTip

Python Sum Numbers

## Python Program to Calculate the Sum of Two Numbers In Python, calculating the sum of two or more numbers is one of the most fundamental operations. This tutorial demonstrates how to perform addition in Python using variables, user input, and built-in functions. --- ## 1. Basic Addition Using Variables In this approach, we define two variables to store the numbers, use the addition operator (`+`) to calculate their sum, and then print the result. ### Code Example ```python # Define two numbers num1 = 5 num2 = 10 # Calculate the sum of the two numbers total_sum = num1 + num2 # Output the result print("The sum of the two numbers is:", total_sum) ``` ### Code Explanation 1. **`num1 = 5` and `num2 = 10`**: These lines define two variables, `num1` and `num2`, and assign them the integer values `5` and `10` respectively. 2. **`total_sum = num1 + num2`**: This line adds the values of `num1` and `num2` using the `+` operator and stores the result in the variable `total_sum`. 3. **`print("The sum of the two numbers is:", total_sum)`**: This line outputs the result to the console. The `print()` function displays the descriptive string followed by the value of `total_sum`. ### Output ```text The sum of the two numbers is: 15 ``` --- ## 2. Adding Numbers via User Input To make the program dynamic, you can accept input directly from the user using the `input()` function. Since `input()` reads data as a string, you must explicitly convert the inputs to numbers (integers or floats) before performing the addition. ### Code Example ```python # Store input numbers num1 = input("Enter first number: ") num2 = input("Enter second number: ") # Convert the inputs to float to handle both integers and decimals total_sum = float(num1) + float(num2) # Display the sum # The format() function or f-strings can be used for clean formatting print(f"The sum of {num1} and {num2} is {total_sum}") ``` ### Output Example ```text Enter first number: 12.5 Enter second number: 7.3 The sum of 12.5 and 7.3 is 19.8 ``` --- ## 3. Summing a List of Numbers Using `sum()` If you need to sum multiple numbers stored in a list or tuple, Python provides a highly efficient built-in function called `sum()`. ### Code Example ```python # Define a list of numbers numbers = [1, 2, 3, 4, 5] # Calculate the sum using the built-in sum() function total = sum(numbers) print("The sum of the list is:", total) ``` ### Output ```text The sum of the list is: 15 ``` --- ## Important Considerations * **Avoid Using Reserved Keywords**: In the basic example, we used the variable name `total_sum` instead of `sum`. This is because `sum` is a built-in Python function. Overwriting it (e.g., `sum = num1 + num2`) will prevent you from using the built-in `sum()` function later in your code. * **Type Conversion**: Always convert user input using `int()` (for whole numbers) or `float()` (for decimals). Attempting to add two input strings directly (e.g., `"5" + "10"`) will result in string concatenation (`"510"`) instead of mathematical addition.
← Python Even CheckSelenium Practical β†’