学习笔记以备日后温习:《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|