Operators and branches

Operators and branches

Principles for defining and assigning variables

  • When defining variables in python, you need to apply for memory first. The size of the memory application depends on the size of the data, and then save the data in memory to associate with the variables.
  • When you re assign a value to a variable, you will re apply for new memory. The new memory size is determined by the size of the new data.
a = 10
print(id(a)) # 2348561558096 ID (variable) get data address
a = 100
print(id(a)) # 2063869367760

Mathematical operator

  • +,-,*,/,%,//,**.
  • Addition, subtraction, multiplication and division in python is the same as addition, subtraction, multiplication and division in mathematics.
print(1 + 1) # 2
print(1 - 1) # 0
print(1 * 1) # 1
print(1 / 1) # 1
  • There are floating-point numbers in the object of addition, subtraction and multiplication, and the result is a floating-point number. If it is an integer, the result is an integer.
  • In division, the result must be a floating point number.
  • %(remainder), x%y is the remainder of x divided by y to judge whether a number can be divided by another number. It depends on whether the remainder of the two numbers is 0. It can also take the lower digits of an integer (the remainder to the power of 10 or 10).
print(10 % 3) # 1
print(4 % 2)  # 0
  • //(rounding), quotient rounding to small. You can remove the low digits.
print(3.2 // 2) # 1.0
print(-3 // 2)  # -2
print(121 // 10)  # 12
  • **Power operation
print(1 ** 2) # 1
print(16 ** 0.5) # 4.0

Comparison operator

  • <,>,<=,>=,==,!=, The results are bool.
print(10 < 1)  # False
print(1 == 1)  # True
print(5 >= 5)  # True
x=10
print(0 <= x <= 10)  # True to judge whether x is between 0 and 10

Logical operator

  • Logical and or not operations: and, or, not.
  • And application scenario: used when multiple conditions need to be established at the same time, which is equivalent to and.
  • Operation rules: condition 1and condition 2. If both results are True, the result is True. As long as one of them is False, the result is False.
num = 99
print(99 % 3 == 0 and 99 % 5 == 0)  # Can False be divided by three and five
  • Or logic or operation: used when multiple conditions are required, as long as one of them is satisfied, which is equivalent to or.
  • Operation rules: condition 1or condition 2. If one of the two conditions is True, the result is True, and if both conditions are False, the result is False.
v = 8
print(type(v) == int or type(v) == float)  # True to judge whether v is digital data
# Winning rule: odd numbers with ten digits of 0 or numbers with single digits plus two digits
  • not logical non operation: negating a condition. When the forward writing of a condition is very complex and the reverse writing is very simple, the condition is written backwards.
  • Operation rules: not conditions
age = 20
print('18 or more', age >= 18)
print('Not greater than or equal to 18', not age >=18)
print('Equal to 20', age == 20)
print('Not equal to 20', not age == 20)

Assignment Operators

  • Assignment operators: =, + =, - =, * =, / =,% =, / / =, * * =.
  • The function of all assignment operators is to store data directly or indirectly in a variable without producing any results. Variable assignment operator data.
  • The assignment expression has no result.
a = 1
print(a) # 1
  • Compound assignment operators: + =, - =, * =, / / =, * * =,% =.
  • Variable + = data, take out the data in the variable and operate with the subsequent data, and assign the operation result to the previous variable. The variable must be an assigned variable.
b = 10
b += 2
print(b)  # 12

Operator precedence

  • Mathematical operators > comparison operators > logical operators > assignment operators.
  • In mathematical operators: * * > *, /,% > +, -.
  • If there are brackets, count them first ().
a = 10 + 1
print(a)  # 11

Process control

  • Process control, program execution mode.
  • Sequential structure, the code is executed from top to bottom, and each statement is executed once.
  • Branch structure, select whether to perform or not to perform relevant operations according to conditions.
  • Loop structure to make the code execute repeatedly.

Branching structure

  • If statement, an if single branch structure, performs an operation when the conditions are met. If the conditions are not met, the operation will not be performed.
if Conditional statement:
    Code snippet   # grammar
  • if -- keyword; Fixed writing

    Conditional statement - any expression with results, such as specific data, variables storing data, and operation expressions (assignment is not allowed)

    : -- Fixed writing.

    Code segment -- structurally, it is to maintain an indent or multiple statements with if; Code snippets are code that is executed only when conditions are met.

a = 10
if a > 100:
    print('Abnormal score')
print('1')  # 1
  • Execution process: first judge whether the conditional statement is True. If so, execute the code segment, and then execute the following code. If False, execute the following code without executing the code segment. If the result is not bool, convert it to bool and then look. (when other data is converted to bool, it is True except for zero value and null value)
print(bool('False'))  # True
  • If double branch structure, execute an operation when the condition is met, and execute another operation when the condition is not met, if else.
if Conditional statement:
    Code snippet 1
else: 
	Code snippet 2
year = 8
if year >= 18:
    print('adult')
else:
    print('under age')  # adult
num = 78
if num % 2 == 0:
    print('even')
else:
    print('odd')  # even
  • if multi branch structure, do different things according to different conditions.
score = 98
if score >= 90:
    print('A')
elif 80 <= score < 90:
    print('B')
elif 70 <= score < 80:
    print('C')
else:
    print('D')  # A
  • The latter condition is judged when the previous condition is not true. Only when all conditions are not true will the code in else be executed.
age = 88
if age >= 60:
    print('old age')
elif age >= 40:
    print('middle age')
elif age >= 28:
    print('Prime of life')
elif age >= 20:
    print('youth')
elif age >= 12:
    print('juvenile')
else:
    print('children') # old age

Keywords: Python

Added by wazo00 on Wed, 01 Dec 2021 20:18:34 +0200