Spend half an hour reading this article, and you will get something!
catalogue
Operator
Operator | operation | example | Evaluated as |
** | index | 2**3 | 8 |
% | Modulo / remainder | 22%8 | 5 |
// | Integer division / quotient rounding | 21//8 | 2 |
/ | division | 21/8 | 2.625 |
* | multiplication | 3*5 | 15 |
- | subtraction | 3-1 | 2 |
+ | addition | 3+1 | 4 |
Comparison operator
Operator | meaning |
== | be equal to |
!= | Not equal to |
< | less than |
> | greater than |
<= | Less than greater than |
>= | Greater than or equal to |
print(1 == 1) #T print(1 == 2) #F print(1 != 2) #T print(1 != 1) #F print('hello' == 'hello') #T print('hello' == 'world') #F print('t' == 'T') #F print(True == True) #T print(True != False) #T print(1 < 2) #T print(1 > 2) #F print(1 <= 2) #T print(1 < 1) #F
These are some comparisons of comparison operators!
boolean operation
Boolean operators (or, and, not)
Binary Boolean operation
expression | Evaluated as |
True and False | True |
True and True | False |
False and True | False |
False and False | False |
expression | Evaluated as |
True or False | True |
True or True | True |
False or True | True |
False or False | False |
expression | Evaluated as |
not True | False |
not False | True |
The above are the different return values of the three operators. Let's take a look at the examples:
print((4 < 5) and (5 < 6)) # True print((4 < 5) and (5 > 6)) #False print((4 < 5) or (5 > 6)) #T print((4 > 5) or (5 > 6)) #F print(1 + 1 == 2 or not 2 + 2 == 4) #T print(1 + 1 == 2 and not 2 + 2 == 4) #F
data type
Common data types are integer (int), floating point (float), and string type (str)
data type | example |
---|---|
Integer (int) | -1,-2,0,1,2,3 |
Floating point | -1.1,-0.1,1.1,1.11 |
String (str) | 'a','b','abcd','123456' |
# The str output in pycharm here is the same as the int type. In order to distinguish, it is recommended to use idle print(str(10)) print(int(10)) ptint(float(10))
operator
Integers: integers are the same as numbers in mathematics, such as 1, 2, 3, 4, 5... These are integers as long as they are numbers without decimal points, not Chinese characters or special characters.
We can perform (+) (-) (*) (/) operations on integers. Look at the example!
print(2 + 3) # addition print(3 - 2) # subtraction print(6 / 3) # Division. The result here has a decimal point. If you don't want to add an int integer to the decimal point for conversion, it's good eg: print(int(6 / 3)) print(2 * 3) # multiplication
Floating point numbers: such as 0.3, 0.1 and 0.001 are all floating point numbers
There are two special examples in the calculation process that get different results, but python will try its best to find accurate results for us. These two special examples have such problems in most languages:
print(0.2 + 0.1) # The printed result is: 0.300000000000000 4 print(3 * 0.1) # The printed result is: 0.300000000000000 4 print(0.2 + 0.3) # The printed result is: 0.5
Integer and floating-point number: divide any two numbers and the result is a floating-point number. Even if an integer can be divided, one is an integer and the other is a floating-point number, the result is also a floating-point number. See example:
print(3 / 2) # The result is 3.0 print(1 + 2.0) # The result is: 3.0 print(2 * 3.0) # The result is: 6.0 print(2.0 ** 3) # To the third power of 2.0, the result is 8.0
Underline in number: it is mainly used when the number is large and plays a role in grouping. The actual size of the number remains unchanged. See example:
#Set a variable i and print i i = 10000_000 print(i)
The result is:
10000000
Assign values to multiple variables at the same time: this method is often used to assign a series of data to a group of variables. See an example:
x, y, z = 1, 2, 3 print(x, y, z)
Here is just a demonstration of how to assign values. They are one-to-one correspondence (x = 1, y = 2, z = 3). This writing also improves the readability of the code. The result is:
1 2 3
last! Let's have a comment!!! (≖ ‿ ≖)✧
As I wrote at the door, the notes # are actually comments. Comments serve as a code description. When the amount of code is large, you can find the code location you need more effectively# The content of comments will be ignored by the python interpreter to run the code well! Adding comments is also a good habit.