1、条件语句
形式:
if 判断语句 :
执行语句1
elif 判断语句2:
执行语句2
elif 判断语句3:
执行语句3
#...
else:
执行语句4
占位符 pass
意义:
if (如果) A :
就 B(当A为True)
elif(或者) C :
就 D(当A为False并且C为True)
else(否则) :
就E(当A和C都为False)
example:
'''
需求: 1.年龄大于12岁,卖成人票。年龄大于60,就不需要买票
2. 4-12 ,儿童票
3.0-3 ,不买票
'''
#1
if a>=12:
print('你好先生,您需要购买成人票') #1
elif a>=4 and a<12:
print('您好小朋友,您需要购买儿童票') #2
else:
print('你好baby,您不需要买票') #3
# 随机数
import random
r =random.randint(0,100)
print('随机数',r)
a = r
# 2
if a>=12:
if a>=60:
print('您好,你的年龄 %s,不需要买票'%a) #4
else:
print('你好先生 %s,您需要购买成人票'%a) #1
elif a>=4 and a<12:
print('您好小朋友 %s,您需要购买儿童票'%a) #2
else:
print('你好baby %s,您不需要买票'%a) #3
## pass 占位,什么事都不做
a = 1
b = 2
if a>b:
pass
elif a<b:
pass
else: # =
pass
if a>b:
print('a大于b')
elif a<b:
print('a小于b')
else: # =
print('a等于b')
2、循环语句
1.while循环
while 判断语句A:
执行语句B
else:
print('程序正常结束,执行else')
#while
'''
while True: #死循环
print('hello')
'''
a = 1
while a<5: #写while 循环的时候,一定要写一个终止条件
print('hello',a)
a = a+1
注意:循环一般要有终止条件
2.break和continue
while True:
break #终止循环
continue #跳过本次循环
#break 会终止循环,循环不再执行
#continue是跳过本次循环,循环继续
print('=========break==========')
##break 强行终止 循环 ,相当于 Ctrl +c
b = 0
while True: #死循环
print('hello',b)
if b>5: # 0
break
b +=1
print('=========continue==========')
##continue 跳过本次循环,进入下一个循环
#打印出10以内的奇数
m = 0
while m <10:
m += 1
if m%2 ==0:
continue
print(m)
3.range
range(10) #表示0 - 9 这个范围
range(1,10) #表示 1 - 9这个范围
range(1,10,2) #表示 1 - 9这个范围,并且以步长2进行取数
range(10) #范围 左闭右开
list(range(10))
list(range(5,10))
list(range(5,10,2))
print('=========for==========')
for i in range(10):
print(i)
4.for循环
for item in iterable:
执行语句
else:
print('程序正常结束,执行else')
#循环条件可以是任何可迭代的对象,如:序列类型,集合和字典
#列表
li =['a','b','c']
for f in li:
print(f)
#字符串
for s in 'abcdefgh':
print(s)
#字典
di ={'ai':1,'bi':2,'ci':3}
for d in di: #遍历迭代对象
print(d)
print(di[d])
###
for f in li:
print(f)
f =100
print(f)
##for 打印10以内奇数
for i in range(1,11): #for自动结束,while自己写终止条件
if i % 2 ==0:
continue
print(i)
### 嵌套循环,5小组,每个8位同学
for i in range(5):
print('第 %s小组'%(i+1))
for j in range(8):
print('第 %s小组,第 %s同学'%((i+1),(j+1)))
for i in range(5):
print('第 %s小组'%(i+1))
n=1
while n<=8:
print('第 %s小组,第 %s同学'%((i+1),n))
n +=1
5.else
while True:
break
else:
print('OK')
#for
for item in iterable:
break
else:
print('OK')
"""
只有正常结束的循环,非break结束的循环才会执行else部分
"""
#else 只有正常结束的循环,非break结束的循环才会执行else部分
a =0
while a<10:
print(a)
a +=1
else:
print('while 循环正常结束')
b =0
while b<10:
print(b)
b +=1
if b>5:
break
else:
print('while 循环正常结束')
#for
for i in range(10):
print(i)
else:
print('for 正常结束')
for i in range(10):
print(i)
if i>5:
break
else:
print('for 正常结束')
3、print方法扩展
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
print('aaaaa',end='') #打印后不会回车
print('bbbbbbb')
print('aaa',1)
print('aaa',1,sep='*********') #会以****隔开两个打印的对象
Python基本数据类型:https://www.songqinnet.com/article/966
Python序列类型各自方法:https://www.songqinnet.com/article/970
Python字符串拼接和格式化输出:https://www.songqinnet.com/article/973
Python集合、字典及运算符:https://www.songqinnet.com/article/975
Python条件与循环语句:https://www.songqinnet.com/article/978
Python函数基础:https://www.songqinnet.com/article/981
Python内置函数、作用域、闭包、递归:https://www.songqinnet.com/article/983
文章来源: 松勤网
-
讨债公司/蓝月传奇辅助/蓝月辅助(0) 回复 (0)