Latest web development tutorials

Python digital sum

Document Object Reference Examples Python3

The following examples are by user input two numbers, even numbers and calculate the sum of:

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

# Filename : test.py
# author by : www.w3big.com

# 用户输入数字
num1 = input('输入第一个数字:')
num2 = input('输入第二个数字:')

# 求和
sum = float(num1) + float(num2)

# 显示计算结果
print('数字 {0} 和 {1} 相加结果为: {2}'.format(num1, num2, sum))

Execute the above code output results:

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

In this example, we entered two user numbers by summation. Use the built-in function input () to get the user's input, input () returns a string, so we need to use float () method to convert a string to a number.

Two digital computing, we use the sum plus sign (+) operator, except addition, there is a minus sign (-), multiplication (*), the division sign (/), in addition to floor (//) or take the remainder ( %). More number crunching can see our Python digital operation .

We can also be more operations as a single line of code:

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

# Filename : test.py
# author by : www.w3big.com

print('两数之和为 %.1f' %(float(input('输入第一个数字:'))+float(input('输入第二个数字:'))))

Execute the above code output results:

$ python test.py 
输入第一个数字:1.5
输入第二个数字:2.5
两数之和为 4.0

Document Object Reference Examples Python3