How to sum two input numbers in python

青灯夜游
Release: 2023-01-05 16:08:44
Original
67277 people have browsed it

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))
Copy after login

Output:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!