Trong chương trình này, bạn sẽ học cách thêm hai số và hiển thị nó bằng hàm in [].
Để hiểu ví dụ này, bạn nên có kiến thức về các chủ đề lập trình Python sau:
- Đầu vào, đầu ra và nhập khẩu của Python
- Kiểu dữ liệu Python
- Các nhà khai thác Python
Trong chương trình dưới đây, chúng tôi đã sử dụng toán tử
The sum of 1.5 and 6.3 is 7.80 để thêm hai số.
Ví dụ 1: Thêm hai số
# This program adds two numbers
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
Đầu ra
The sum of 1.5 and 6.3 is 7.8
Chương trình bên dưới tính toán tổng của hai số được nhập bởi người dùng ..
Ví dụ 2: Thêm hai số với đầu vào của người dùng
# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
Đầu ra
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8
Chương trình bên dưới tính toán tổng của hai số được nhập bởi người dùng ..
Ví dụ 2: Thêm hai số với đầu vào của người dùng
Trong chương trình này, chúng tôi đã yêu cầu người dùng nhập hai số và chương trình này hiển thị tổng của hai số được nhập bởi người dùng.
print['The sum is %.1f' %[float[input['Enter first number: ']] + float[input['Enter second number: ']]]]
Đầu ra
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8
Chương trình bên dưới tính toán tổng của hai số được nhập bởi người dùng ..
Tại đây bạn sẽ nhận được chương trình Python để thêm hai số. Chương trình trước tiên sẽ yêu cầu người dùng nhập hai số, tính tổng của họ và cuối cùng in nó. Input [] là một hàm sẵn có được sử dụng để lấy đầu vào từ người dùng.is an inbuilt function which is used to take input from user.
a = int [input ["nhập số đầu tiên:"]]=int[input["enter first number: "]] b = int [input ["nhập số thứ hai:"]]=int[input["enter second number: "]] sum=a+b=a+
b print["sum:",sum]["sum:",sum]Chương trình Python để thêm hai số
Đầu ra
Nhập số thứ nhất: 5Nenter Số thứ hai: 7Sum: 12
enter second number:7
sum: 12
Video hướng dẫn
Cần trợ giúp lập trình Python? Truy cập gertmentXP.com, họ có lập trình viên Python chuyên gia có sẵn 24*7.Python Programming Help? Visit AssignmentXp.com, they have expert python programmer available 24*7.
bài chuyển hướng
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcExamples:
Input: num1 = 5, num2 = 3 Output: 8 Input: num1 = 13, num2 = 6 Output: 19
Trong chương trình bên dưới để thêm hai số, người dùng trước tiên được yêu cầu nhập hai số và đầu vào được quét bằng hàm input [] và được lưu trữ trong các biến số 1 và số2. Sau đó, các biến số1 và số2 được thêm vào bằng toán tử số học + và kết quả được lưu trữ trong tổng biến. Dưới đây là chương trình Python để thêm hai số: & nbsp;
Ví dụ 1: & nbsp;
Python3
The sum of 1.5 and 6.3 is 7.84
The sum of 1.5 and 6.3 is 7.85
The sum of 1.5 and 6.3 is 7.86
The sum of 1.5 and 6.3 is 7.87
The sum of 1.5 and 6.3 is 7.85
The sum of 1.5 and 6.3 is 7.89
# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
0 The sum of 1.5 and 6.3 is 7.85
The sum of 1.5 and 6.3 is 7.84
The sum of 1.5 and 6.3 is 7.80
# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
4# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
5# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
6# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
7 # Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
8# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
9Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.80
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.81
Output:
Sum of 15 and 12 is 27
Ví dụ 2: Thêm hai số được cung cấp bởi người dùng Input & NBSP;Adding two number provided by user input
Python3
print['The sum is %.1f' %[float[input['Enter first number: ']] + float[input['Enter second number: ']]]]
3The sum of 1.5 and 6.3 is 7.85
print['The sum is %.1f' %[float[input['Enter first number: ']] + float[input['Enter second number: ']]]]
5print['The sum is %.1f' %[float[input['Enter first number: ']] + float[input['Enter second number: ']]]]
6print['The sum is %.1f' %[float[input['Enter first number: ']] + float[input['Enter second number: ']]]]
7The sum of 1.5 and 6.3 is 7.85
print['The sum is %.1f' %[float[input['Enter first number: ']] + float[input['Enter second number: ']]]]
5Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.80
# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
0 The sum of 1.5 and 6.3 is 7.85
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.83
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.84
The sum of 1.5 and 6.3 is 7.80
# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
5Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.89
# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
0Output:
First number: 13.5 Second number: 1.54 The sum of 13.5 and 1.54 is 15.04
Ví dụ 3:
Python3
Sum of 15 and 12 is 276
Sum of 15 and 12 is 2777
The sum of 1.5 and 6.3 is 7.85
The sum of 1.5 and 6.3 is 7.85
First number: 13.5 Second number: 1.54 The sum of 13.5 and 1.54 is 15.040
First number: 13.5 Second number: 1.54 The sum of 13.5 and 1.54 is 15.042
The sum of 1.5 and 6.3 is 7.84
The sum of 1.5 and 6.3 is 7.85
The sum of 1.5 and 6.3 is 7.86
First number: 13.5 Second number: 1.54 The sum of 13.5 and 1.54 is 15.042
The sum of 1.5 and 6.3 is 7.87
The sum of 1.5 and 6.3 is 7.85
The sum of 1.5 and 6.3 is 7.89
First number: 13.5 Second number: 1.54 The sum of 13.5 and 1.54 is 15.042
Sum of 15 and 12 is 27;1
The sum of 1.5 and 6.3 is 7.85
Sum of 15 and 12 is 27;3
Sum of 15 and 12 is 27;4____10
# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
4First number: 13.5 Second number: 1.54 The sum of 13.5 and 1.54 is 15.042
# Store input numbers
num1 = input['Enter first number: ']
num2 = input['Enter second number: ']
# Add two numbers
sum = float[num1] + float[num2]
# Display the sum
print['The sum of {0} and {1} is {2}'.format[num1, num2, sum]]
5Sum of 15 and 12 is 27;9
The sum of 1.5 and 6.3 is 7.800
The sum of 1.5 and 6.3 is 7.801
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.89
The sum of 1.5 and 6.3 is 7.803
Output:
Sum of 15 and 12 is 27;