2021-07-06python 语句

本文介绍了Python中的条件执行,包括if-elif-else结构和assert语句的使用,展示了如何根据条件执行不同代码块。同时,文章讲解了while循环和for循环的运用,包括遍历列表和使用range()生成序列。这些基础知识对于理解和编写Python程序至关重要。
摘要由CSDN通过智能技术生成

有条件执行if

name=input('what is your name?')
if name.endswith('Gumby'):
	if name.startswith('Mr.'):
		print('Hello,Mr.Gumby')
	elif name.startswith('Mrs.'):
		print('Hellod,Mrs.Gumby')
	else:
		print('Hello,Gumby')
else:
	print('Hello,stranger')
		

断言assert是if语音中很用的一个亲戚,类似于伪代码。如果知道必须满足特定的条件,程序才能正常运行,可在程序中天健assert语句充当检查点。

age=-1
assert 0<age<100,'The age must be realistic'
Tracebak(most recent call last):
	File "<stdin>",line 1,in?
AssertionError:The age must be realistic

while

x=1
while x<100:
	print (x)
	x+=1

for

words=['this','is','an','ex','parrot']
for word in words:
	print (word)
for number in range(1,100):
	print (number)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值