当前位置:网站首页>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
- B
- A
- A
- C、D
- A
- D
Completion
- None
- type
- Ture
- 9
- 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
边栏推荐
- Serious hazard warning! Log4j execution vulnerability is exposed!
- A new explanation of tcp/ip five layer protocol model
- DOM文档
- Ribbon负载均衡服务调用
- C XX management system
- [arm] build boa based embedded web server on nuc977
- Describe an experiment of Kali ARP in LAN
- Introduction to alluxio
- pytorch(网络模型)
- uni-app吸顶固定样式
猜你喜欢
Serious hazard warning! Log4j execution vulnerability is exposed!
pytorch(环境、tensorboard、transforms、torchvision、dataloader)
【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...
How does P2P technology reduce the bandwidth of live video by 75%?
小小面试题之GET和POST的区别
Fedora alicloud source
【C语言】深度剖析数据在内存中的存储
适配器模式
怎么把平板作为电脑的第二扩展屏幕
Sofa weekly | open source person - Yu Yu, QA this week, contributor this week
随机推荐
Command line interface of alluxio
国务院发文,完善身份认证、电子印章等应用,加强数字政府建设
cross entropy loss = log softmax + nll loss
June 3 is a happy day
LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree
[C language] deep analysis of data storage in memory
cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching
Uni app ceiling fixed style
5分钟包你学会正则表达式
电机专用MCU芯片LCM32F037系列内容介绍
状态模式,身随心变
redis探索之布隆过滤器
Thinking about bad money expelling good money
ZigBee learning in simple terms lesson 3 external interruption
cartographer_ optimization_ problem_ 2d
Project suspension
【MYSQL】MySQL 百万级数据量分页查询方法及其优化
uni-app吸顶固定样式
Installation and deployment of alluxio
Something about MariaDB