Python basic operators

Reference link: Basic operators in Python

Article Directory

1 Arithmetic operator 2 Bit operator 3 Comparison operator 4 Assignment operator 5 Identity operator 6 Member operator 7 Logical operator

Today I summarize the meaning of the seven major operators in Python. This article is roughly sorted according to the priority of the operators. You can see the priority of each operator by looking at the catalog~~

1 Arithmetic Operator

Exponent ** has the highest priority, followed by multiplication *, division /, rounding //, remaining %, and finally adding + and subtracting -.

Here I will focus on /, //, %. The other operators are very simple, so I don't need to explain more.

1 ) A slash /, division, division of two ints also retain decimals.

3 /2 = 1.5

10 /3 = 3.3333333333333335

4 /2 = 2.0

2 ) Two slashes // round down the result of division. When there are floating-point types on both sides of //, .0 will be retained on the basis of rounding the result.

3 //2 = 1

4 //2 = 2

4 //2.0 = 2.0

3 ) Percent sign %, take the modulus, and return the remainder of the division. Both sides of% must be integers. When there are negative integers, the sign of the remainder is the same as that on the right side of %.

5 %3 = 2

3 %(-2) = -1 # The sign is consistent with -2, so it is -1

4 %2 = 0

4 %2.0 = 0.0

2 Bit operator

Bitwise operators are calculated based on binary. In binary, 1 is usually True, and 0 is usually False:

1 ) Bitwise logical operators: and &, or |, exclusive or ^, negate ~.

&: bitwise AND operator, the corresponding bit of two binary numbers is 1, then the result of this bit is 1, otherwise it is 0; |: bitwise OR operator, when one of the corresponding bits of two binary numbers is 1, The result bit is 1; ^: XOR operator, when the corresponding bits of two binary numbers are different, the result is 1; ~: Negation operator, the binary is inverted, that is, the inversion of 1 is 0, and the inversion of 0 is 1.

2 ) Shift operator: Left shift <<, right shift >>.

<< Each shift of one bit is equivalent to multiplying by 2. Each shift of >> is equivalent to dividing by 2. If it cannot be divided evenly, it is rounded down.

Seeing this, you may not understand what the bitwise operator is talking about... In order to make this clear, I will write another article. Due to space limitations, this time I won’t go into depth~

Just know that there is such a thing for the time being, fill in the hole next time~

3 Comparison operator

The most basic: less than <, greater than>, equal to ==, less than or equal to <=, greater than or equal to >=, not equal to !=. These are clear at a glance, and there is no need to explain.

4 Assignment operator

1 ) Is equal to =, it's very simple, just put the value on the right with the label on the left. 2) +=, that is, add the original value on the right and left, and update the value on the left to the added new value. For example, a+=1 is equivalent to a=a+1, but a=a+1 is not as efficient as a+=1. 3) Similarly, there are -=, *=, /=, %=, **=, //=.

5 Identity operator

The identity operators are is and is not. Here to talk about the difference between is, =, == which is easy for novices to confuse.

1 ) Let me talk about is and ==

is compares whether the id values of two objects are equal, that is, whether they point to the same memory address; == compares whether the contents of the two objects are equal, that is, whether the values are equal.

a = "Python"

b = a

b == a

True

id(a)

2974848979888 # a's address

id(b)

2974848979888 # The address of b is the same as a

b is a

True

In the above example, the memory addresses of b and a are the same, they point to the same block of memory, so the results of is and == are both True, because direct assignments are references to values. If b and a point to different memories, then the result of b is a is False, and the result of b==a is True.

a = [1,2,3]

b = [1,2,3]

print(a == b)

True

id(a)

2974887936648

id(b)

2974887936456 # Although the elements in the two lists a and b are the same, the addresses are different

print(a is b)

False

One last thing to note is that Python only caches smaller integer objects (range [-5, 256]), not all integer objects. For example, only small integer objects [-5,256] are put into the cache for reuse:

a = 1

b = 1

print(a == b)

True

print(a is b)

True

a = 257

b = 257

print(a == b)

True

print(a is b)

False

However, this situation can only be executed in the command line. When executed in Pycharm or saved as a file, the result is different. This is because the interpreter has made some optimizations.

2 ) Look at = and ==

These two are easier to distinguish.

= The meaning of is to assign a value to a variable, such as a=3, assign the value 3 to a; == is to judge whether they are equal, and return True or False. For example, if 1 == 1, if they are equal, return True; if 1 == 2, if they are not equal, then return False.

a = [1,2]

b = [1,2]

c = a

print(a == b)

True

print(a is b)

False

print(a is c)

True

3 )!= and is not

Double equal sign == and inequality sign != judge whether they are equal; is and is no t judge whether they are the same object.

a = [1,2,3]

b = [1,2,3]

print(a == b)

True

print(a != b)

False

print(a is b)

False

print(a is not b)

True

6 Member operator

in and not in, used to determine whether the element is in the current object

print('ab' in 'abc')

True

print(a in ['a','b','c'])

False

print('a' in ['a','b','c'])

True

print(['a','b'] in ['a','b','c'])

False

print(['a','b'] not in ['a','b','c'])

True

7 Logical Operators

Priority: not> and> or.

If both sides of and are True, return True, otherwise return False; if both sides are False, return False, otherwise return True.

Short circuit phenomenon:

The expression before and is False, then the following expressions are not executed; the expression before or is True, then the following expressions are not executed.

print(1 > 2 and  3 < 2 or not 4 > 5)

True

Disassemble the above example, 1> 2 and 3 <2 or not 4> 5.

Because the priority of the comparison operator is higher than the logical operator, the first step is converted to False and False or not False; because the priority of not is greater than and, it is converted to False and False or True; because the priority of and is greater than or, it is converted False or True; the final result is True.

Recommended Posts

Python basic operators
Python basic syntax (1)
Python3 basic syntax
Python basic summary
03. Operators in Python entry
Python basic syntax generator
Python basic drawing tutorial (1)
Python function basic learning
python_ crawler basic learning
Python basic data types
Basic syntax of Python
Basic knowledge of Python (1)
Python basic data types
Python basic syntax iteration
Python basic knowledge question bank
Python basic syntax list production
Python introductory notes [basic grammar (on)]
Python entry notes [basic grammar (below)]
Python file operation basic process analysis
Python crawler basic knowledge points finishing
Real zero basic Python development web
Python basic syntax and number types
Basic analysis of Python turtle library implementation
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
Python3 module
python (you-get)
Python string
Python basics
Python descriptor
Python exec
Python notes
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python basic actual combat-guess the age game
Python3 list
Python multitasking-coroutine
python introduction
07. Python3 functions
Python basics 3
Python multitasking-threads
python sys.stdout
Python entry-3
Centos 7.5 python3.6
python queue Queue
Python basics 4
Python basics 5