List and tuple types in Python

Source: Internet
Author: User

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(‘-----------------------------------‘)
    • The second Kind
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.