语法1:
if 条件:
code1
code2
code3
....
age=180
height=163
weight=75
sex='female'
is_beautiful=True
if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
print('表白。。。')
print('=====>other code')
语法2:
if 条件:
code1
code2
code3
....
else:
code1
code2
code3
....
age=180
height=163
weight=75
sex='female'
is_beautiful=True
if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
print('表白。。。')
else:
print('阿姨好')
语法3:多分枝
强调:if的多分枝=但凡有一个条件成立,就不会再往下判断其他条件了
if 条件1:
code1
code2
code3
....
elif 条件2:
code1
code2
code3
....
elif 条件3:
code1
code2
code3
....
........
else:
code1
code2
code3
....
练习
如果:成绩>=90,那么:优秀
如果成绩>=80且<90,那么:良好
如果成绩>=70且<80,那么:普通
其他情况:很差
score = input('>>: ')
score=int(score)
if score >= 90:
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差')
语法4:if嵌套
age=18
height=163
weight=75
sex='female'
is_beautiful=True
is_successfull=True
if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
print('表白。。。')
if is_successfull:
print('在一起')
else:
print('我逗你玩呢')
else:
print('阿姨好')
print('other code....')
如果:今天是Monday,那么:上班
如果:今天是Tuesday,那么:上班
如果:今天是Wednesday,那么:上班
如果:今天是Thursday,那么:上班
如果:今天是Friday,那么:上班
如果:今天是Saturday,那么:出去浪
如果:今天是Sunday,那么:出去浪
today=input('>>: ')
if today == 'Monday':
print('上班')
elif today == 'Tuesday':
print('上班')
elif today == 'Wednesday':
print('上班')
elif today == 'Thursday':
print('上班')
elif today == 'Friday':
print('上班')
elif today == 'Saturday':
print('出去浪')
elif today == 'Sunday':
print('出去浪')
else:
print('''必须输入其中一种:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
''')
today=input('>>: ')
if today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' or today == 'Thursday' or today == 'Friday':
print('上班')
elif today == 'Saturday' or today == 'Sunday':
print('出去浪')
else:
print('''必须输入其中一种:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
''')
1什么是循环
循环就是一个重复的过程
2为何要有循环
人可以重复的去做某一件事
程序中必须有一种机制能够控制计算机像人一样重复地去做某一件事
3如何用循环
语法
while条件:
code1
code2
code3
...
user_from_db='egon'
pwd_from_db='123'
whileTrue:
inp_user=input('please input your username:')
inp_pwd=input('please input your password:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('login successfull')
else:
print('userorpassworderr')
while+break:break代表结束本层循环
user_from_db='egon'
pwd_from_db='123'
whileTrue:
inp_user=input('please input your username:')
inp_pwd=input('please input your password:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('login successfull')
break
else:
print('user or password err')
while+continue:continue代表结束本次循环(本次循环continue之后的代码不在运行),直接进入下一次循环
强调:continue一定不要作为循环体的最后一步代码
start=1
whilestart<8:#5<8
ifstart==4:
start+=1#start=5
continue
print(start)
start+=1
whileTrue:
print(1)
print(2)
print(3)
user_from_db='egon'
pwd_from_db='123'
whileTrue:
inp_user=input('please input your username:')
inp_pwd=input('please input your password:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('loginsuccessfull')
break
else:
print('user or password err')
continue
while循环的嵌套
user_from_db='egon'
pwd_from_db='123'
whileTrue:
inp_user=input('please input your username:')
inp_pwd=input('please input your password:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('login successfull')
whileTrue:
cmd=input('>>>:')#cmd='quit'
ifcmd=='quit':
break
print('%srun......'%cmd)
break
else:
print('user o r password err')
........................................................................................................
user_from_db='egon'
pwd_from_db='123'
tag=True
whiletag:
inp_user=input('please inpu tyour username:')
inp_pwd=input('please input you rpassword:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('login successfull')
whiletag:
cmd=input('>>>:')#cmd='quit'
ifcmd=='quit':
tag=False
break
print('%srun......'%cmd)
else:
print('userorpassworderr')
while+else
else的代码会在while循环没有break打断的情况下最后运行
n=1
while n<5:
if n==4:
break
print(n)
n+=1
else:
print('=====》')
user_from_db='egon'
pwd_from_db='123'
count=0
tag=True
whiletag:
ifcount==3:
print('输错次数过多')
break
inp_user=input('please input your username:')
inp_pwd=input('please input your password:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('login successfull')
whiletag:
cmd=input('>>>:')#cmd='quit'
ifcmd=='quit':
tag=False
break
print('%srun......'%cmd)
else:
print('user or passworderr')
count+=1#count=3#输错3次
names=['egon','alex_dsb','lxx_sb','yxx_dsb']
i=0
whilei<len(names):#4<4
print(names[i])
i+=1
for循环:可以不依赖索引而取指
names=['egon','alex_dsb','lxx_sb','yxx_dsb']
foriteminnames:#item='lxx_sb'
print(item)
dic={'x':1,'y':2,'z':3}
forkindic:#k='x'
print(k,dic[k])
for vs while
for可以不依赖于索引取指,是一种通用的循环取指方式
for的循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的
names=['egon','alex_dsb','lxx_sb','yxx_dsb']
foriinrange(0,len(names)):
print(names[i])
for+break
for+continue
for+else
foriinrange(10):
# if i==4:
# break
print(i)
else:
print('====>')
1、有序:但凡有索引的数据都是有序的
2、可变不可变
可变类型:在值变了的情况下,id不变,证明在改原值
不可变类型:在值变了的情况下,id也跟着变,证明不是在改原值
一:基本使用:int
1用途:记录年龄、等级、号码
2定义方式
age=18#age=int(18)
print(type(age))
int('abadf')#报错
int('10.1')#报错
int('101')#int只能将字符串中包含纯数字转成整型
进制转换(了解**)
其他进制转成十进制
二进制:0,1
10#1(21)+0(20)
十进制:0-9
371#3(102)+7(101)+1(100)
八进制:0-7
371#3(82)+7(81)+1(80)
十六进制:0-9A-F
371#3(162)+7(161)+1(8*0)
十进制转成其他进制
print(bin(12))
print(oct(12))#14=>1(81)+4(8**0)
print(hex(16))
二:该类型总结
1存一个值
3不可变
x=10
print(id(x))
x=11
print(id(x))
一:基本使用:float
1用途:记录身高、体重、薪资
2定义方式
salary=3.1#salary=float(3.1)
res=float('3.3')
print(type(res))
3常用操作+内置的方法
二:该类型总结
1存一个值
3不可变
x=10.3
print(id(x))
x=10.4
print(id(x))