Home >Backend Development >Python Tutorial >How to sum two input numbers in python

How to sum two input numbers in python

青灯夜游
青灯夜游Original
2021-04-15 14:33:3167436browse

How to input two numbers for sum in python: first use the input() function to receive the two numbers input from the keyboard; then use the float() function to uniformly convert the two received values ​​into floating point numbers; Then use the " " operator to add the two numbers to get an added value; finally use the print() function to output the value.

How to sum two input numbers in python

#The operating environment of this tutorial: Windows 7 system, Python 3 version, Dell G3 computer.

The user inputs two numbers and calculates the sum of the two numbers

# -*- coding: UTF-8 -*-

# 用户输入数字
num1 = input('输入第一个数字:')
num2 = input('输入第二个数字:')
 
# 求和
sum = float(num1) + float(num2)
 
# 显示计算结果
print('数字 {0} 和 {1} 相加结果为: {2}'.format(num1, num2, sum))

Output:

输入第一个数字:1.5
输入第二个数字:2.5
数字 1.5 和 2.5 相加结果为: 4.0

Related function description

The input() function in Python3.x accepts a standard input data and returns it as string type.

float() function is used to convert integers and strings into floating point numbers.

【Related recommendations: Python3 video tutorial

The above is the detailed content of How to sum two input numbers in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn