Python跟着[美]?埃里克·马瑟斯的Python编程 从入门到实践 第2版学的,当时买回来这本书就在书的扉页写下来日期“2021.6.15”,也是在这一天第一次见到了我的导师。
不说多了,今天早上上了节英语课,下午三点多到自习室待到现在。Python学了操作列表这一小节,主要有遍历整个列表、用range函数创建数值列表、切片、遍历切片、元组、遍历元组,今天的课后动手练一练都能做对,一个小小的突破!
#10.28学习记录
names=["xiaohan","xiaoyan","xiaogang"]
print(names[-1])
print(names[0])
print(f"my name is {names[0]},you have some question!")
names[1]="xiaopei"
print(names[1])
#操作列表 for循环
lovers=["xiaol","xiaop","xiaoy"]
for nihao in lovers: #这里特别注意,for循环这一行末尾要加冒号(:)
print(nihao) 变量"nihao"自己随便定义
magicians=["liuqian","xiehao","shengli"]
for magician in magicians:
print(magician)
the lovin=["hahahha"] #变量定义只能是一个词,不可以多个词
print(the lovin)
words=["you","can","tell","me"]
for xiaolei in words:
print(xiaolei)
lovers=["xiaol","xiaop","xiaoy"]
for affectiona in lovers: #经历三次迭代
print(f'{affectiona.title()},I write to you every week')
print(f"Nice talking to you,{affectiona.title()}.\n") #注意换行符“\n”的位置
lovers=["xiaol","xiaop","xiaoy"]
for affectiona in lovers: #经历三次迭代
print(f'{affectiona.title()},I write to you every week') #在for循环后,没有缩进的代码只运行一次
print(f"Nice talking to you,{affectiona.title()}.\n") #注意换行符“\n”的位置
words=["tell","me","why"]
for word in words:
print({word.title()})
lovers=["xiaol","xiaop","xiaoy"]
for affectiona in lovers: #经历三次迭代
print(f'{affectiona.title()},I write to you every week')
print(f"Nice talking to you,{affectiona.title()}") #注意换行符“\n”的位置
print(f"I want to see you next time,{affectiona.title()}.\n")
#对于for语句后面要循环的语句一定要缩进
lovers=["xiaol","xiaop","xiaoy"]
for affectiona in lovers: #经历三次迭代
print(affectiona)
lovers=["xiaol","xiaop","xiaoy"]
for affectiona in lovers: #经历三次迭代
print(affectiona)
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
#P48小练习
Pizzas=["Italian pizza",'American pizza']
for Pizza in Pizzas:
print(Pizza)
Pizzas=["Italian pizza",'American pizza']
for Pizza in Pizzas:
print(f"There are many kinds of pizza I like to eat, but my favorite is {Pizza}")
print(f"Italian pizza looks good and American pizza is delicious!")
print(f"I really love pizza!")
animals=["cat","dog","bear"]
for animal in animals:
print(animal)
animals=["cat","dog","bear"]
for animal in animals:
print(f"A {animal} would make a great pet.")
print(f"They are all my favorite animals!")
#创建数值列表
#用range函数,此处本应该打印1-10数字,但实际运行只会打印1-9,因为这是编程语言中的差一行为,只会打印到指定的第二个值时停止
for me in range(0,10):
print(me)
for me in range(1,100000):
print(me)
for me in range(10):
print(me)
#函数range几乎能创建任何需要的数集
numbers=list(range(1,20)) #创建数字列表,注意list后面用(),不是用[].
print(numbers)
numbers=list(range(1,100,11)) #用range函数还可以指定步长,例如此,从1开始,然后不断加11,直到达到或超过100
print(numbers)
xiaoma=[]
for xiaolei in range(1,1000,30):
haha=xiaolei**2 #(**2)表示乘方运算
xiaoma.append(haha) #append在末尾添加列表函数
print(xiaoma)
xiaohao=[]
for xiaolei in range(1,1000):
xiaohao.append(xiaolei**2) #append在末尾添加列表函数
print(xiaohao)
numbers=[1,100,85,5498,421]
print(min(numbers)) #对列表进行求均值、最大值、最小值等计算
print(max(numbers))
print(sum(numbers))
print(mean(numbers))
#列表解析
example=[numbers**2 for numbers in range(1,10)] #此时这行代码后面不需要冒号
print(example)
example=[li**2 for li in range(1,10,2)]
print(example)
#P52小练习
numbers=list(range(1,20)) #注意此处使用list()
print(numbers)
numbers=[number for number in range(1,20,2)]
print(numbers)
numbers=list(range(1,10000001))
print(numbers)
numbers=list(range(1,10000001))
for number in numbers:
print(number)
numbers=list(range(1,1000001))
print(min(numbers))
print(max(numbers))
print(sum(numbers))
numbers=list(range(1,21,2))
for number in numbers:
print(number)
numbers=list(range(3,31,3))
for number in numbers:
print(number)
numbers=[]
for number in range(1,11):
numbers.append(number**3)
print(numbers)
cube = [1 ** 3, 2 ** 3, 3 ** 3, 4 ** 3, 5 ** 3, 6 ** 3, 7 ** 3, 8 ** 3, 9 ** 3, 10 ** 3]
for value in cube:
print(value)
numbers=[value**3 for value in range(1,11)]
print(numbers)
#切片
hansome_boy=["wangjunkai","yiyangqianxi","wangyuan"]
print(hansome_boy[0:3]) #提取列表第一、二、三个元素,将起始索引指定为0,终止索引指定为3
print(hansome_boy[0])
print(hansome_boy[:2]) #没指定第一个索引,将自动从表头开始,将自动索引第一、二个元素
print(hansome_boy[1:]) #没指定终止索引,将从第二个元素开始索引到末尾的所有元素
print(hansome_boy[-2:]) #负数索引返回距离列表末尾相应距离的元素,如此处表示索引最后两个人
#遍历切片
hansome_boys=["wangjunkai","yiyangqianxi","wangyuan"]
for hansome_boy in hansome_boys[:]: #省略起始索引和结束索引即为全部索引
print(hansome_boy)
#复制列表
xiaohao=[1,3,4,5]
xiaoli=xiaohao[:]
print(xiaohao)
print(xiaoli)
xiaohao.append(9)
xiaoli.append(10)
print(xiaohao)
print(xiaoli)
#P57小练习
hansome_boys=["wangjunkai","yiyangqianxi","wangyuan","matianyu","sunhonglei"]
print("The first three items in list are:")
print(hansome_boys[:3])
print("Three items from the middle of the list are:")
print(hansome_boys[1:4])
print("The last three items in list are:")
print(hansome_boys[-3:])
Pizzas=["Italian pizza",'American pizza']
friend_pizzas=Pizzas[:]
Pizzas.append("chinese pizza")
print(Pizzas)
friend_pizzas.append("chinese pizza")
print(friend_pizzas)
print("My favorite pizza are:")
for Pizza in Pizzas:
print(Pizza)
print("My friend's favorite pizza are:")
for Pizza in friend_pizzas:
print(Pizza)
#元组 Python将不可修改的值称为不可变的,不可变的列表称为元组 用圆括号
dimension=(12,30)
dimension[0]=18 #此处操作被禁止,不能修改元组的元素
print(dimension[0])
#遍历元组的所有值
dimensions=(10,20,30,40,50,60,70,80,90)
for dimension in dimensions:
print(dimension)
#P59小练习
foods=("bread","rice","noddles","corn","dumplings")
for food in foods:
print(food)
foods=("bread","rice","noddles","corn","dumplings")
print(foods[1:])
#python中每级缩进都使用四个空格,一般用制表符缩进,不用空格
#使用Python时,谨记每行不超过80个字符(没有硬性规定,大家形成的通识)
?明天继续学习,加油!
|