September 15 knowledge sorting

September 15 knowledge sorting

1, Variable

1. Define multiple variables at the same time

1) Define the same value of multiple variables at the same time:

Variable name 1 = variable name 2 = variable name 3 =... = Data

Example:

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

(the number of variables and data should be consistent)

name, age ,gender = 'GM', 30, 'male'
print(name, age, gender)

2. Variable re assignment

After re assigning a value to the variable, the variable uses the latest value in the variable

score = 100
print(score)
score = 98
print(score)

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

Computer memory unit:

Minimum unit: bit

Unit from small to large: bit byte kb Mb G T

1byte = 8bit 1kb = 1024byte 1Mb = 1024kb 1G = 1024Mb 1T = 1024G

When defining variables, python will apply for memory to save data. The size of the memory application depends on the size of the saved data.

The memory applied for the last data in c language is easy to overflow and waste memory. The advantage is that it runs faster without re applying for memory.

When re assigning a value to a variable, it will re apply for new memory. The size of the new memory is determined by the new data size, and then the variable will be associated with the new memory, and the originally retrieved memory will be automatically regressed and destroyed

id (variable) -- get the memory address corresponding to the variable

num  =100
print(id(num))
num = 95
print(id(num))

2, Mathematical operator

1. What are the mathematical operators

Mathematical operators include:

+: plus sign, adding two numbers

-: minus sign, subtracting two numbers

*: multiplier sign, multiplying two numbers

/: division sign: divide two numbers

%: percentage sign. The function is to take the remainder (divide and take the remainder)

//: double slash, the function is integer division

**: double star, function is power function

2. Functions of mathematical operators

1) add, subtract, multiply and divide add, subtract, multiply and divide functions as like as two peas in +, -, *, and / or mathematics.

Whether the results of addition, subtraction and multiplication have floating-point numbers depends on whether the operation object is an integer or decimal

print(10+20)
print(2-5)
print(2*5)

The result of the division operation is a floating-point number, and the number obtained by dividing 4 by 2 is 2.0

print(5/2)
print(4/2)

2) Remainder:%

Function: remainder (modulus) (remainder in mathematical function)

x% y ------------ find the remainder of x divided by y

Divisor = divisor x quotient + remainder

print(10%3)
print(15%3)

%Application scenario 1: judge whether there is an integer division relationship between two numbers -- judge whether the remainder of two numbers is zero

print(10 % 2)

%Application scenario 2:

Take the lower digits of an integer:

The units obtained from 10 are single digits, the units obtained from 100 are the last two digits, the units obtained from 1000 are the last three digits, and so on

num = 2346
print(num % 10)
print(num % 100)

3) Division://

The quotient is rounded to the small, and the negative number is also rounded to the small number

(judging the division relationship is only related to the remainder)

print(5/2)   #2.5
print(5//2) #2 and quotient adjacent integers 2 and 3 are smaller, so take 2
print(1.8//2) 0 # 0.9 1 and 0 0 take 0
print(-5//2) #-3 - 2.5 - 3 and - 2 take the smaller as - 3

Application scenario of integer division: remove the low digits

Remove the single digit to divide by 10, remove the last two digits to divide by 100, remove the last three digits to divide by 1000, and so on

num = 2342
print(num // 10)
print( num // 100)
print( num // 1000)

practice:

# Exercise: get the ten digits of any positive integer
# num1 = int(input('Please enter a number '))
# print(num1 % 100 // 10)
# print(num1 //10 %10)

# practice; Gets the hundreds of any positive integer
# num2 = int(input('Please enter a number '))
# print(num2 % 1000 // 100)
# print(num2 //100 %10)
#
# num3 = int(input('Please enter a number '))
# print(num3 % 10000 // 1000)
# print(num3 //1000 %10)

4) Power operation:**

x ** y -- find the Y power of x

print(8**3)
print(1.3**4)

When the computer stores floating-point numbers, it will store approximate numbers, which are not accurate. It is sufficient to see that the floating-point number is approximately equal to the value (the reason for the storage of the computer itself)

You can calculate the square through this

print(8**(1/3))

3, Comparison operator

Comparison operators include: >, <, = =,! =, > =<=

The comparison operator determines whether there is a relationship between two data

The operation results of all comparison operators are basically Boolean values

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

python's comparison operator supports concatenation representation range

x = 0.5
print(0 <= x <= 1)

Only python can write this.

4, Logical operator

Logical operators include:

And (logical and operation), or (logical or operation), not (logical non operation)

1.and (logic and operation)

1) Application scenario

It is 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

If both are True, the result is True. As long as one condition is not True, the whole result is False

True and True ->True
False and True ->False
True and False ->False
False and False ->False

practice:

#Exercise: write down the conditions to judge whether a number can be divided by 3 and 7 at the same time
num = 67
a = num % 3
b = num % 7
print(a == 0 and b == 0)
print(num % 21 == 0)#If you divide by 3 and 7 at the same time, you can also divide by 21
# Exercise: determine whether the specified number is an even number divisible by 3
num= 6
print(num % 3 == 0 and num % 2 == 0 )
print(num % 6 == 0)

2.or (logic or operation)

1) Application scenario

It is used when multiple conditions are required. As long as one condition is satisfied, it is equivalent to or in life

2) Operation rules: condition 1 or condition 2

If one of the conditions is True, the result is True. If both conditions are False, the result is False

True or False ->True
True or True ->True
False or True ->True
False or False ->False

practice:

# Exercise 1: determine whether a specified number can be divided by 5 or 7
num = int(input('Please enter a number:'))
print(num % 5 == 0 or num % 7 == 0)

#Exercise 2: determine whether the specified year is a leap year
year = int(input('Please enter the year'))
print(year % 4 == 0 and year % 100 != 0 or year % 400 == 0 )

3.end (logical non operation)

1) Application scenario

Negating a condition,

In general, if the forward writing condition is very complex, but the reverse writing condition is very simple, add not before the condition

2) Rule: not followed by condition

not True  -> False
not False ->True

practice:

num1 = 90
print(not num1 > 10)
print(num1 <= 10)

Logical operator exercise:

# Exercise: judge the condition that a number cannot be divided by 3 and 7 at the same time
# Case 1: it can be divided by 3 but not by 7
# Case 2: it can be divided by 7, but not by 3
# Case 3: neither divisible by 7 nor divisible by 3
# Conditional reverse write (can be divided by 3 and 7 at the same time): num% 21 = = 0
# Cannot be divided by 3 and 7 at the same time: not num% 21 = = 0
print(not num % 21 == 0)

4. Logical operator expansion

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
Short circuit of or: if condition 1 is True, condition 2 does not execute the due code

2) General operation rules of logical operators

Data 1 and data 2 -- > data 2 - if there is a short circuit, it is the first one. If there is no short circuit, it is data 2
Data 1 or data2 -- > data1

print(True and 100) #100
print(False and 100) #False
print(True or 100)  #True
print(False or 100)  #100

print(7 and 8)  #8
print (7 or 8) #7

Important conclusion: all python data can be converted from Boolean values to Boolean values

If the data is a 0 value and a null value (string, empty string and None and the number 0) are converted to a Boolean value of False, the other data is True

print(bool(0), bool(0.0), bool(''), bool(None))
print(bool(0.34), bool(8), bool(' '), bool(-2.34),bool('abc'), bool(-2), bool('1'))

The result of not must be a Boolean value

5, Assignment operator

1. Assignment operators: =, + =, - =, * =,% =, / =, / / =**=

1) The function of assignment operator is not to calculate a result, but to save data into variables

2) The left side of all assignment operators must be variables

2. = equal sign: function assignment symbol. The data on the right is assigned to the variable on the left

a= 100

3.+=, -=, *=, %=, /=, //=, **=

Variable + = data -- take out the variable data and add the data after the equal sign, and the obtained result is re assigned to the variable

Other compound operator rules are the same as + =

The variable must have been assigned before the compound operator can be assigned

Emphasis: all assignment operations have no results

print(a=0) will report an error - the assignment operation has no result and cannot be output

a += 10
print(a)
a -= 20
print(a)

6, Operator priority

1. Priority order of four operators

Mathematical operators > comparison operators > logical operators > assignment operators

2. In mathematical operators:

**> *, /, / /,% (same level in order) > +-

3. If there are brackets, the one in the brackets shall be counted first (here brackets refer to parentheses' () ')

result = 100 + 2 * 3 ** 2 > 20 * 10 - 3

If Boolean values are encountered in mathematical operations, False can be regarded as 0 and True as 1

result = 100 + (10 > 20)
result = 100 + True
print(result)

height = float(input('Please enter your height'))
weight = int(input('Please enter your weight'))
target = weight / height ** 2
print(18.5 <= target <= 24.9)

Keywords: Python R Language AI

Added by macpaul on Sun, 19 Sep 2021 04:10:00 +0300