One, conditions
1、什么是条件?
条件是判断、影响事物的一个因素
2、什么可以当做条件?
布尔值、比较运算符、数字、None、列表、字典、字符串、元组、集合
其中:判断为假的为 不成立的比较、False 、0、None与空(空字符串、空列表、
空集合、空字典、空元组)
3、为什么要用条件?
因为计算机执行语句判断时,要让计算机知道该如何选择语句去进行执行,
条件的真假为语句是否执行的依据
Second, the process control
1, flow control of the if statement
if 语句
语法1 :
if 条件:
代码1
代码2
代码3
例如:
if 18 > age > 24 :
print ( '你还很年轻哟!' )
print ( '加油小伙伴' )
语法2 :
if 条件:
代码1
代码2
代码3
else :
代码1
代码2
代码3
例如:
if 18 > age> 24 :
print ( '你还很年轻哟!' )
print ( '加油小伙伴' )
else :
print ( '你还小,加油长大' )
print ( '成年人没有容易二字' )
语法3 :
if 条件1 :
代码1
代码2
代码3
elif 条件2 :
代码1
代码2
代码3
elif 条件2 :
代码1
代码2
代码3
例如:
if 18 > age> 24 :
print ( '你还很年轻哟!' )
print ( '加油小伙伴' )
elif age< 18 :
print ('你还小,加油长大' )
elif 40 > age > 24 :
print ( '成年人没有容易二字' )
语法4 :
if 条件1 :
代码1
代码2
代码3
elif 条件2 :
代码1
代码2
代码3
elif 条件2 :
代码1
代码2
代码3
else :
代码1
代码2
代码3
例如:
if 18 > age> 24 :
print ( '你还很年轻哟!' )
print ( '加油小伙伴' )
elif age< 18 :
print ('你还小,加油长大' )
elif 40 > age > 24 :
print ( '成年人没有容易二字' )
else :
print ( '你好,中年人' )
语法5 :if 嵌套if
if 条件:
语句1
语句2
if 条件:
语句1
语句2
else :
语句1
语句2
else :
语句1
语句2
if 18 > age> 40 :
if 18 > age > 24
print ( '你还很年轻哟!' )
else :
print ( '成年人没有容易二字' )
else :
print ( '你是中年人还是小朋友?' )
2, while the flow control loop
2.1, using the basic syntax of the while loop
语法:
print (1 )
while 条件:
代码1
代码2
代码3
print (3 )
例如:
count= 0
while count < 5 :
print ( count)
count+= 1
print ( '顶级代码----->' )
2.2, the cycle of death and efficiency
2.2.1 infinite loop
count = 0
while count < 5 :
print ( count)
while True :
name = input ( '输入你的姓名' )
print ( name)
2.2.2, efficiency
while True :
1 + 1
while 1 :
print ( 'xxxx' )
2.3, application cycle
做一个登录口
例如:
uesrname = '123'
password = '666'
while True :
inp_name= input ( '请输入您的账号:' )
inp_pwd= input ( '请输入您的密码:' )
if inp_name == username and inp_pwd == password:
print ( '登录成功' )
else :
print ( '账号名或密码错误' )
2.4, two ways to exit the loop
方式一:
tag= True
while tag:
inp_name= input ( '请输入您的账号:' )
inp_pwd= input ( '请输入您的密码:' )
if inp_name == username and inp_pwd == password:
print ( '登录成功' )
tag = False
else :
print ( '账号名或密码错误' )
方式二:
while True :
inp_name= input ( '请输入您的账号:' )
inp_pwd= input ( '请输入您的密码:' )
if inp_name == username and inp_pwd == password:
print ( '登录成功' )
break
else :
print ( '账号名或密码错误' )
2.5, and end loop nests
语法:
tag= True
while tag:
while tag:
while tag:
tag= False
例如:
tag= True
while tag:
inp_name= input ( '请输入您的账号:' )
inp_pwd= input ( '请输入您的密码:' )
if inp_name == username and inp_pwd == password:
print ( '登录成功' )
while tag:
cmd= input ( "输入命令>: " )
if cmd == 'q' :
tag= False
else :
print ( '命令{x}正在运行' . format ( x= cmd) )
else :
print ( '账号名或密码错误' )
语法:
while True :
while True :
while True :
break
break
break
例如:
while True :
inp_name= input ( '请输入您的账号:' )
inp_pwd= input ( '请输入您的密码:' )
if inp_name == username and inp_pwd == password:
print ( '登录成功' )
while True :
cmd= input ( "输入命令>: " )
if cmd == 'q' :
break
print ( '命令{x}正在运行' . format ( x= cmd) )
break
else :
print ( '账号名或密码错误' )
2.6、while与continue
while+continue:结束本次循环,直接进入下一次循环
例如:
count= 0
while count < 6 :
if count == 4 :
count+= 1
continue
print ( count)
2.7、while与else
while+else:else包含的代码会在while循环结束后,并且while循环是在没
有被break打断的情况下正常结束的时候,才会运行
例如:
count= 0
while count < 6 :
if count == 4 :
continue
print ( count)
count+= 1
else :
print ( '======>' )
3, the process control loop for
3.1 What is the for loop
for循环是python提供的第二种循环机制
3.2 Why should the for loop
理论上for循环能做的事情,while循环全都可以做,之所以要有for循环
是因为for循环在循环取值(遍历取值)比while循环更简洁
3.3, how to use a for loop
语法:
for 变量名 in 可迭代对象:
代码1
代码2
代码3
3.3.1, a listing cycle value
例如:
L = [ 'dog' , 'cat' , 'xingxing' ]
for x in L:
print ( x)
3.3.2, dictionaries cycle value
例如:
dic= { 'k1' : 111 , 'k2' : 2222 , 'k3' : 333 }
for k in dic:
print ( k, dic[ k] )
3.3.3 String cycle value
例如:
msg= "you can you up,no can no bb"
for x in msg:
print ( x)
3.3.4、for与else、continue、break
使用方式与while一样,可以参考while循环
3.3.5, for the control loop cycles and the range ()
range功能介绍
>> > range ( 10 )
[ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
>> >
>> > range ( 1 , 9 )
[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
>> >
>> > range ( 1 , 9 , 1 )
>
[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
>> > range ( 1 , 9 , 2 )
[ 1 , 3 , 5 , 7 ]
for i in range ( 30 ) :
print ( '===>' )
range补充知识(了解)
1、for搭配range,可以按照索引取值,但是麻烦,所以不推荐
l=['aaa','bbb','ccc'] # len(l)
for i in range(len(l)):
print(i,l[i])
for x in l:
print(x)
2、range()在python3里得到的是一只"会下蛋的老母鸡"
3.3.6, for a nested loop
for循环嵌套:外层循环循环一次,内层循环需要完整的循环完毕,终止
for循环只有break一种方案
例如:
for i in range ( 3 ) :
print ( '外层循环-->' , i)
for j in range ( 5 ) :
print ( '内层-->' , j)
3.4, for loop and while loop similarities and differences
1、相同之处:都是循环,for循环可以干的事,while循环也可以干
2、不同之处:while循环称之为条件循环,循环次数取决于条件何时
变为假 for循环称之为"取值循环",循环次数取决in后包含的值的个数