《Python编程从入门到实践》学习笔记04操作列表

#遍历整个列表
magicians=['alice','david','caeolina']
for magician in magicians:
    print(magician)

alice
david
caeolina

#在for循环中执行更多操作
magicians=['alice','david','caeolina']
for magician in magicians:
    print(f"{magician.title()},that was a great trick!")

Alice,that was a great trick!
David,that was a great trick!
Caeolina,that was a great trick!

magicians=['alice','david','caeolina']
for magician in magicians:
    print(f"{magician.title()},that was a great trick!")
    print(f"I can't wait to see your next trick,{magician.title()}.\n")

Alice,that was a great trick!
I can’t wait to see your next trick,Alice.

David,that was a great trick!
I can’t wait to see your next trick,David.

Caeolina,that was a great trick!
I can’t wait to see your next trick,Caeolina.

magicians=['alice','david','caeolina']
for magician in magicians:
    print(f"{magician.title()},that was a great trick!")
    print(f"I can't wait to see your next trick,{magician.title()}.\n")
print('Thank you,everyone.that was a great magical show!')

Alice,that was a great trick!
I can’t wait to see your next trick,Alice.

David,that was a great trick!
I can’t wait to see your next trick,David.

Caeolina,that was a great trick!
I can’t wait to see your next trick,Caeolina.

Thank you,everyone.that was a great magical show!

#range()
#list()

#range(x)=range(0,x)
#range(x,y)
#range(x,y,z)
list(range(6))

[0, 1, 2, 3, 4, 5]

list(range(1,6))

[1, 2, 3, 4, 5]

list(range(0,6,2))

[0, 2, 4]

list(range(0,6,3))

[0, 3]

#使用range()函数创建数字列表
even_numbers=list(range(2,11,2))
print(even_numbers)

[2, 4, 6, 8, 10]

odd_numbers=list(range(1,11,2))
print(odd_numbers)

[1, 3, 5, 7, 9]

#任务:创建1个列表,包含1-10的平方
squres=[]
for value in range(1,11):
    squres.append(value**2)
print(squres)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

#对数字列表进行简单的统计计算
digits=[1,2,3,4,5,6,7,8,9,0]
max(digits)

9

digits=[1,2,3,4,5,6,7,8,9,0]
min(digits)

0

digits=[1,2,3,4,5,6,7,8,9,0]
sum(digits)

45

#列表解析
squares=[value**2 for value in range(1,11)]
print(squares)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

#使用列表的一部分
players=['charles','martina','michael','florence','eli']
print(players[0])

charles

players=['charles','martina','michael','florence','eli']
print(players[0:3])

[‘charles’, ‘martina’, ‘michael’]

players=['charles','martina','michael','florence','eli']
print(players[1:4])

[‘martina’, ‘michael’, ‘florence’]

players=['charles','martina','michael','florence','eli']
print(players[:4])

[‘charles’, ‘martina’, ‘michael’, ‘florence’]

players=['charles','martina','michael','florence','eli']
print(players[2:])

[‘michael’, ‘florence’, ‘eli’]

#负数索引
players=['charles','martina','michael','florence','eli']
print(players[-3:])

[‘michael’, ‘florence’, ‘eli’]

#每隔两个元素就提取1个
players=['charles','martina','michael','florence','eli']
print(players[0:4:2])

[‘charles’, ‘michael’]

#遍历切片
players=['charles','martina','michael','florence','eli']
print('Here are the first three players on my team:')
for player in players[0:3]:
    print(player.title())

Here are the first three players on my team:
Charles
Martina
Michael

#复制列表
my_foods=['pizza','falafel','carrot cake']
friends_foods=my_foods[:]

print("My favourite foods are:")
print(my_foods)

print("\nMy favourite foods are:")
print(friends_foods)

My favourite foods are:
[‘pizza’, ‘falafel’, ‘carrot cake’]

My favourite foods are:
[‘pizza’, ‘falafel’, ‘carrot cake’]

my_foods=['pizza','falafel','carrot cake']
friends_foods=my_foods
#这样会导致两个列表一模一样
del friends_foods[0]
print("My favourite foods are:")
print(my_foods)

print("\nMy favourite foods are:")
print(friends_foods)

My favourite foods are:
[‘falafel’, ‘carrot cake’]

My favourite foods are:
[‘falafel’, ‘carrot cake’]

#元组-不可变的列表
dimensions=(200,50)
print(dimensions[0])
print(dimensions[1])

200
50

dimensions=(200,)
dimensions[0]=250
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_15476\341576722.py in <module>
      1 dimensions=(200,)
----> 2 dimensions[0]=250

TypeError: 'tuple' object does not support item assignmen
dimensions=(200,50)
for dimension in dimensions:
    print(dimension)

200
50

#修改元组变量
dimensions=(200,50)
print('Original dimensions:')
for dimension in dimensions:
    print(dimension)
    
dimensions=(400,100)
print('\nModified dimensions:')
for dimension in dimensions:
    print(dimension)

Original dimensions:
200
50

Modified dimensions:
400
100

你可能感兴趣的:(python,python,笔记)