Day2 Python variables and operators

Python variables and operators

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

name, age, gender ='Xiao Ming', 25, 'male'
print(name, age, gender)
x, y, z = 12, 24, 36
print(x, y, z)

2. Reassign

After the variable is re assigned, the latest value in the variable is used when using the variable

score = 100
print(score)
score = 90
print(score)

How Python defines and re assigns variables
  • 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

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

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

    num = 100
    print(id(num))
    num = 200
    print(id(num))
    

Mathematical operator

+ ,-, *, / ,%, //, **

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

  • The result of the division operation is a floating point number
print(10 + 20)
print(2 - 5)
print(2 * 5)
print(5 / 2)
print(4 / 2)

%- surplus (mold taking)

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

print(10 % 3)
print(15 % 3)
print(9 % 2)

Application 1: judge whether there is an integer division relationship between two numbers - judge whether the remainder of two numbers is 0
print(50 % 2)
print(25 % 5)
print(16 % 3)

Application 2: remove the low digits of integers
  • 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 = 26515
print(num % 10)
print(num % 100)
print(num % 1000)

//- divide (round down)

print(5 / 2)		# 2.5
print(5 // 2)		# 2
print(1.8 // 2)		# 0.0
print(-5 // 2)		# -3
Application scenario: remove the low digits
num = 2342
print(num // 10)
print(num // 100)
print(num // 1000)

Exercise: get the ten digits of any positive integer

num = 9527
print(num // 10 % 10)
print(num %100 // 10)
num_1 = 1098
print(num_1 // 10 % 10)
print(num_1 % 100 // 10)

Gets the hundreds of any positive integer

num = 28293
print(num // 100 % 10)
print(num % 1000 // 100)

**- power operation

x**y - find the Y power of x

print(8 ** 3)
print(1.3 ** 4)
print(9 ** 0.5)
print(8 ** (1/3))

Comparison operator

<, > ,==, != ,<=, >=

The result of all comparison operators is Boolean
print(10 > 20)
print(10 < 20)
print(10 == 20)
print(10 != 20)
print(10 <= 10)
print(10 >= 10)

The comparison operator in Python supports concatenation and represents the range
x = 0.75
print(0 <= x <= 1)

Logical operator

and ,or ,not

1. and
1)Application scenario: it is used when multiple conditions need to be met at the same time, which is equivalent to that in life
2)Operation rule: condition 1 and Condition 2	-	Both True The result is True,As long as one is False The result is                               False
True and False		->True
True and True		->True
False and True		->True
False and False		->False

Exercise: write down the conditions to judge whether a number can be divided by 3 and 7 at the same time

num = 84
print(num % 7 == 0 and num % 3 == 0)
print(num % 21 == 0)

Determines 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
1)Application scenario: used when multiple conditions are required, as long as one condition is satisfied, which is equivalent to or in life
2)Operation rule: condition 1 or Condition 2	-	As long as one of the two conditions is True The result is True,Both False                               The result is when False

Exercise 1: determine whether a number can be divided by 5 or 7

num = 125
print(num % 5 == 0 or num % 7 == 0)

Exercise 2: determine whether the specified year is a leap year

year = 2009
print((year % 4 == 0 and year % 100 != 0) or year % 400 == 0)

3. not
1)Application scenario: (negate a condition) if there are many complex forward write conditions, but the reverse is very simple, the condition			  Just write it backwards and add it not
2)Operation rules: not condition	-	Negate condition
not True	-	False
not False	-	True

Exercise: determine whether a number cannot be divided by 3 or 7

num = 90
#Conditional forward write
print((num % 7 == 0 and num % 3 != 0) or (num % 7 != 0 and num % 3 == 0) or (num % 7 != 0 and num % != 3))
#Conditional reverse write
print(not (num % 7 == 0 and num % 3 == 0))

Logical operator extension

1. Short circuit operation
Condition 1 and Condition 2
and Short circuit: if condition 1 is False,Then the code corresponding to condition 2 will not be executed
 Condition 1 or Condition 2
or Short circuit: if condition 1 is True,Then the code corresponding to condition 2 will not be executed
2. General operation rules of logical operators
1)and
 Data 1 and Data 2
2)or
 Data 1 or Data 2
print(7 and 8)
print(7 or 8)
print(not 7)
print(not 0)

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

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

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

=: variable = Data

a = 10
a = 100
print(a)

+= ,-= ,*=, /= ,%=, //= ,**=

Variable + = Data - add the data saved in the variable and the data after + = and reassign the operation result to the previous variable

Note: for compound assignment operators, the preceding variables must be assigned variables
Emphasis: all assignment operations have no results
a += 10
print(a)	# 110
a -= 20
print(a)	# 90
a %= 4
print(a)	#2
a **= 3
print(a)

Operator precedence

Mathematical operators > comparison operators > logical operators > assignment operators (lowest)

In mathematical operators: * * > * > / > / / > >% > + >-

(those in brackets shall be counted first)

result = 100 + 2 * 3 ** 2 > 20 * 10 - 3
print(result)

If Boolean data is involved in mathematical operations, False is 0 and True is 1

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

Tags: Python Pycharm

Posted on Fri, 17 Sep 2021 15:07:38 -0400 by Dargrotek