Learning summary on September 15

Learning summary on September 15

1, Variable

1. Define multiple variables at the same time

  1. Define multiple variables at the same time and assign the same value: variable name 1 = variable name 2 = variable name 3 =... = data.

    x = y = z = 0
    
    print(x, y, z)
    
  2. Define multiple variables at the same time and assign different values: variable name 1, variable name 2, variable name 3,... = data 1, data 2, data 3.

    ❗ Note: the number of assigned values must be consistent with the number of variables.

    name, age, gender = 'Xiao Ming', 30, 'male'
    
    print(name, age, gender)
    

2. Variable re assignment

  1. After the variable is re assigned, the latest value in the variable is used again.

    score = 100
    print(score)
    
    score = 95
    print(score)
    
    Operation result: 95
    

3. The principle of Python defining variables and re assigning values to variables

  1. Python will apply for memory to save data when defining variables. The size of the memory application depends on the size of the saved data.

2. When re assigning a value to a variable, a new memory will be applied. The size of the new memory is determined by the new data size. Then the variable will be associated with the new memory, and the original memory will be destroyed automatically.

3. Get the memory address corresponding to the variable: ID (variable)

num = 100
print(id(num))

num = 1000
print(id(num))

2, Mathematical operators: +, -, *, /,%, / /**

Functions as like as two peas in 1. +, -, *, and / or mathematics are similar to those in mathematics, x, and.

❗ Note: the result type of division operation is floating point number.

print(10 + 20)     #30
print(2 - 5)      # -3
print(2 * 5)      # 10
print(5 / 2)      # 2.5
print(4 / 2)      # 2.0

2.% - remainder (mold)

# x% Y - find the remainder of x with y
# Divisor ÷ divisor = quotient... Remainder
# Divisor = divisor x quotient + remainder

print(10 % 3)     # 1
# Application 1: judge whether there is an integer division relationship between two numbers - judge whether the remainder of two numbers is 0
print(10 % 2)     # 0
print(25 % 5)     # 0
# Application 2: take the low digits of an integer
# An integer takes the remainder of 10 and can obtain the single digits of this number
# An integer can obtain the last two digits of this number by rounding 100
# An integer can obtain the last three digits of this number by taking the remainder of 1000

num = 2346
print(num % 10)   #6
print(789 % 10)   #9
print(num % 100)  #46

3. / / - division (quotient rounded to the small)

print(5 // 2) # 2 out of 2.5
print(1/8 // 2) # 0.9 takes 0
print(-5 // 2) # - 2.5 take - 3
# Application: remove low digits
num = 2342
print(num // 10)  # 234
print(num // 100) # 23
print(num // 1000) # 2
# Exercise 1: get the tens of any positive integer
num = 5678
print(num // 10 % 10)      # 7
print(num % 100 // 10)     # 7
# Exercise 2: get the hundreds of any positive integer
num = 28293
print(num // 100 % 10)     # 2
print(num % 1000 // 100)   # 2

4. * * - power operation

# x ** y - find the Y power of x
print(2 ** 2)        # 4
print(9 ** (1/2))      # 3.0
print(8 ** (1/3))      # 2.0

3, Comparison operator

❗ ⒋ conclusion: the operation results of all comparison operators are Boolean values.

print(10 > 20)
print(10 < 20)
print(10 == 20)
print(10 != 20)
print(10 >= 10)
print(10 <= 10)

1.Python's comparison operator supports concatenation representation range

x = 0.5

print(0 <= x <= 1

4, Logical operators: and, or, not

1. and (logic and)

  1. Application scenario: used when multiple conditions need to be met at the same time, which is equivalent to and in life.

  2. Operation rules: condition 1 and condition 2 - both are True, and the result is True; One of them is False, and the result is False.

    # Exercise 1: write down the conditions for judging whether a number can be divided by 3 and 7
    num = 22
    print('Whether it can be divided by 3 and 7 at the same time:', num % 3 == 0 and num % 7 == 0)
    print('Whether it can be divided by 3 and 7 at the same time:', num % 21 == 0)
    # Exercise 2: an even number that determines whether a specified number can be divided by 3
    num = 9
    print('An even number that can be divided by 3:', num % 3 == 0 and num % 2 == 0)
    print('An even number that can be divided by 3:', num % 6 == 0)
    

2. or (logical or)

  1. Application scenario: it is used for multiple conditions as long as one condition is satisfied, which is equivalent to or in life

  2. Operation rules: condition 1 or condition 2 - if one of the two conditions is True, the result is True; When both are False, the result is False.

    # Exercise 1: determine whether a specified number can be divided by 5 or 7
    num = 5
    print('Can it be divided by 5 or 7:', num % 5 == 0 or num % 7 == 0)
    # Exercise 2: determine whether the specified year is a leap year
    # Normal leap year - a year that can be divided by 4 but cannot be divided by 100
    # Century leap year - a year divisible by 400
    yr = 2008
    print('Is the specified year a leap year:', (yr % 4 == 0 and yr % 100 != 0) or (yr % 400 == 0))
    

3. not (logical)

  1. Application scenario: (negate a condition) if the forward write condition is very complex, but the reverse write condition is very simple, the condition is written backward and not is added.
  2. Rule: not condition
  • not True -> False
  • not False -> True
# Exercise: write down the conditions under which a number cannot be divided by 3 and 7 at the same time
# 1) It can be divided by 3 but not by 7
# 2) It can be divided by 7 but not by 3
# 3) It cannot be divided by 3 or 7

# Conditional reverse write:
num = 21
print('Judge that a number cannot be divided by 3 and 7 at the same time:', not num % 21 == 0)

5, Logical operator extension

1. Short circuit operation
Condition 1 and condition 2
Short circuit of and: if condition 1 is False, the code corresponding to condition 2 will not be executed.

Condition 1 or condition 2
or: if condition 1 is True, the code corresponding to condition 2 will not be executed.

2. General operation rules of logical operators
❗ Note: the operation result is either data 1 or data 2.

and
Data 1 and data 2:

No short circuit, data 2
Short circuit, data 1
or
Data 1 or data2:

No short circuit, data 2

Short circuit, data 1

print(7 and 8)  # 8
print(7 or 8)   # 7
print(not 7)    # False
print(not 0)    # True

❗ ️ ❗ ️ ❗ Important conclusion: any type of data in Python can be converted to Boolean (all have Boolean values), where zero or null values are False and others are True

6, Assignment operator

  1. Assignment operators: =, + =, - =, * =, / =,% =, / / =**=
    The assignment operator is not used to calculate a result, but to save the data to a variable
    The left side of all assignment operators must be variables

  2. =
    x = 10
    y = 100

  3. +=,-=,*=,/=,%=,//=,**=
    Syntax: variable + = Data

Note: add the data saved in the variable and the data after + = and re assign the operation result to the variable.
❗ Note: the variable in front of the compound assignment operator must be a variable that has been assigned.

a = 10
a += 20
print(a)    # 30

b = 20
b -= 10
print(b)   # 10

❗ ️ ❗ ️ ❗ Emphasis: all assignment operations have no results.

7, Operator priority

  1. Operator priority: mathematical operator > comparison operator > logical operator > assignment operator (lowest).

  2. In mathematical operators: * * > *, / / /,% > +-

  3. If there are brackets, calculate the - () in the brackets first

  4. If Boolean data is involved in mathematical operations, False is regarded as 0 and True as 1.

    result = 100 + True
    print(result)      # 101
    

Tags: Python AI linear algebra

Posted on Sat, 16 Oct 2021 01:16:33 -0400 by gin