if、elif、else用法。
多个if语句之间条件平等,互不影响,只要满足条件的都会输出。如
score=40 #输出结果为:未达到优秀,未及格
if score<80:
print(“未达到优秀。”)
if score<60:
print(“未及格”)
if,elif,else属于对立关系,按照上下顺序执行,当走了某个分支之后,即使其他分支条件满足,也不再走其他分支。如下:
score=40 #输出结果为:未达到优秀。
if score<80:
print(“未达到优秀。”)
elif score<60:
print(“未及格”)
elif score<50:
print(“不发奖状”)
else:
print(“暑假补课”)
当一段代码中含有多个if时,elif和else的判断取决于与它同级最近的if语句。如:、
score=40 #输出结果为:未达到优秀。不发奖状
if score<80:
print(“未达到优秀。”)
if score>60:
print(“及格”)
elif score<50:
print(“不发奖状”)
else:
print(“暑假补课”)
while循环
循环三要素:
(1)初始值。(2)循环条件。(3)步长。
i=1 #初始值
while i<=3: #循环条件
print(“吃瓜群众。”) #核心代码
i+=1 #步长
死循环:
while True 或 while 1.
python对象的基本特征:分别为id(身份标识),type(数据类型),value(值)。
“==”是对两个对象之间的value(值)和type(类型)进行比较如:
c=[1,2,3,4]
d=[1,2,3,4]
print(c==d) #True
“is” 是对id、type、和value都进行比较
c=[1,2,3,4]
d=[1,2,3,4]
print(c is d) #False3
python数据类型。(重点)
#在python中有6大基本数据类型,分别为:数字(number),字符串(str)、列表(list)、集合(set)、元组(tuple)、字典(dict)。
可变类型:list、set、dict
不可变类型:str、number、tuple