Hướng dẫn write a program to print the sum of two numbers in python - viết một chương trình để in ra tổng của hai số 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:

    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.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

    # 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

    # 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

    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: '))))
    
    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

    # 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

    # 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

    Output:

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

    First number: 13.5 Second number: 1.54
    The sum of 13.5 and 1.54 is 15.04
    2
    Sum of 15 and 12 is 27;
    1
    The sum of 1.5 and 6.3 is 7.8
    5
    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))
    
    4

    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 viết một chương trình để tìm tổng của hai số?

    printf ("Nhập hai số nguyên:");Scanf (" %d %d", & số1, & số2);Sau đó, hai số này được thêm vào bằng toán tử + và kết quả được lưu trữ trong biến tổng.Cuối cùng, hàm printf () được sử dụng để hiển thị tổng số.printf (" %d + %d = %d", số 1, số2, tổng);printf("%d + %d = %d", number1, number2, sum);

    Làm thế nào để bạn thêm tổng của một số trong Python?

    Sử dụng python's sum () để thêm một số giá trị số một cách hiệu quả.Danh sách concatenate và bộ dữ liệu với SUM () sử dụng SUM () để tiếp cận các vấn đề tổng hợp phổ biến. efficiently. Concatenate lists and tuples with sum() Use sum() to approach common summation problems.

    Chương trình thêm hai số vào Python là gì?

    Hàm tổng python () tính toán tổng các phần tử của ITBER có thể.Có thể là một danh sách, một tuple, hoặc một từ điển.Hàm sum () hoạt động với cả số nguyên và số dấu phẩy động.sum() function calculates the sum of elements of the iterable. Iterable can be a list, a tuple, or a dictionary. sum() function works with both integers and floating-point numbers.