a = ‘python‘print(‘hello,‘, a or ‘world‘)b = ‘‘print (‘hello,‘, b or ‘world‘)print(‘-----------------------------------‘)
Create List
L = [‘Adam‘,95.5,‘Lisa‘,85,‘Bart‘,59]print(‘创建list:‘,L)print(‘-----------------------------------‘)
#按照索引访问list
print(‘按照索引访问list:‘,L[3])print(‘-----------------------------------‘)
#倒序访问list
print(‘倒序访问list:‘,L[-6])print(‘-----------------------------------‘)
#向List添加新元素
L.insert(0,‘Paul‘)print(‘向List添加新元素:‘,L)print(‘-----------------------------------‘)
#从list删除元素
L.pop(2)print(‘list删除元素:‘,L)print(‘-----------------------------------‘)
#在List中替换元素
Bart classmate to transfer away, happened to a Paul classmate, to update the class membership list, we can first delete Bart, and then add Paul in.
First Kind
L.pop(4)L.pop(4)L.insert(4,‘Paul‘)print(‘在List中替换元素:(第一种)‘,L)print(‘-----------------------------------‘)
L[3] = ‘Paul‘print(‘在List中替换元素:(第二种)‘,L)print(‘-----------------------------------‘)
- Exercise: The class's classmates ranked according to the score is this: L =[' Adam ', ' Lisa ', ' Bart ' but after an exam, Bart's classmates accidentally made the first, while Adam's classmates scored the bottom of the test. Create a new rank by assigning a value to the index of the list.
L = [‘Adam‘,‘Lisa‘,‘Bart‘]L[0] = ‘Bart‘L[2] = ‘Adam‘print(‘新的排名:‘,L)print(‘-----------------------------------‘)
Create a tuple
- A tuple is another ordered list. The only difference between creating a tuple and creating a list is to replace [] with ().
t = (‘Adam‘, ‘Lisa‘, ‘Bart‘)print(‘创建tuple:‘,t)print(‘-----------------------------------‘)
#创建单元素tuple
t = (‘Adam‘,‘Lisa‘,‘Bart‘,)print(‘创建单元素tuple:‘,t)print(‘-----------------------------------‘)
# "Variable" tuple
t = (‘a‘,‘b‘,[‘A‘,‘B‘])L = t[2]L[0] = ‘X‘L[1] = ‘Y‘print(‘“可变”的tuple‘,t)print(‘-----------------------------------‘)
#定义了tuple:
- t = (' A ', ' B ', [' A ', ' B ']), because t contains a list element, which causes the contents of a tuple to be mutable. Can you modify the above code so that the tuple content is not mutable?
t = (‘a‘,‘b‘,(‘A‘,‘B‘))print(‘让tuple内容不可变:可以把大括号改为小括号‘,t)
List and tuple types in Python