This article is participating in Python Theme Month. See the link to the event for more details

preface

I believe that there is a serious study of the last class of students, have mastered the use of variable numerical types, although the content is a little simple, but still hope that students have mastered the future or more practice, so nonsense is not to say, let us begin to learn the content of this class!

What is an operator

An operator is a special symbol used to represent data operations, assignments, comparisons, etc. The Python language uses operators to concatenate one or more operations into executable statements that implement specific functions. Operators in Python are classified into the following types: ~ Assignment operator ~ arithmetic operator ~ bitwise operator ~ index operator ~ comparison operator ~ logical operator

Assignment operator

The assignment operator is used to specify a value for a variable or constant. Python uses “=” for the assignment operator. In general, the assignment operator is used to assign the value of an expression to another variable. Ex. :

st = "python"
pi = st
print(PI) Output: PythonCopy the code

As an additional note, Python also supports continuous assignment of multiple variables. For example:

a = b = c = 20
print(b) Output results:20
Copy the code

Arithmetic operator

Python supports all of the basic arithmetic operators that perform basic mathematical operations such as addition, subtraction, multiplication, division, and remainder. + : the addition operator, for example:

a = 5.2
b = 3.1
ab_sum = a + b
print(ab_sum)8.3
Copy the code

– : Subtraction operator, for example:

a = 5.2
b = 3.1
sub = a - b
print(sub) Output:2.1
Copy the code

In addition to being used as a subtraction operator, “-” can also be used as a negation operator. For example:

a = -5.0
x = -a
print(x) Output:5.0
Copy the code

* : the multiplication operator, for example:

a = 5.2
b = 3.1
ab_sum = a * b
print(ab_sum)16.12
Copy the code

/ : division operator, for example:

a = 8
b = 4
ab_sum = a / b
print(ab_sum)2.0
Copy the code

// : divisible operator, for example:

a = 5.2
b = 3.1
ab_sum = a // b
print(ab_sum)1.0
Copy the code

In addition, Python does not allow using 0 as a divisor, or a ZeroDivisonError will be raised.

% : the remainder operator. Python does not require that both operands of the remainder operator be integers. Python’s remainder operator fully supports the remainder of floating point numbers. The remainder is the result of dividing the second operand by the first operand. The remainder is the result of dividing the first operand by the second. For example:

a = 5.2
b = 3.1
ab_sum = a % b
print(ab_sum)2.1
Copy the code

** : power operator, for example:

a = 5支那2
print(a) Output results:25
Copy the code

Bitwise operators (simple enough)

Bit operators are commonly used in low-level development such as graphics, image processing, and creating device drivers. Bit-operators can be used to directly manipulate the original bit bits of a numeric value, especially when using custom protocols to communicate, using bit-operators to encode and decode the original data is also very effective. & : bitwise and | : bitwise or ^ : bitwise xor ~ : take the bitwise < < : left displacement operator. > > : right shift operator

Extended assignment operator

Assignment operators can be combined with arithmetic operators, bitwise operators, etc., to extend the more powerful operators. The extended assignment operator looks like this:

Index operator

The index operator, which was used earlier in the string section, is the square brackets, within which you can use either a single index value or an index range. In fact, you can also specify steps when using index ranges. Ex. :

a = 'abcdefghijklkmn'
print(a[2:8:3]) Output: cfCopy the code

Compare operators with bool types

Python provides bool types to represent True (True) or false (false), such as the common 5 > 3 comparison. This is correct and is called True (True) in the program world. Python uses True for this; In the programming world, this is called false. Python uses false for this.

Ex. :

a = 5 
b = 4
print(a > b)
print(a < b)
print(a >= b)
print(b <= 4)
print(a == b)
print(a ! = b)print(a is b)
print(a is notB) Output results:True
False
True
True
False
True
False
True
Copy the code

Note: == looks similar to IS, but is essentially different. == only compares the values of two variables, but IS requires both variables to reference the same object.

Logical operator

The logical operator operates on a variable, constant, or expression of type bool. The return value of the operation is also a bool. Python’s logical operators have the following three: and: returns True only when both operands are True; Otherwise return false or: or, as long as one of the operands is True, return True; Otherwise return false. Not: no, only one operand is required. If the operand is True, return false; Return True if the operand is False

Ex. :

print(5 > 3 and 20 > 5)
print(5 > 6 or 20 > 5)
print(not 5 > 6) Output results:True
True
True
Copy the code

The ternary operator (emphasis) is different from other programming languages

Python implements the function of a ternary operator through an if statement, so you can think of this statement as an approximate ternary operator. The syntax of an if statement as a ternary operator is as follows:

True_statements if expression else False_statements
Copy the code

Ex. :

a = 5
b = 3
st = "A greater than b" if a > b else "A is not greater than B"
print(st) Output result: A is greater than BCopy the code

Python allows multiple statements to be placed in True_statements or False_statements of the ternary operators. Python supports two types of placement: Separate multiple statements by commas (,) : Each statement is executed, and the program returns a tuple of the return values of multiple statements

Multiple statements are separated by semicolons: Each statement is executed each day, and the program returns only the value of the first statement

In the first case, for example:

a = 5
b = 4
st = print("Python ternary operators"), "A greater than b" if a > b else "A is not greater than B"
print(st) Output: Python ternary operator (None.'a greater than b)
Copy the code

In the second case, for example:

a = 5
b = 4
st = print("Python ternary operators"); "A greater than b" if a > b else "A is not greater than B"
print(st) Output: The Python ternary operatorNone
Copy the code

The in operator.

Python provides the in operator to determine whether a member is in a sequence. For example, STR is a sequence. Therefore, programs can use the in operator to determine whether a string contains a particular substring.

s = 'crazyit.org'
print('it' in s)
print('it' not inS) Output result:True
False
Copy the code

Operator associativity and precedence

All mathematical operations proceed from left to right, and most operators in Python combine from left to right, with the exception of the monocular, assignment, and triadic operators, which combine from right to left; that is, they operate from right to left.

Multiplication and addition are two combinable operators, that is, the operands on the left and right sides of the operators can be interchanged without affecting the result.

Operators have different priorities, which is the order in which they are evaluated in an expression. Here is their priority order

Questions and answers after class

1. Why is there no video tutorial? A: The video tutorial is in preparation for recording. Please wait patiently

2. Will the course be updated every day? A: The course will not be updated every day, because I have a busy work schedule, so I can’t promise to update the course every day, maybe 2 or 3 times a week.

3. Now you have learned the basics of Python, will you continue to learn the actual Python? A: Because this tutorial is free for students who start from the basic, you can first pay attention to the following public account, pay attention to the notice of the public account, and after the basic update, you will continue to update the actual combat (including: crawler, Django, flask, big data, artificial intelligence, etc.)

After-class supervision

Recently a part of the students in the background of private chat I said, I am ready to learn every day, but a go to the computer desk is not up to work, how to do? For students in this situation, I decide to spend some time every day to supervise and tutor your study. If you need to, click “Contact author” to register.

conclusion

The content of the course is only so much, I hope everyone can seriously to study each lesson well, remember to learn the way of programming is really a little bitter, but every day can insist on down, there will certainly be a lot of harvest, will be in the next class some exercises, content is about learning to consolidate the basis of the front, stay tuned.