Python basics lesson 4 notes and homework

Operator

Arithmetic Operator

Operator description
+ Add-Add two objects
- Subtract-get a negative number or subtract one number from another
* Multiply-Multiply two numbers or return a string repeated several times
/ Divide-x divided by y
% Modulo-returns the remainder of the division
** Power-returns x to the power of y
// Divide-round down to the nearest quotient

Assignment operator

Operator description
= The simple assignment operator c = a + b assigns the result of a + b to c
+= The addition assignment operator c += a is equivalent to c = c + a
-= The subtraction assignment operator c -= a is equivalent to c = c-a
*= The multiplication assignment operator c *= a is equivalent to c = c * a
/= The division assignment operator c /= a is equivalent to c = c / a
%= The modulus assignment operator c %= a is equivalent to c = c% a
**= The power assignment operator c **= a is equivalent to c = c ** a
//= The integer division assignment operator c //= a is equivalent to c = c // a
:= Walrus operator, which can assign values ​​to variables inside expressions. New operators in Python3.8

Comparison operator

Operator description
== Equal to-compare whether the objects are equal
!= Not Equal-Compare whether two objects are not equal (a != b) returns True.
> Greater than-Returns whether x is greater than y (a> b) returns False.
< Less than-Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. This is equivalent to the special variables True and False respectively. Note the capitalization of these variable names. (a <b) Return True.
>= Greater than or equal to-returns whether x is greater than or equal to y. (a >= b) returns False.
<= Less than or equal to-returns whether x is less than or equal to y. (a <= b) returns True.

Logical Operators

Python language supports logical operators. The following assumes that the variable a is 10 and b is 20:

Operator Logical expression description
and x and y Boolean "and"-If x is False, x and y return the value of x, otherwise return the calculated value of y. (a and b) Return 20.
or x or y Boolean "or"-If x is True, it returns the value of x, otherwise it returns the calculated value of y. (a or b) returns 10.
not not x Boolean "not"-If x is True, return False. If x is False, it returns True. not(a and b) returns False

Conditional operator

Expression 1 if statement else expression 2

a = 10
b = 20
c = 'a 大' if a > b else 'b 大'
print(c)

operation

Realize 521 xxx in 4 ways of formatting strings. Will you marry me?

n = 'XXX'
a = '521 XXX 嫁给我好吗?'
b = '%d %s 嫁给我好吗?' % (521, n)
c = '521 {} 嫁给我好吗?'.format(n)
d = f'521 {n} 嫁给我好吗?'
print(a)
print(b)
print(c)
print(d)

Now there are three variables, abc, and three values ​​are stored in the three variables. Please use the conditional operator to get the maximum of the three values

a = 3
b = 9
c = 5
d = a if a > b else b
d = d if d > c else c
print(d)

Guess you like

Origin blog.csdn.net/chaziliao2/article/details/112977011