Description of content:
- 1, variables,
- 2, constant
- 3. Operators
1, variables,
① What is a variable
Data whose value can change during program execution, representing this storage space, need to use the variable name to find this data. Take a visual example: a supermarket locker, where you are given a slip of paper to retrieve your items. The grid of the locker can be understood as the memory space of the variable. The strip is the name of the variable. Taking and putting is to modify the data in the memory space corresponding to the name of the variable.
② Examples of using variables
Variables are “defined before they are used”. Python is a “dynamically typed language” that can bind different types of data values when assigning values. The variable type is also determined. Python can obtain the data type of a variable using the type() function as shown in the following example:
# Variable definition (determine the initial value type)
a = 1
print('%s => %d' % (type(a),a))
# Variable assignment
a = 'Python'
print('%s => %s' % (type(a),a))
The result is as follows:
# <class 'int'> => 1
# <class 'str'> => Python
Copy the code
③ Multiple variable assignments
Python supports multiple variable assignments, and the following two assignments are correct:
a = b = c = 1
A, b, c = 1,2,"Python"
Copy the code
④ Del keyword
We can use the del keyword to delete a reference to an object, but it is wrong to call a variable after deletion. For example, if we call del a and then print out a, NameError: name ‘a’ is not defined.
2, constant
There is no special way to define constants in Python. They are usually capitalized by variable names, but are just a “hint” that they are essentially variables!
3. Operators
① Arithmetic operators (7 kinds) & assignment operators (8 kinds)
The following seven arithmetic operators are included in Python:
+
(add)-
(cut)*
(乘) /
(in addition)%
More than (a)*
(Power, power)//
(Floor division, discard decimals)
The following is an example of the code used:
print("3 + 7 = %d" % (3 + 7))
print("3 - 7 = %d" % (3 - 7))
print("3 * 7 = %d" % (3 * 7))
print("7 / 3 = %f" % (7 / 3))
print("7 %% 3 = %d" % (7 % 3))
print("3 ** 6 = %d" % (7支那3))
print("3 // 6 = %f" % (7 // 3))
Copy the code
The running results are as follows:
3 + 7 = 10
3 - 7 = 4 -
3 * 7 = 21
7 / 3 = 2.333333
7 % 3 = 1
3支那6 = 343
3 / / 6 = 2.000000
Copy the code
The assignment operator is added after the arithmetic operators a = (equal), such as: a + = 1 is equivalent to: a = a + 1, 8 kinds of assignment operator: = + = – = * = * = / / = = / = %
② Comparison operator (6 kinds)
Python provides the following six comparison operators:
= =
(equal to)! =
(not equal to)>
(greater than)<
(less than)> =
(Greater than or equal to)< =
(Less than or equal to)
The result of the comparison will return a Boolean value, True or False, which is usually used in conditional judgment.
③ bit operators (6 kinds)
Bitwise operators treat numbers as binary. First of all, you need to understand that it is forbidden to switch lines. We usually talk about numbers in decimal. Constant complementation, for example, converts a decimal 6 to a binary:
6% 2 = 0 so the first digit is 0
6 / 2 = 3
3% 2 = 1 tells us that the second digit is 1
3 / 2 = 1
1% 2 = 1 tells us that the third digit is 1
# So the decimal 6 converted to binary is: 110
# Try decimal 17 to binary again:
17% 2 = 1 tells us that the first bit is 1
17 / 2 = 8
8% 2 = 0 tells us that the second bit is 0
8 / 2 = 4
4% 2 = 0 tells us that the third bit is 0
4 / 2 = 2
2% 2 = 0 tells us that the fourth bit is 0
2 / 2 = 1
1% 2 = 1 tells us that the fifth digit is 1
Copy the code
So the value of decimal 17 to binary is: 10001. Now that you know how to convert decimal to binary, how to convert binary to decimal? Add weights, such as binary 110 to decimal:
1* * 2 ^ 2 + 12 * 2 ^ ^ 1 + 0 0 = 4 + 2 = 6
Copy the code
Try 10001 to decimal again:
1* 2 ^ 4 + 0 *2 ^ 3 + 0* 2 ^ 2 + 0 *2 ^ ^ 0 = 1 + 1 * 2 + 1 = 16 17
Copy the code
Python provides the bin function to get a binary representation of an integer. Here’s an example of the six bitwise operators in Python:
a = 6
b = 17
print(%s' % bin(a))
print(%s' % bin(b))
# & if both corresponding bits are 1, otherwise 0
print('a & b = %s: %d' % (bin(a & b), a & b))
# | bitwise or, two corresponding one of 1, 1, two is not 0, 0
print('a | b = % s corresponds to a decimal value is: % d' % (bin (a | b), a | b))
# ^ Xor, the two corresponding bits are different 1, the same 0
print('a ^ b = %s' (bin(a ^ b), a ^ b))
1, 0 = 1,1 = 0
print('~a = %s: %a' % (bin(~a), ~a))
The left shift operator moves all bits to the left by a certain number of digits, discarding the highest bits and adding 0 to the lowest bits
print('a << 2 = %s the decimal value is: %a' % (bin(a << 2), a << 2))
# >The right-shift operator moves all bits to the right by a certain number of digits
print('a > > 2 = % s corresponds to a decimal value is: a' % % (bin (a > > 2), a > > 2))
Copy the code
The following output is displayed:
aThe corresponding binary value is 0b110
B Indicates the binary value 0b10001
The decimal value of a & b = 0b0 is:0
A | b = 0 b10111 corresponds to a decimal value is:23
a ^The decimal value of b = 0b10111 is:23
The decimal value of ~a = -0b111 is -7
a << 2The decimal value of = 0b11000 is:24
a >> 2The decimal value of = 0b1 is:1
Copy the code
④ Logical operators (3 kinds)
Python provides three types of logical operators: and(logical and), or(logical or), and not(logical not), which are commonly used to determine conditions. An example of this code is as follows:
score = 70
If both expressions are true, execute
if score > 60 and score < 100:
print('Pass mark')
Logic or, if an expression is true, execute it
score = -10
if score < 0 or score > 100:
print("Illegal score value, cannot be less than 0 or greater than 100")
If the expression is true, it will not be executed
score = 100
if not score == 100:
print("No perfect score.")
else:
print("Perfect")
Copy the code
The following output is displayed:
Score to pass the
The score value is invalid and cannot be less than 0 or greater than 100
Full marks
Copy the code
⑤ Member operators (2 kinds)
Python provides member operators to “determine whether a particular value is present in a specified sequence”, in and not in. Examples of code for determining whether a particular value is present in a list are as follows:
people_list = ['Ming'.'little red'.'white'.'pig']
if 'pig' in people_list:
print("Piglet is on the list.")
if 'big pig' not in people_list:
print("Big pigs are not on the list.")
Copy the code
The following output is displayed:
Piglet is in the list
Big pigs are not on the list
Copy the code
⑥ Identity operator (2 kinds)
Python provides the identity operators to “determine whether two identifiers refer to an object”, is and is not, as shown in the following example:
Common data types
a = 1
b = 1
print('Currently a and B refer to the same variable: %s' % (a is b))
Complex data types (arrays)
a = [1]
b = [1]
print('CURRENTLY a and B refer to a different variable: %s' % (a is not b))
# string
a = 'python'
b = 'python'
print('Currently a and B refer to the same variable: %s' % (a is b))
Copy the code
The following output is displayed:
Currently a and B refer to the same variable:True
Currently a and B refer to different variables:True
Currently a and B refer to the same variable:True
Copy the code
⑦ Operator priority
Operator priorities in Python are shown in the table below:
The operator | describe |
---|---|
支那 |
index |
~ + - |
Take the negative, plus or minus |
% * / / / |
Multiplication and division, remainder, floor division |
<< >> |
Move around |
& |
Bitwise and |
^ |
Xor, bitwise or |
> < <= > >= |
Comparison operator |
< > = =! = |
Equal operator |
= %= /= //= -= += *= **= |
The assignment operator |
is is not |
Identity operator |
in not in |
Member operator |
not or and |
Logical operator |
If this article is helpful to you, welcome to leave a message, like, forward quality three lian, thank 😘~