--python learning python programming: from entry to practice (Chapter 3)

3.1 List ([]). A digital or letter or name ect. Any element can be added to the list, or no relationship between the elements

= Bicycles [ 'Trek', 'Cannondale', 'Redline'] 
Print (Bicycles)

[ 'Trek', 'Cannondale', 'Redline']
access list element (list name [element index])
= Bicycles [ 'Trek', 'Cannondale', 'Redline'] 
Print (Bicycles [0])

Trek
result output neater use title () function (list name [element index] .title ())
Print (Bicycles [0] .title ()) 
Trek

element index starts from 0 instead of 1, the last element designated -1, -2 penultimate element. and so on, without knowing the length of the list you can access the last element
= Bicycles [ 'Trek', 'Cannondale', 'Redline'] 
Print (Bicycles [-1] .title ())
Print (Bicycles [0] .title ())
Print (Bicycles [. 1] .title ())

Redline
Trek
Cannondale

using a list of values of the individual values
bicycles = ['trek','cannondale','redline']
message = "this is a new "+bicycles[0].title() + "."
print(message)
this is a new Trek. 

Assignment:
3-1 Name: some friend's name is stored in a list, and call it names. In turn accesses each element in the list, so each friend's name are printed.
names = ["Bell","Peter","Alice","Ben"]
print(names[0])
print(names[1])
print(names[2])
print(names[3])
Bell
Peter
Alice
Ben


3-2 greeting: to continue to use the list of exercises 3-1, but does not print each friend's name, and print a message for each person. Each message contains the same greeting, but looked to the corresponding friend's name.
names = ["Bell","Peter","Alice","Ben"]
message = names[0]+ ", nice to meet you! "
print(message)
message = names[1]+ ", nice to meet you! "
print(message)
message = names[2]+ ", nice to meet you! "
print(message)
message = names[3]+ ", nice to meet you! "
print(message)
Peter, Nice to Meet you!
Alice, Nice to Meet you!
Ben, Nice to Meet you!

3-3 own list: Think about your favorite commuting, such as riding a motorcycle or drive a car, and create a more contained list of commuting. According to the list print a series of declarations on these commuting, such as "I would like to own a Honda motorcycle".
ways = ["bike","on foot","train"]
sayings = "I would like to own a Honda "+ ways[0]
print(sayings)
sayings = "I like "+ ways[1].title()
print(sayings)
sayings = "The "+ ways[-1] + " is fast!"
print(sayings)

I would like to own a Honda bike
I like  On Foot
The train is fast!
3.2 修改,添加和删除元素

修改列表元素
ways = ["bike","on foot","train"]
print(ways)
ways[0]="plane"
print(ways)
[ 'Bike', 'ON Foot', 'Train'] 
[ 'Plane', 'ON Foot', 'Train']

additive element

added to the end list
ways = ["bike","on foot","train"]
ways.append('plane')
print(ways)


['bike', 'on foot', 'train', 'plane']
ways = []
ways.append('plane')
ways.append('on foot')
ways.append('bike')
ways.append('train')
print(ways)

['plane', 'on foot', 'bike', 'train']

列表中插入元素

insert(元素索引,元素)
ways = ["bike","on foot","train"]
print(ways)
ways.insert(0,'plane')
print(ways)

['bike', 'on foot', 'train']
['plane', 'bike', 'on foot', 'train']


从列表中删除元素
del语句
Ways = [ "Bike", "ON Foot", "Train"] 
Print (Ways)
del Ways [0]
Print (Ways)

[ 'Bike', 'ON Foot', 'Train']
[ 'ON Foot', ' Train ']


pop () function pop (pop-up): the list is like a stack, the end of the list and delete elements equivalent to pop the top element, the storage time pop () to print the last

= Ways [ "Bike", "Foot ON", "Train"] 
Print (Ways)
poped_ways = ways.pop ()
Print (Ways)
Print (poped_ways)

[ 'Bike', 'ON Foot', 'Train']
[ 'Bike', 'ON Foot']
Train

remove elements based on the value
remove () this function has the wisdom to use it? When you do not know the position of elements to be deleted, and only know the value of the element to be deleted
= Ways [ "Bike", "Foot ON", "Train"] 
ways.remove ( "Train")
Print (Ways)

[ 'Bike', 'ON Foot']

Amazing! ! ! ! When we value removed from the list, you can then use the value
ways = ["bike","on foot","train"]
print(ways)
ways.remove("train")
too_fast = "train"
print(ways)
print("The " + too_fast +" is fast.")

['bike', 'on foot', 'train']
['bike', 'on foot']
The train is fast.

元素从列表中删除时,但是它还储存在too_fast 中

注意啦:::remove()只能删除第一个指定的值
ways = ["bike","on foot","train","train"]
print(ways)
ways.remove("train")
print(ways)

['bike', 'on foot', 'train', 'train']
['bike', 'on foot', 'train']


组织列表
永久性排序
sort(),按字母顺序排序,永久性修改,无法恢复
ways = ["bike","on foot","train","car"]
ways.sort()
print(ways)

['bike', 'car', 'on foot', 'train']

与字母顺序相反的排序,久性修改,无法恢复
ways.sort(reverse=True) 向sort()传递reverse=True
ways = ["bike","on foot","train","car"]
ways.sort(reverse=True)
print(ways)

临时排序

sorted()函数能按特定的顺序显示元素,不影响它们在列表中的原始排序

ways = ["bike","on foot","train","car"]
print("Here are the ways:")
print(ways)
print("\nHere are the ways:")
print(sorted(ways))
print("\nHere are the ways:")
print(ways)

Here are the ways:
['bike', 'on foot', 'train', 'car']
Here are the ways:
['bike', 'car', 'on foot', 'train']
 
Here are the ways:
['bike', 'on foot', 'train', 'car']
 
 与字母顺序相反,向sorted()传递reverse=True
 
ways = ["bike","on foot","train","car"]
print("Here are the ways:")
print(ways)
print("\nHere are the ways:")
print(sorted(ways,reverse=True))
print("\nHere are the ways:")
print(ways)

Here are the ways:
['bike', 'on foot', 'train', 'car']
Here are the ways:
['train', 'on foot', 'car', 'bike']
Here are the ways:
['bike', 'on foot', 'train', 'car']
 
倒着打印列表
reverse()只是反转列表元素序列,永久改变了元素顺序,但可恢复,需对列表再次调用reverse()
ways = ["bike","on foot","train","car"]
print(ways)
ways.reverse()
print(ways)

['bike', 'on foot', 'train', 'car']
['car', 'train', 'on foot', 'bike']

确定列表的长度
len()函数,列表的长度
ways = ["bike","on foot","train","car"]
print(len(ways))
4
 
 
 


Guess you like

Origin www.cnblogs.com/ljx12579/p/12535901.html