Python if、while、for语句:

Python 条件if语句:

score = 90
if score >= 90:  # 一般格式,多重分支
    print('优秀')
elif score >= 80:
    print('良')
elif score >= 60:
    print('及格')
else:
    print('不及格')

result = '及格' if score >= 60 else '不及格'  # 三元运算
print(result)


def add(x):
    print(x + 10)


def default_method(x):
    print('什么都不做')


operation = {
    'add': add,
    'update': lambda x: print(x * 2),
    'delete': lambda x: print(x * 3)
}

operation.get('add', default_method)(10)
operation.get('delet', default_method)(10)

Python while循环语句:

x = 'youpinketang'

while x:  # x为真true,循环;x为空字符串,为faulse停止循环
    print(x, end=' ')
    x = x[1:]

print('\n')
x = 10
while x:
    x -= 1
    if x % 2 != 0:
        continue
    print(x, end=' ')

while True:
    name = input('请输入您的姓名:')
    if name == 'stop':
        break
    age = input('请输入您的年龄:')
    print('您好:{},您的年龄是:{},欢迎学习Pythpn'.format(name, age))
print('循环结束')

Python for循环语句:

for x in [1, 2, 3, 4]:  # in 序列里面遍历
    print(x, end=' ')

lsum = 0
for x in [1, 2, 3, 4, 5]:
    lsum += x
print(lsum)
print('\n')

emp = {
    'name': 'Tom',
    'department': 'tech',
    'job': 'develop',
    'salary': 9000.00
}
for key in emp:
    print('{}=>{}'.format(key, emp[key]))

s1 = 'youpinketang'
s2 = 'coderoomg'
result = []

for x in s1:
    if x in s2:
        result.append(x)

print(result)

l = [x for x in s1 if x in s2]
print(l)

for x in range(0, 101, 3):  # range是生成器对象,不是函数返回序列
    print(x)

s = 'youpinkecheng'  # 遍历输出每个字母及其索引位置序号
for idx, item in enumerate(s):  # (idx, item)构成元组,括号可省略
    print('{}=>{}'.format(idx + 1, item))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值