Python基础整理之if、while、for

学习python已经有一段时间了,近期有时间就整理整理学过的东西,温故而知新。

Python基础整理之if、while、for

一、if-else

代码格式

if 条件:
    满足条件时
else:
    不满足条件时

栗子:

chePiao = 1 # 用1代表有车票,0代表没有车票
if chePiao == 1:
    print("有车票,可以上火车")
    print("终于可以见到Ta了,美滋滋~~~")
else:
    print("没有车票,不能上车")
    print("亲爱的,那就下次见了,一票难求啊")

运行结果

有车票,可以上火车
终于可以见到Ta了,美滋滋~~~

二、elif

代码格式:

if 满足1:
    事情1
elif 满足2:
    事情2
elif 满足3:
    事情3

案例:

score = 77
if score>=90 and score<=100:
    print('本次考试,等级为A')
elif score>=80 and score<90:
    print('本次考试,等级为B')
elif score>=70 and score<80:
    print('本次考试,等级为C')
elif score>=60 and score<70:
    print('本次考试,等级为D')
elif score>=0 and score<60:
    print('本次考试,等级为E')

运行结果

本次考试,等级为C

和else一起使用

格式:

if 满足1:
    事情1
elif 满足2:
    事情2
else
    其他事情

三、if嵌套

格式:

if 条件1:
    满足条件1。。。。
    if 条件2:
        满足条件2。。。

案例:

chePiao = 1     # 用1代表有车票,0代表没有车票
daoLenght = 9     # 刀子的长度,单位为cm
if chePiao == 1:
    print("有车票,可以进站")
    if daoLenght < 10:
        print("通过安检")
    else:
        print("没有通过安检")
        print("刀子的长度超过规定,等待警察处理...")
else:
    print("没有车票,不能进站")

运行结果

有车票,可以进站
通过安检

情节描述:上公交车,并且可以有座位坐下

要求:输入公交卡当前的余额,只要超过2元,就可以上公交车;如果空座位的数量大于0,就可以坐下

代码:

money = 10
seatCount = 0
if money > 2: 
    print("可以上车") 
    if(seatCount > 0): 
        print("可以坐下") 
    else: 
        print("不可以坐下")
else: 
    print("不可以上车") 

运行结果

可以上车
不可以坐下

四、while循环

格式:

while 条件:
    满足条件。。。。

案例:

i =0
while i < 3:
    print("当前是第%d次执行循环"%(i+1))
    print("i=%d"%i)
    i+=1

运行结果:

当前是第1次执行循环
i=0
当前是第2次执行循环
i=1
当前是第3次执行循环
i=2

五、while循环的应用

1、计算1-100的累加和

i = 1
sum = 0
while i <= 100:
    sum = sum + i
    i += 1
print('1~100的累加和为:%d'%sum)

运行结果:

1-100的累加和为:5050

2、计算1~100之间偶数的累加和

i = 1
sum = 0
while i <= 100:
    if i % 2 == 0:
        sum = sum + i
    i += 1
print('1-100的偶数累加和:%d'%sum)

运行结果:

1-100的偶数累加和:2550

六、while循环的嵌套

格式:

while 条件1:
    满足条件1。。。。
    while 条件2:
        满足条件2。。。

案例一:打印如下图形


*
* *
* * *
* * * *
* * * * *

代码块:

i = 1
while i <= 5:
    j = 1
    while j <= i:
        print('* ', end= '')
        j += 1
    print('\n')
    i += 1

案例二:九九乘法表

i = 1
while i <= 9:
    j = 1
    while j <= i:
        print('%d*%d=%-4d'%(j, i, i * j), end='')
        j += 1
    print('\n')
    i += 1

运行结果:

1*1=1

1*2=2   2*2=4

1*3=3   2*3=6   3*3=9

1*4=4   2*4=8   3*4=12  4*4=16

1*5=5   2*5=10  3*5=15  4*5=20  5*5=25

1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36

1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49

1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64

1*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81

七、for循环

格式:

for 临时变量 in 列表或者字符串等:
    循环满足条件。。。
else
    不满足条件执行

案例一

str = 'hello word'
for i in str:
    print(i)

结果:

h
e
l
l
o

w
o
r
d

八、break和continue

1break

for循环带入break

案例

name = 'dongGe'   
for x in name:
    print('----')
    if x == 'g':
        break
    print(x)

运行结果:

----
d
----
o
----
n
----

while循环带入break

案例

i = 0
while i < 10:
    i = i + 1
    print('----')
    if i == 5:
        break
    print(i)

运行结果

----
1
----
2
----
3
----
4
----

小结 break的作用:用来结束整个循环

2、continue

for循环带入continue

实例

name = 'dongGe'
for x in name:
    print('----')
    if x == 'g':
        continue
    print(x)

运行结果:

----
d
----
o
----
n
----
----
G
----
e

while循环带入continue

实例

i = 0
while i<10:
    i = i+1
    print('----')
    if i==5:
        continue
    print(i)    

运行结果:

----
1
----
2
----
3
----
4
----
----
6
----
7
----
8
----
9
----
10

小结:continue的作用:用来结束本次循环,紧接着执行下一次的循环

上一篇:Python整理之字符串操作 下一篇:《MongoDB权威指南》之MongoDB初探

评论

评论(0)

暂无评论