Python Add Two Numbers Examples
Adding two numbers is one of the simplest operations in Python. Here are 5 examples showing different ways to add numbers.
दो नंबर जोड़ना Python में सबसे आसान ऑपरेशन है। यहाँ 5 उदाहरण दिए गए हैं जो अलग-अलग तरीकों से जोड़ना दिखाते हैं।
Examples of Adding Numbers in Python
Example 1: Adding Two Numbers Using Variables
a = 5
b = 7
sum = a + b
print('Sum:', sum)
Output:
Adds two integer variables a and b and prints the sum.
दो integer variables a और b को जोड़कर परिणाम दिखाता है।
Example 2: Adding Two Numbers Using Input()
a = int(input('Enter first number: '))
b = int(input('Enter second number: '))
sum = a + b
print('Sum:', sum)
Output:
Enter second number: 4
Sum: 7
Reads two integers from user input and adds them.
यूजर से दो integer input लेकर उन्हें जोड़ता है।
Example 3: Adding Two Float Numbers
a = 3.5
b = 4.2
sum = a + b
print('Sum:', sum)
Output:
Adds two floating point numbers.
दो floating point नंबर जोड़ता है।
Example 4: Adding Numbers Using a Function
def add_numbers(x, y):
return x + y
result = add_numbers(10, 20)
print('Sum:', result)
Output:
Defines a function to add two numbers and calls it.
दो नंबर जोड़ने के लिए function बनाता है और उसे कॉल करता है।
Example 5: Adding Numbers from a List
numbers = [1, 2, 3, 4]
sum = 0
for num in numbers:
sum += num
print('Sum:', sum)
Output:
Adds all numbers in a list using a loop.
एक list के सभी नंबरों को loop से जोड़ता है।