Python流程控制
2022/6/30 1:51:14
本文主要是介绍Python流程控制,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Python流程控制
1、if条件选择
# coding:utf-8 # Time:2022/6/28 20:57 # Author:Yang Xiaopeng num = 23 if num>2: print("dayu") if num<2: print("xiaoyu") ## if else if num>10: print("D") else: print("A") ## if elif else if num < 10: print("D") elif num < 20: print("C") elif num < 30: print("B") else: print("A") ## if 嵌套 if num > 10: if num>20: if num >30: print("A") else: print("B") else: print("C") else: print("D")
2、for循环
# coding:utf-8 # Time:2022/6/28 20:57 # Author:Yang Xiaopeng for i in (1,2,3,4,5,6,7,8): if i < 8: print(i,end="____") else: print(i) list1 = [1,2,3,4,5,6] for item in list1: if item / 2 == 1: print(item) for item in "asjhlafjhkllsadk": if item == 'k': print(item) else: print(item,end="_")
3、while循环
# coding:utf-8 # Time:2022/6/28 20:57 # Author:Yang Xiaopeng # 死循环 """ while True: pass """ num = 1 while num<10: print(num,end="_") num+=1
4、break 及 continue
- break: 结束循环
- continue 退出本轮循环,继续下一轮循环
# coding:utf-8 # Time:2022/6/28 20:57 # Author:Yang Xiaopeng num = 0 while num<10: num += 1 if num <=6: continue print(num, end="_") print("") num =0 while num<10: num = num + 1 if num == 6: break print(num, end="_")
这篇关于Python流程控制的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Python基础编程
- 2024-11-25Python编程基础:变量与类型
- 2024-11-25Python编程基础与实践
- 2024-11-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南