对于一个混子来说,我从飞✈桨学到了很多python方面的知识,也从一个python小白变成了一个进阶小白。
以下均为笔记,代码来自原课程
list是一种有序的集合,可以随时添加和删除其中的元素。
list1 = [1, 2, 3, 4, 5 ]
list2 = ["a", "b", "c", "d","e","f"]
list3 = ['physics', 'chemistry', 1997, 2000]
len(list1)
list1[4]
list3.append(5)
list1.pop()
123456789
1.1.2 元组 tuple
另一种有序列表叫元组:tuple。tuple和list非常类似,但是tuple一旦初始化就不能修改
tuple1 = (1, 2, 3, 4, 5 )
tuple2 = ("a", "b", "c", "d","e","f")
tuple3 = ('physics', 'chemistry', 1997, 2000)
len(tuple1)
tuple1.append(3)
12345
1.1.3 集合 set
set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。
s = {1,1,2,3,4}
1
1.2 流程控制1.2.1 条件判断
light = '红灯'
if light == '红灯':
print('停')
elif light == '绿灯':
print('行')
else:
print('等一等')
12345678
1.2.2 循环
#猜数字小游戏
my_number = 3200 # 这是真实的价格
guess_number = input('这台冰箱多少钱?') # 所有符号都是英文标点(半角字符)
guess_number = int(guess_number)
while guess_number != my_number:
if guess_number<my_number: # 使用缩进来标识代码段
guess_number = input('猜低了!再猜')
guess_number = int(guess_number)
else:
guess_number = input('猜高了!再猜')
guess_number = int(guess_number)
print('
恭喜您,猜对了!
')
123456789101112
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第一个实例
print( '当前水果 :', fruit)
123
1.2.3 break continue pass
#break
my_number = 3200 # 这是真实的价格
guess_number = input('这台冰箱多少钱?')
guess_number = int(guess_number)
while True:
if guess_number<my_number:
guess_number = input('猜低了!再猜')
guess_number = int(guess_number)
elif guess_number>my_number:
guess_number = input('猜高了!再猜')
guess_number = int(guess_number)
else:
break
print('
恭喜您,猜对了!
')
123456789101112131415
# continue : 跳过本轮
# 打印1-10中的偶数
for i in range(10):
num = i 1
if num%2 != 0:
continue
print(num)
print(num**2)
123456789
# pass: 占位,还没想好怎么写,先让程序跑起来
for i in range(5):
pass
,