Hướng dẫn how do you sum two inputs in python? - làm thế nào để bạn tổng hợp hai đầu vào trong python?

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.8
0 để 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.

Chương trình Python để thêm hai số

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)

Đầ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

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    ĐọcExamples:

    Bàn luận

    Cho hai số num1 và num2. Nhiệm vụ là viết một chương trình Python để tìm việc bổ sung hai số này. Ví dụ:

    Input: num1 = 5, num2 = 3
    Output: 8
    
    Input: num1 = 13, num2 = 6
    Output: 19

    Python3

    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;

    The sum of 1.5 and 6.3 is 7.8
    4
    The sum of 1.5 and 6.3 is 7.8
    5
    The sum of 1.5 and 6.3 is 7.8
    6

    The sum of 1.5 and 6.3 is 7.8
    7
    The sum of 1.5 and 6.3 is 7.8
    5
    The sum of 1.5 and 6.3 is 7.8
    9

    Output:

    Sum of 15 and 12 is 27

    # 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.8
    5
    The sum of 1.5 and 6.3 is 7.8
    4
    The sum of 1.5 and 6.3 is 7.8
    0
    # 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
    Adding two number provided by user input 

    Python3

    # 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))
    
    9
    Enter first number: 1.5
    Enter second number: 6.3
    The sum of 1.5 and 6.3 is 7.8
    0
    Enter first number: 1.5
    Enter second number: 6.3
    The sum of 1.5 and 6.3 is 7.8
    1

    Ví dụ 2: Thêm hai số được cung cấp bởi người dùng Input & NBSP;

    print('The sum is %.1f' %(float(input('Enter first number: ')) + float(input('Enter second number: '))))
    
    3
    The sum of 1.5 and 6.3 is 7.8
    5
    print('The sum is %.1f' %(float(input('Enter first number: ')) + float(input('Enter second number: '))))
    
    5
    print('The sum is %.1f' %(float(input('Enter first number: ')) + float(input('Enter second number: '))))
    
    6

    print('The sum is %.1f' %(float(input('Enter first number: ')) + float(input('Enter second number: '))))
    
    7
    The sum of 1.5 and 6.3 is 7.8
    5
    print('The sum is %.1f' %(float(input('Enter first number: ')) + float(input('Enter second number: '))))
    
    5
    Enter first number: 1.5
    Enter second number: 6.3
    The sum of 1.5 and 6.3 is 7.8
    0

    Output:

    First number: 13.5 Second number: 1.54
    The sum of 13.5 and 1.54 is 15.04

    # 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.8
    5
    Enter first number: 1.5
    Enter second number: 6.3
    The sum of 1.5 and 6.3 is 7.8
    3
    Enter first number: 1.5
    Enter second number: 6.3
    The sum of 1.5 and 6.3 is 7.8
    4
    The sum of 1.5 and 6.3 is 7.8
    0

    Python3

    # 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
    Enter first number: 1.5
    Enter second number: 6.3
    The sum of 1.5 and 6.3 is 7.8
    9
    # 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

    Ví dụ 3:

    Sum of 15 and 12 is 27
    6
    Sum of 15 and 12 is 27
    77
    The sum of 1.5 and 6.3 is 7.8
    5
    The sum of 1.5 and 6.3 is 7.8
    5
    First number: 13.5 Second number: 1.54
    The sum of 13.5 and 1.54 is 15.04
    0

    First number: 13.5 Second number: 1.54
    The sum of 13.5 and 1.54 is 15.04
    2
    The sum of 1.5 and 6.3 is 7.8
    4
    The sum of 1.5 and 6.3 is 7.8
    5
    The sum of 1.5 and 6.3 is 7.8
    6

    First number: 13.5 Second number: 1.54
    The sum of 13.5 and 1.54 is 15.04
    2
    # 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
    Sum of 15 and 12 is 27;
    9
    The sum of 1.5 and 6.3 is 7.8
    00
    The sum of 1.5 and 6.3 is 7.8
    01
    Enter first number: 1.5
    Enter second number: 6.3
    The sum of 1.5 and 6.3 is 7.8
    9
    The sum of 1.5 and 6.3 is 7.8
    03

    Output:

    Sum of 15 and 12 is 27;

    Làm thế nào để bạn thêm số đầu vào trong Python?

    Ví dụ 2: Thêm hai số với đầu vào người dùng, chúng tôi sử dụng đầu vào hàm tích hợp () để lấy đầu vào.Vì, input () trả về một chuỗi, chúng tôi chuyển đổi chuỗi thành số bằng hàm float ().Sau đó, các số được thêm vào.use the built-in function input() to take the input. Since, input() returns a string, we convert the string into number using the float() function. Then, the numbers are added.

    Làm thế nào để bạn thêm hai số trong danh sách Python?

    Khoa học dữ liệu thực tế sử dụng Python..
    Lấy hai danh sách L1 và L2.Khởi tạo đầu và nhiệt độ là null ..
    C: = 0 ..
    Trong khi L1 và L2 đều là danh sách không trống.Nếu l1 không trống, thì hãy đặt A: = 0, nếu không thì đặt A: = l1.val.....
    Nếu C là không khác, thì.Nút: = Nút mới có giá trị 1, đầu tiếp theo: = nút ..
    trở lại tạm thời ..

    Làm thế nào để bạn tìm thấy tổng số nguyên giữa hai số trong Python?

    Cách dễ nhất mà tôi biết:..
    Bắt đầu = 1 # Bắt đầu phạm vi của bạn ..
    kết thúc = 10 # kết thúc phạm vi của bạn ..
    num_list = phạm vi (bắt đầu, kết thúc + 1) # Danh sách tất cả các số trong phạm vi đã cho ..
    Final_sum = sum (num_list).
    In "Sum cuối cùng =", Final_Sum ..
    #Combining tất cả cùng nhau trong một dòng ..
    in "tổng cuối cùng =", tổng (phạm vi (bắt đầu, kết thúc + 1)).