Introduction to Python zero Basics - 17 - assignment operators and comparison operators in Python

Posted by the apprentice webmaster on Sat, 26 Feb 2022 00:06:15 +0100

Python assignment operator

What are the assignment operators?

operatordescribeExamples
=assignmentc = a+b
-=Subtraction assignmentc -= a -> c = c - a
=+Additive assignmentc += a -> c = c + a
*=Multiplicative assignmentc *= a -> c = c * a
/=division assignment c /= a ->c=c / a
%=Modular assignmentc %= a -> c= c % a
**=Power assignmentc **= a ->c ** a
//=Division operationc //= a -> c // a

Here is a simple example:

n1 = 100
f1 = 25.5
n1 -= 80  #Equivalent to n1=n1-80
f1 *= n1 - 10 #Equivalent to F1 = F1 * (N1 - 10)
print("n1=%d" % n1)
print("f1=%.2f" % f1)

# >>>The operation results are as follows:
# >>> n1=20
# >>> f1=255.00

Generally, as long as the extended assignment operator can be used, it is recommended to use this assignment operator.

However, please note that this assignment operator can only assign values to existing variables, because the variable itself needs to participate in the operation in the assignment process. If the variable is not defined in advance, its value is unknown and cannot participate in the operation. For example, the following is wrong:

n += 10

This expression is equivalent to n = n + 10. N is not defined in advance, so it cannot participate in addition operation.

Assignment of different data types in Python

Single variable assignment single object

a = 1
b = 1.0
c = "character string"
d = [1, 2, 3, 4]
e = (1, 2, 3, 4)
f = {1, 2, 3, 4}
g = {1: 1, 2: 2}

Multiple variables are assigned to multiple objects at the same time

python features can assign values to multiple variables in one line of code at the same time

# Multivariable
a, b = 1, 2
print(a, b)

a, b, c, d = 1, 2.0, True, "character string"
print(a, b, c, d)

e, f, g, h = [1, 2, 3, 4], (1, 2, 3, 4), {1, 2, 3, 4}, {1: 1, 2: 2}
print(e, f, g, h)


# Output results
1 2
1 2.0 True character string
[1, 2, 3, 4] (1, 2, 3, 4) {1, 2, 3, 4} {1: 1, 2: 2}

Assign multiple objects to a single variable

This is the feature of Python tuple. When a single variable uses =, the tuple can be on the right without adding (), which is also called tuple packaging

# Single variable
a = 1, True, "character string"
print(a, type(a))


# Output results
(1, True, 'character string') <class 'tuple'>

Multiple variables are assigned to a single sequence object

  • This is also called sequence unpacking, because the = right side of unpacking operation can be any sequence
  • Sequence unpacking requires that the number of variables on the left of the equal sign is the same as the number of elements in the sequence on the right
a, b, c = (1, 2, 3)
print(a, b, c)

a, b, c = [1, 2, 3]
print(a, b, c)

a, b, c = {1, 2, 3}
print(a, b, c)


# Output results
1 2 3
1 2 3
1 2 3

summary

Multiple assignment is actually a combination of tuple packing and sequence unpacking

Python comparison operator

What are the comparison operators?

Comparison operatorexplain
>Greater than, returns True if the value before > is greater than the value after it, otherwise returns False.
<Less than, returns True if the value before < is less than the value after < otherwise returns False.
==Equal to. If the values on both sides of = = are equal, it returns True; otherwise, it returns False.
>=Greater than or equal to (equivalent to ≥ in Mathematics). If the value in front of > = is greater than or equal to the following value, it returns True; otherwise, it returns False.
<=Less than or equal to (equivalent to ≤) in mathematics. If the value before < = is less than or equal to the value after it, it returns True; otherwise, it returns False.
!=Not equal to (equivalent to ≠ in Mathematics), if= If the values on both sides are not equal, return True; otherwise, return False.
<>Not equal to (abandoned in Python 3, effective in Python 2)
isJudge whether the objects referenced by two variables are the same. If they are the same, return True; otherwise, return False.
is notJudge whether the objects referenced by two variables are different. If they are different, return True; otherwise, return False.

Examples of Python comparison operators:

print("89 Greater than 100:", 89 > 100)
print("24*5 Whether it is greater than or equal to 76:", 24*5 >= 76)
print("86.5 Is it equal to 86.5: ", 86.5 == 86.5)
print("34 Is it equal to 35.0: ", 34 != 35.0)
print("False Less than True: ", False < True)
print("True Is equal to True: ", True < True)

Operation results:

89 Greater than 100: False
24*5 Whether it is greater than or equal to 76: True
86.5 Is it equal to 86.5:  True
34 Is it equal to 35.0:  True
False Less than True:  True
True Is equal to True:  False

==The difference between is and is

As a beginner of Python, you may be unfamiliar with is. Many people will mistakenly confuse it with the function of = =, but in fact, is is is essentially different from = = and is not the same thing at all.

==It is used to compare whether the values of two variables are equal, while is is used to compare whether the two variables refer to the same object, for example:

import time  #Introducing time module

t1 = time.gmtime() # gmtime() is used to get the current time
t2 =  time.gmtime()

print(t1 == t2) #Output True
print(t1 is t2) #Output False

Operation results:

True
False

The gmtime() method of the time module is used to obtain the current system time, accurate to seconds. Because the program runs very fast, t1 and t1 get the same time== It is used to judge whether the values of t1 and t2 are equal, so it returns True.

Although the values of t1 and t2 are equal, they are two different objects (each call to gmtime() returns a different object), so t1 is t2 returns False. It's like two twin sisters. Although their appearance is the same, they are two people.

So, how to judge whether two objects are the same? The answer is to determine the memory addresses of the two objects. If the memory addresses are the same, it means that the two objects use the same block of memory, of course, the same object; It's like two names use the same body, of course, the same person.

Topics: Python Back-end