Integers and floating-point numbers
Python supports four blending of integers and floating-point numbers directly, and the arithmetic rules are exactly the same as the arithmetic rules in mathematics.
Basic operations:
1 + 2 + 3 # ==> * 5-6 # ==> 147.5/8 + 2.1 # ==> 3.0375
Using parentheses can elevate precedence, which is exactly the same as math, and note that only parentheses can be used, but parentheses may nest many layers:
(1 + 2) * 3 # ==> 9 (2.2 + 3.3)/(1.5 * (9-0.3)) # ==> 0.42145593869731807
Unlike mathematical operations, the result of a Python integer operation is still an integer, and the result of the floating-point operation is still a floating-point number:
1 + 2 # ==> integer 31.0 + 2.0 # ==> Floating point 3.0
But the result of mixed integer and floating-point numbers becomes a floating-point number:
1 + 2.0 # ==> Floating point 3.0
Why do you differentiate between integer and floating-point arithmetic? This is because the result of an integer operation is always accurate, and the result of the floating-point operation is not necessarily accurate, because the computer memory is large, and can not accurately represent an infinite loop of decimals, for example, a binary 0.1
representation is an infinite loop decimal.
Is the result not a floating-point number when the division operation of the integer is encountered in addition to the endless? Let's try it out:
11/4 # ==> 2
To the surprise of many beginners, the integer division of Python, even if it is not endless, the result is still an integer, the remainder is directly thrown away. However, Python provides a calculation of the remainder of the operation% that can be computed:
% 4 # ==> 3
If we want to calculate the exact result of 11/4, the "integer and floating-point mixed operation results are floating-point number" law, the two numbers of one into a floating point and then the operation is no problem:
11.0/4 # ==> 2.75
Task
Please calculate 2.5 + 10/4 and explain why the calculation results are not expected 5.0?
Please fix the above operation so that the calculated result is 5.0
Boolean type
We've learned that Python supports Boolean types of data, with only a Boolean type True
and False
two values, but the Boolean type has the following operations:
and Operation : Only two Boolean values are true when the result is true.
True and True # ==> Truetrue and False # ==> Falsefalse and True # ==> Falsefalse and false # = = > False
or Operation : evaluates to true as long as there is a Boolean value of true.
True or True # ==> truetrue or False # ==> TrueFalse or True # ==> TrueFalse or False # ==> Fa Lse
Non-operation : Turns true to false, or false to true:
Not True # ==> falsenot False # ==> True
Boolean operations are used in a computer to make conditional judgments, and the computer can automatically execute different follow-up code, depending on the result of the calculation as true or false.
In Python, Boolean types can also do and, or, and not with other data types, see the following code:
A = Trueprint A and ' a=t ' or ' a=f '
The result of the calculation is not a Boolean type, but the string ' a=t ', which is why?
Because Python regards 0
, 空字符串‘‘
and None
is treated as False, other numeric values and non-empty strings are considered True, so:
True and ' a=t ' evaluates to ' a=t ' continue to calculate ' a=t ' or ' a=f ' results or ' a=t '
To explain the above results, it also involves an important law of the and and or operations: short-circuit calculation.
1. In the calculation a and b
, if A is false, then according to the algorithm, the whole result must be false, so it returns a; If A is True, the entire result must depend on B, and therefore returns B.
2. In the calculation a or b
, if A is true, the whole result must be true, according to the or algorithm, and therefore return a; If A is False, the whole result must depend on B, and therefore return B.
So when the Python interpreter is doing a Boolean operation, as long as the calculation results can be determined in advance, it will not go back and return the result directly.
Task
Please run the following code and interpret the printed results:
A = ' python ' print ' Hello, ', A or ' world ' B = 'print ' Hello, ', B or ' world '
Getting Started with Python (c) integers and floating-point numbers + Boolean types