Python常用语句

1、Python 条件语句

用法:

if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
    执行语句4……

实例:(要注意空格对齐)

num = 5

 if num == 3: # 判断num的值

    print 'boss'

 elif num == 2:

    print 'user'

 elif num == 1:

    print 'worker'

 elif num < 0: # 值小于零时输出

    print 'error' else: print 'roadman' # 条件均不成立时输出

 

2、Python 循环语句

1)while

len=5
while len>0:
    len-=1
    if len%2>0:
        continue
    print(len)
else:
    print(len)

2)for

fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # 第二个实例
    print('当前水果 :', fruit)
print ("Good bye!")

---------------------------------

fruits = ['banana', 'apple',  'mango']
for index in range(len(fruits)):
    print ('当前水果 :', fruits[index])
else:
    print("end")
print ("Good bye!")

---------------------------------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值