当前位置:网站首页>Day3 - variables and operators

Day3 - variables and operators

2022-06-26 05:41:00 m0_ sixty-seven million thirty-six thousand three hundred and f

day3 - Variables and operators

Summary of learning :

1、 Variable

1) Definition : A variable is a container that holds data

2) grammar : Variable name = data

​ explain : The variable name is named by the programmer ; Is an identifier. , It can't be a keyword ; When you choose a name, you should see the name and know the meaning , Do not use system function names 、 Class name and module name , The letters are all lowercase , Multiple words are separated by underscores

students_age = 12
print(students_age)

3) Using variables —— Is to use the data stored in the variable ( What the saved data can do , Variables can be done )

x = 3
print(type(3))
print(type(x))

4) Re assign a variable ( When re assigning values to variables, you can assign data of different data types )

age = 18
print(age)
age = '18 year '
print('age') # here 'age' Is string , Single quotes required 

5) Define multiple variables at the same time

Define the same value of multiple variables at the same time : Variable 1, Variable 2, Variable 3…= data

Define different values of multiple variables at the same time : Variable 1, Variable 2, Variable 3… = data 1, data 2, data 3…

( When defining different values of multiple variables, the number of variables must be the same as the number of data )

6)python The principle of defining variables and reassigning values

1、python Defining variables is to apply for memory first , The size of the memory application depends on the size of the data to be saved 
2、 When reassigning , Will reapply for new memory , The size of the new memory depends on the size of the new data 

2、 Operator

1) Numeric operators :+、—、*、/、%、//、**

/ The result must be float

Application scenario of surplus :

1、 Judge whether there is an integer division relationship between two numbers -—— Whether the remainder is zero

2、 Take the lower digits ( For example, take the single digit of a number )—— Yes 10 perhaps 10 Of N Secondary surplus

num = 3
print(num % 2)

to be divisible by ( Seeking quotient , Round to the small ) Application scenarios of :

1、 Take the quotient of an integer if it can be divided

2、 Remove the lower digits of an integer - to be divisible by 10 perhaps 10 Of N The next power ( Generally, it is used in combination with surplus )

# Get the ten digits of a number 
num = 1135
print(num // 10 % 10)  #3
# Get the hundred and ten digits of a number 
num = 11234
print(num // 10 % 100)
print(num % 1000 // 10)

Power operation : Fractions and decimals should be enclosed in parentheses

2) Comparison operator :>、<、>=、<=、!=

All comparison operators run with Boolean results (bool) value

age = 12
print(11 <= age <=13) #Ture

3) Logical operators :and( Logic and )、or( Logic or )、not( Logic is not )

a) Logic and - and

Application scenarios : Equivalent to... In life ‘ also ’, Used to connect conditions that require two to be true at the same time

Operational rules : All two are Ture, The result is Ture, As long as one of them is False, The result is False

# Determine whether a number can be simultaneously 3 and 7 to be divisible by 
num = 339
print(num % 3 == 0 and num % 7 == 0) #False

b) Logic or - or

Application scenarios : If one of two or more conditions is satisfied

Operational rules : Conditions 1 or Conditions 2 - All two are False The result is False, As long as one is Ture Namely Ture

# Judge whether a number can be 3 perhaps 7 to be divisible by 
num = 339
print(num % 3 == 0 or num % 7 == 0) #Ture

c) Logic is not - not

Operational rules :not Conditions - Negate the specified condition

Use scenarios : Deny a condition ; If a condition is written forward, the situation is very complicated , The reverse is really simple , Then let's write the conditions in reverse and add not

not Ture - False
not False - Ture
# Be no older than 18
age = 18
print(not age > 18)
# Number cannot be greater than 14
age = 14
print(not > 14)

4) Assignment operator :=、+=、-=、*=、%=、//=、**=、/=

notes : The function of all assignment operations is to store data in variables

a)=

Variable name = data

b)+=

Variable name += data - Take out the data saved in the variable and assign it to the variable after adding the following data ( Variable names must be defined )

notes : The same is true for other compound operations

c = 1
c -= 2
print(c)
c = 3
c **= 2
print(c) #9
 The priority of the operation character :
 Numeric operators > Comparison operator > Logical operators > Assignment operator 

Data types and Operator jobs

choice question

  1. B
  2. A
  3. A
  4. C、D
  5. A
  6. D

Completion

  1. None
  2. type
  3. Ture
  4. 9
  5. 9;6;2

Programming questions

num = int(input(' Please enter a number :'))

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

# Input 21 Print Ture, Input 24 Print False
x = int(input(' Please enter a number :'))
print((x % 3 == 0 or x % 7 == 0) and (x % 3 != 0 or x % 7 != 0))
# Input  14  Print  True,  Input  4  Print  False,  Input  21  Print  False
year = int(input(' Please enter a year :'))
print((year % 4 == 0 and year % 100 != 0) or year % 400 == 0)
# Input 2020 Print Ture, Input 2011 Print False
student_time = 61
print(student_time // (60**2), ' when ', student_time % 3600 // 60, ' branch ', student_time % 60, ' second ')
# Time  67  second  —> 0  when  1  branch  7  second 
s, t = 1.55, 55
x = t / (s ** 2)
print(18.5 <= x <= 24.9)
 # Enter the weight : 55,  Enter the height :1.55,  Output : True

Short answer
1.
Digital data :int 、float

Text data :str

Boolean value :bool

Null value :NoneType

nothing

原网站

版权声明
本文为[m0_ sixty-seven million thirty-six thousand three hundred and f]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180505254237.html