python全栈开发-Day2 布尔、流程控制、循环
python全栈开发-Day2 布尔 流程控制 循环
一、布尔
1、概述
#布尔值,一个True一个False #计算机俗称电脑,即我们编写程序让计算机运行时,应该是让计算机无限接近人脑,或者说人脑能干什么,计算机就应该能干什么,
人脑的主要作用是数据运行与逻辑运算,此处的布尔类型就模拟人的逻辑运行,即判断一个条件成立时,用True标识,不成立则用False标识
a=3
b=5
a > b #不成立就是False,即假
#False
a < b #成立就是True, 即真
#True
#接下来就可以根据条件结果来干不同的事情了:
if a > b:
print(a is bigger than b )
else :
print(a is smaller than b )
#上面意味着,计算机已经可以像人脑一样根据判断结果不同,来执行不同的动作。2、布尔类型的重点知识!!!:
#所有数据类型都自带布尔值 1、None,0,空(空字符串,空列表,空字典等)三种情况下布尔值为False 2、其余均为真
3、 重点:
#1.可变类型:在id不变的情况下,value可以变,则称为可变类型,如列表,字典 #2. 不可变类型:value一旦改变,id也改变,则称为不可变类型(id变,意味着创建了新的内存空间)
二、 流程控制之if...else
既然我们编程的目的是为了控制计算机能够像人脑一样工作,那么人脑能做什么,就需要程序中有相应的机制去模拟。
人脑无非是数学运算和逻辑运算,对于数学运算在上一节我们已经说过了。对于逻辑运算,即人根据外部条件的变化而做出不同的反映,比如:
1、如果:女人的年龄>30岁,那么:叫阿姨
age_of_girl=31
if age_of_girl > 30:
print('阿姨好')2、 如果:女人的年龄>30岁,那么:叫阿姨,否则:叫小姐
age_of_girl=18
if age_of_girl > 30:
print('阿姨好')
else:
print('小姐好')3、 如果:女人的年龄>=18并且<22岁并且身高>170并且体重<100并且是漂亮的,那么:表白,否则:叫阿姨
age_of_girl=18
height=171
weight=99
is_pretty=True
if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True:
print('表白...')else:
print('阿姨好')4、如果:成绩>=90,那么:优秀
如果成绩>=80且<90,那么:良好
如果成绩>=70且<80,那么:普通
其他情况:很差
score=input('>>: ')
score=int(score)
if score >= 90:
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差')if 条件1: 缩进的代码块 elif 条件2: 缩进的代码块 elif 条件3: 缩进的代码块 ...... else: 缩进的代码块
三、 流程控制之while循环
1 、为何要用循环
#上节课我们已经学会用if .. else 来猜年龄的游戏啦,但是只能猜一次就中的机率太小了,如果我想给玩家3次机会呢?
就是程序启动后,玩家最多可以试3次,这个怎么弄呢?你总不会想着把代码复制3次吧。。。。
age_of_oldboy = 48
guess = int(input(">>:"))
if guess > age_of_oldboy :
print("猜的太大了,往小里试试...")
elif guess < age_of_oldboy :
print("猜的太小了,往大里试试...")
else:
print("恭喜你,猜对了...")
#第2次
guess = int(input(">>:"))
if guess > age_of_oldboy :
print("猜的太大了,往小里试试...")
elif guess < age_of_oldboy :
print("猜的太小了,往大里试试...")
else:
print("恭喜你,猜对了...")
#第3次
guess = int(input(">>:"))
if guess > age_of_oldboy :
print("猜的太大了,往小里试试...")
elif guess < age_of_oldboy :
print("猜的太小了,往大里试试...")
else:
print("恭喜你,猜对了...")
#即使是小白的你,也觉得的太low了是不是,以后要修改功能还得修改3次,因此记住,写重复的代码是程序员最不耻的行为。
那么如何做到不用写重复代码又能让程序重复一段代码多次呢? 循环语句就派上用场啦2 、条件循环:while,语法如下
while 条件:
# 循环体
# 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。
# 如果条件为假,那么循环体不执行,循环终止#打印0-10
count=0
while count <= 10:
print('loop',count)
count+=1
#打印0-10之间的偶数
count=0
while count <= 10:
if count%2 == 0:
print('loop',count)
count+=1
#打印0-10之间的奇数
count=0
while count <= 10:
if count%2 == 1:
print('loop',count)
count+=13 、死循环
import time #调用模块后面会讲
num=0
while True:
print('count',num)
time.sleep(1)
num+=1 4 、循环嵌套与tag
tag=True #定义真 while tag: ...... while tag: ........ while tag: tag=False #定义假
#练习,要求如下:
1 循环验证用户输入的用户名与密码
2 认证通过后,运行用户重复执行命令
3 当用户输入命令为quit时,则退出整个程序 1 #实现一:
2 name='egon'
3 password='123'
4
5 while True:
6 inp_name=input('用户名: ')
7 inp_pwd=input('密码: ')
8 if inp_name == name and inp_pwd == password:
9 while True:
10 cmd=input('>>: ')
11 if not cmd:continue
12 if cmd == 'quit':
13 break
14 print('run <%s>' %cmd)
15 else:
16 print('用户名或密码错误')
17 continue
18 break
19
20 #实现二:使用tag
21 name='egon'
22 password='123'
23
24 tag=True
25 while tag:
26 inp_name=input('用户名: ')
27 inp_pwd=input('密码: ')
28 if inp_name == name and inp_pwd == password:
29 while tag:
30 cmd=input('>>: ')
31 if not cmd:continue
32 if cmd == 'quit':
33 tag=False
34 continue
35 print('run <%s>' %cmd)
36 else:
37 print('用户名或密码错误')4 、break与continue
#break用于退出本层循环
while True:
print "123"
break
print "456"
#continue用于退出本次循环,继续下一次循环
while True:
print "123"
continue
print "456"5 、while+else
#与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句,while 后面的else 作用是指,<br />当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句
count =while count <= 5 :
count += 1
print("Loop",count)
else:
print("循环正常执行完啦")
print("-----out of while loop ------")
输出
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
循环正常执行完啦
-----out of while loop ------
#如果执行过程中被break啦,就不会执行else的语句啦
count =while count <= 5 :
count += 1
if count == 3:break
print("Loop",count)
else:
print("循环正常执行完啦")
print("-----out of while loop ------")
输出
Loop 1
Loop 2
-----out of while loop ------四 、流程控制之for循环
1 迭代式循环:for,语法如下
for i in range(10):
缩进的代码块
2 break与continue(同上)
3 循环嵌套
#打印九九乘法表
for i in range(1,10):
for j in range(1,i+1):
print('%s*%s=%s' %(i,j,i*j),end=' ') #print有默认的换行,这里就把默认的换行改成空的字符串
print()分析 如何打印金字塔
'''
#max_level=5
* #current_level=1,空格数=4,*号数=1
*** #current_level=2,空格数=3,*号数=3
***** #current_level=3,空格数=2,*号数=5
******* #current_level=4,空格数=1,*号数=7
********* #current_level=5,空格数=0,*号数=9
#数学表达式
空格数=max_level-current_level
*号数=2*current_level-1
'''
#实现
max_level=5
for current_level in range(1,max_level+1):
for i in range(max_level-current_level):
print(' ',end='') #在一行中连续打印多个空格
for j in range(2*current_level-1):
print('*',end='') #在一行中连续打印多个空格
print()