Python基本语法:循环语句
2019-01-27 22:39
309 查看
学习笔记以备日后温习:《Python3入门与进阶》
分支循环条件与枚举
包、模块、函数
功能:输入账号密码完成开锁
account = 'qiyue' password = '123456' print('please input account') user_account = input() print('please input password') user_password = input() if account==user_account and password==user_password: # '''问题一:=是赋值,==表示判断是否相等''' # '''问题二:两个表达式逻辑关系是且,因此用and''' print('success') else: # '''问题三:else后面有冒号:''' print('fail')
constant 常量
invalid 不合法的
copyright 版权
pass #空语句/占位语句
循环语句
#循环中while一般在递归中常用 counter = 1 while counter <=10: counter +=1 print(counter) else: print('终于打到最强王者') #for主要用来遍历/循环 序列或者集合、字典 a = [['apple','orange','banana','grape'],(1,2,3)] for x in a: for y in x: print(y) #输出为一列 print(y,end='') #输出为一行 x是['apple','orange','banana','grape'],(1,2,3) y是apple,orange,banana,grape,1,2,3
由于 python 并不支持 switch 语句,所以多个条件判断,用 elif 来实现,或者用dict
#dict is better than switch a = input() fruits = {'1':'apple','2':'orange','3':'banana'} print(fruits[a])
多维度循环中,break跳出的是内层循环,不影响最外层循环
a = [['apple','orange','banana','grape'],(1,2,3)] for x in a: for y in x: if y=='orange': break #跳出['apple','orange','banana','grape']循环 print(y) esle: print('fruit is gone') 输出为 apple 1 2 3 fruit is gone ~~~~~~~~~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~~~~~~~ a = [['apple','orange','banana','grape'],(1,2,3)] for x in a: if 'banana' in x: break for y in x: if y=='orange': break #跳出['apple','orange','banana','grape']循环 print(y) else: print('fruit is gone') 无输出!!因为break跳出了最外层的if else循环,不在打印fruit is gone
打印10个数字
#C++中 for(i=0;i<10;i++){ } #python中 for x in range(0,10): print(x) for x in range(0,10,2): print(x,end='|') #0|2|4|6|8|
打印奇数
a=[1,2,3,4,5,6,7,8] for x in range(0,len(a),2): #range(起始位,末尾位,步长) print(a[x],end='|') #1|3|5|7|
相关文章推荐
- Python基础语法学习--条件控制与循环语句
- Python的基本语法——语句块
- PHP基本语法(五)—— 循环语句
- Python基础语法——输出输出和基本语句(一)
- Python While循环语句 Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为: while 判断条件:
- Python从入门到放弃(三):基本语句和语法
- Python笔记1:基本语法和控制语句
- python自动化学习之语法学习(判断语句和循环语句)
- Python从入门到放弃(三):基本语句和语法
- Python基本数据类型及循环语句
- python语法快速入门(基本数据类型,条件选择,循环)
- Python学习-while循环基本语法
- 【Python全栈04】基本语法——循环
- Python的基本语法,涵盖数据类型、循环判断、列表、map和set等
- smarty基本语法之判断,循环语句
- python基本循环语句
- Python - Day9 - 初识&变量&条件语句&基本数据类型&循环
- Python详解(一)Python的基本语法,简单数据类型,和while,for循环控制,ifesle控制
- Verilog学习笔记基本语法篇(六)········ 循环语句
- LotusScript基本语法知识……选择和循环语句