前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python基本操作(五)

python基本操作(五)

作者头像
py3study
发布2020-01-15 20:06:36
2400
发布2020-01-15 20:06:36
举报
文章被收录于专栏:python3

if 判断

if 条件: 代码1 代码2 代码3 代码块(同一缩进级别的代码,例如代码1、代码2和代码3是相同缩进的代码,这三个代码组合在一起就是一个代码块,相同缩进的代码会自上而下的运行) cls ='humale' gender = 'female' age = 18 if cls =='human' and gender =='female' and age >16 and age < 22: print('开始表白') print('end...')

语法

  • if
  • if...else
  • if...elif...else

elif...else表示if条件1成立干什么,elif条件2成立干什么,elif条件3成立干什么,elif...否则干什么。

cls = 'human' gender = 'female' age = 28 if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('开始表白') elif cls == 'human' and gender == 'female' and age > 22 and age < 30: print('考虑下') else: print('阿姨好')

while循环

while 条件为 true

​ 代码块

while 1: egon_age = 73

代码语言:javascript
复制
age = input('请输入你猜的年龄》》》')
age = int(age)

if age == egon_age:
    print('猜对了')
elif age > egon_age:
    print('猜大了')
elif age < egon_age:
    print('猜小了')

print('我跳出while循环了')

  • while+break

while 1: egon_age = 73

代码语言:javascript
复制
age = input('请输入你猜的年龄》》》')  # 73
if age == egon_age:  # 成立
    print('猜对了')
    break
elif age > egon_age:
    print('猜大了')
elif age < egon_age:
    print('猜小了')
  • while循环嵌套

prize = {0: 'zhangyan', 1: 'bu_wawa', 2: 'nick', 3: 'balloon_wawa'}

while 1: egon_age = 73

代码语言:javascript
复制
age = input('请输入你猜的年龄》》》')  # 73
age = int(age)  # 73

if age == egon_age:  # 成立
    while True:
        print(F'猜对了!!!\n请选择下列奖项中的一个:{prize}')
        choice = input('请选择你需要的奖品。')
        choice = int(choice)
        if choice == 2:
            print('大傻,这个不能给你')
            print('重新选择\n')
        else:
            print(f'大傻,{prize[choice]}这个你也要\n')
            break
    break
elif age > egon_age:
    print('猜大了')
elif age < egon_age:
    print('猜小了')
  • while+continue

count = 1 while count < 101: # 1<101,2<101,count=50

代码语言:javascript
复制
if count == 50:
    count += 1
    continue   # 不执行下面的代码

print(count)  # 1,2
count += 1  # count=2,count=3

break直接终止整个while循环;continue只是不执行下面的代码,但是会继续循环下去

  • while+elsea 当while循环没有被break掉的时候,会执行else下面的代码。while 不能为true count = 1 while count < 101: # 1<101,2<101,count=50, if count == 50: count += 1 break # 不执行下面的代码 print(count) # 1,2 count += 1 # count=2,count=3 else: print('我没有被break掉')

for循环

代码语言:javascript
复制
for循环的循环次数受限于容器类型的长度,而while循环的循环次数需要自己控制。for循环也可以按照索引取值。

​ for i in 列表和字典

​ i就是列表的每个元素

  • for+break

​ for循环调出本层

  • for+continue for循环调出本层,进入下次循环。
  • for循环的嵌套 外层循环循环一次,内层循环循环所有的 for i in range(3): print(f'-----:{i}') for j in range(2): print(f'*****:{j}') -----:0 *****:0 *****:1 -----:1 *****:0 *****:1 -----:2 *****:0 *****:1
  • for+else for循环没有break的时候触发else内部代码块 name_list = ['nick', 'jason', 'tank', 'sean'] for name in name_list: print(name) else: print('for循环没有被break中断掉') nick jason tank sean for循环没有break中断掉
  • for循环实现loading import time print('Loading', end='') for i in range(6): print(".", end='') time.sleep(0.2) Loading......

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/06/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • if 判断
  • 语法
  • while循环
  • for循环
相关产品与服务
日志服务
日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档