Python programming entry (3): arithmetic expressions and

All of the following examples are based on the latest version of Python, in order to facilitate digestion, each one as much as possible dapper, I hope you can try to master the Python programming "concept", if you can go hands-on try these examples (even if currently not fully get to know ), to deepen understanding.

The computer is the essence of mathematical calculations. All operations are complicated by the combination obtained by a simple operation.

arithmetic

The basic operations are addition, subtraction, multiplication, divisible, take the remainder.
Addition, subtraction, multiplication, and the same elementary school mathematics.

1 + 2
1 - 2
1 * 2

Division with a /meaning that the symbol that will do floating-point operations, the results of decimals.

# 结果为 1.5
3 / 2

Divisible by //this notation, do operation between integers, the result will be rounded down (to remove all of the fractional part).

# 只取整数,所以结果为 1
3 // 2 

By modulo %represent this symbol, is made between integer arithmetic, is the remainder after taking divisible finished.

# 3 除以 2 得 1,余下 1,所以结果为 1
3 % 2

# 5 除以 3 得 1,余下 2,所以结果为 2
5 % 3

What is the expression?

Expression returns a value.

A number can be an expression, such as the number 1, it will give you the result of the operation returns 1, so 1 is a representation.
All arithmetic operations, for example 1 + 2, the operation result will give you returns 3, so 1 + 2 is an expression.

Expressions and expressions are nested, the result was an expression.

For example expression 1 + 2 * 3, 2 * 3 wherein the expression is also found to 6, 1 + 6 and 7 have, still expression.
Primary and mathematics as with the brackets can be changed priority, calculates, for example, (1 + 2) * 3 to give 9.

More complex expressions, can be broken down into simple expressions to arrive at the results.
(1 + 2 * (3 + 4)) 5 *
(1 + 2 * 7) 5 *
(1 + 14) * 5
15 * 5
75

Good practice decomposed expression, the next section talk about variables and assignment statements.

Published 26 original articles · won praise 53 · views 110 000 +

Guess you like

Origin blog.csdn.net/qq_37925422/article/details/102852662