Starfruit Python Advanced Lecture 4-Assignment Number and Equal Sign

My CSDN blog column: https://blog.csdn.net/yty_7

Github address: https://github.com/yot777/

 

Some readers may ask, why are these "basic skills of programmers" placed in "advanced lectures"? In fact, this is just my experience. After the completion of the basic Python tutorial, everyone is eager to try and want to learn more advanced things, but for students who are not in the computer profession, they will encounter an invisible ceiling. This is the so-called "basic programmer skills", which leads to continued I encountered some difficulties while studying. Don't be afraid, I have encountered your hardships, so I will slowly share with you in advanced lectures. If you are unclear about the grammar of Python involved in the advanced lecture, please feel free to review the basic Python tutorial.

 

4.1 Assignment number:

What is the difference between a = 5 and a == 5?

Remember: an equal sign is an assignment number , and two equal signs are equal signs ! This is true for most programming languages ​​(C, C ++, Java, etc.)!

The role of the assignment number is to assign the value on the right side of the symbol (which can be a constant or a variable) to the variable on the left side of the symbol .

The following inferences can be obtained:

1. The left side of the assignment number must be a variable (such as 2 = 2 or 3 = a cannot be written). After the assignment, the value of the variable on the left side of the assignment number will change to the same value as the right side.

2. The right side of the assignment number can be a constant or a variable: if the right side of the assignment number is a variable, the value of the variable on the right side of the assignment number will not change after the assignment.

#赋值号举例1
>>> a = 0
>>> b = 5
>>> print(a)
0
>>> print(b)
5
>>> a = b
>>> print(a)
5
>>> print(b)
5

3. You can assign the same variable multiple times, or even different types of values ​​(but C, Java and other languages ​​that need to declare the variable type in advance will not work), the value stored in the variable is subject to the last assignment

#赋值号举例2
>>> s = 5
>>> s = 6
>>> s = 7
>>> print(s)
7
>>> s = "Hello"
>>> print(s)
Hello
>>> s = [1,2,3]
>>> print(s)
[1, 2, 3]

In addition, Python allows multiple variables to be assigned to the same statement at the same time. The values ​​of multiple variables can be the same or different, but pay attention to the order of the variables

#赋值号举例3
#给i,j,k三个变量赋予相同的值
>>> i = j = k = 10
>>> print(i)
10
>>> print(j)
10
>>> print(k)
10
#给x,y,z三个变量赋予不同的值
>>> x, y, z = 7, 8, 9
>>> print(x)
7
>>> print(y)
8
>>> print(z)
9
#交换变量a和变量b的值
>>> a = 10
>>> b = 20
>>> a, b = b, a
>>> print(a)
20
>>> print(b)
10

Everyone knows how to use the assignment number, right? Now let ’s increase the difficulty a bit, and look at the self-increasing assignment number and the self-decreasing assignment number.

Self-increasing assignment number: + =

The meaning is to add the variable on the left of the symbol to the value or variable on the right of the symbol , and then assign it to the variable on the left of the symbol .

#赋值号举例4
>>> i = 5
>>> i += 1
>>> print(i)
6

How to understand: Make a transformation of i + = 1 and copy all the contents on the left side of the self-increasing assignment number to the right side. Only the variable name is left on the left, and the self-increasing assignment number becomes the assignment number as follows:

                      i = i + 1

This clearly indicates that the value of the variable i + 1 is assigned to the variable i

The self-increment assignment number is very widely used in loop statements. For example, to output a natural number from 1 to 5, the code is as follows:

#自增赋值号举例:
>>> i = 1
>>> while i<6:
...   print(i)
...   i += 1
运行结果:
1
2
3
4
5

In fact, if you are not used to writing i + = 1 , it is perfectly possible to write i = i +1 . But it is not recommended, because almost no one writes i = i +1 .

Self-decreasing assignment number:-=

The meaning is to subtract the value or variable on the right of the symbol from the variable on the left of the symbol , and then assign it to the variable on the left of the symbol .

#自减赋值号举例
>>> j = 6
>>> k = 5
>>> j -= k
>>> print(j)
1

Understanding method: transform j-= k , copy the contents of the left side of the decrement assignment number to the right side, only retain the variable name on the left, and the decrement assignment number becomes the assignment number, as follows:

                      j = j - k

In fact, the multiplication sign and division sign of the four operations can also be added in front of the assignment number to form "self-multiplication assignment number" and "self-division assignment number". The assignment methods are completely similar, but very few people use them.

 

4.2 Equal sign:

The equal sign (==) is actually one of the comparison operators . There are six common conditional comparison operators:

== (equal sign),! = (Unequal sign),> (greater than sign),> = (greater than or equal sign), <(less than sign), <= (less than or equal sign)

The meaning of the comparison operator is what the name implies. But do you know what 3 == 2 and 3! = 2 mean?

#等号举例1
>>> 3 == 2
False
>>> 2 == 2
True
>>> 3 != 2
True
>>> 2 != 2
False

Python returns a judgment result value of True or False for the result of the comparison operator, so the equal sign (==) is often used in judgment statements:

#等号举例2
>>> a = 5
>>> b = 5
>>> if a==b:
...   print("a等于b")
... else:
...   print("a不等于b")
运行结果:
a等于b

Please note that the if statement is only executed when the execution condition is True. a == b is True, so print out "a is equal to b", and give another example:

#等号举例3
>>> c = 6
>>> d = 8
>>> if c==d:
...   print("c等于d")
... else:
...   print("c不等于d")
运行结果:
c不等于d

 

to sum up:

== (two equal signs ) can generally only appear on the line that judges the if statement to indicate equality, and = (one equal sign) that appears elsewhere is an assignment number, assigning values from right to left .

 

Off topic:

In a few programming languages, = (an equal sign) is equal to meaning, such as the SQL language; in some languages, the equal sign and assignment number are written as = (an equal sign), such as Excel formulas, VB, etc., encountered Please pay attention to it.

 

My CSDN blog column: https://blog.csdn.net/yty_7

Github address: https://github.com/yot777/

If you think this chapter is helpful to you, welcome to follow, comment and like! Github welcomes your Follow and Star!
 

Published 55 original articles · won praise 16 · views 6111

Guess you like

Origin blog.csdn.net/yty_7/article/details/104307249