In Python, the List and Tuple types are pythonlisttuple.

Source: Internet
Author: User

In Python, the List and Tuple types are pythonlisttuple.

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 ('create list: ', L) print ('-----------------------------------')
# Access the list by index
Print ('Access list by index: ', L [3]) print ('-----------------------------------')
# Reverse access list
Print ('reverse access list: ', L [-6]) print ('-----------------------------------')
# Add new elements to the List
L. insert (0, 'Paul ') print (' Add a new element to the List: ', L) print ('-----------------------------------')
# Deleting an element from a list
L. pop (2) print ('list Delete element: ', L) print ('-----------------------------------')
# Replacing elements in the List
  • Bart is about to transfer to another school. A Paul came along. to update the class member list, we can delete the Bart and add Paul.

  • First

L. pop (4) L. pop (4) L. insert (4, 'Paul ') print (' replace element in List: (first) ', L) print ('-----------------------------------')
  • Second
L [3] = 'pa' print ('replace the element in the list' (Type 2) ', L) print ('-----------------------------------')
  • Exercise: the rank of the students in the class is as follows: L = ['Adam ', 'lisa', 'bart'] However, after an exam, the Bart students accidentally won the first place, adam took the last one. Please assign values to the index of the list to generate a new ranking.
L = ['Adam ', 'lisa', 'bart'] L [0] = 'bart' L [2] = 'Adam 'print ('new rank :', l) print ('-----------------------------------')
Create a tuple
  • Tuple is another ordered list. The only difference between creating a tuple and creating a list is that [] is replaced by ().
T = ('Adam ', 'lisa', 'bart') print ('create tuple: ', t) print ('-----------------------------------')
# Creating a single-element tuple
T = ('Adam ', 'lisa', 'bart',) print ('create a single element tuple: ', t) print ('-----------------------------------')
# Variable tuple
T = ('A', 'B', ['A', 'B']) L = t [2] L [0] = 'X' L [1] = 'y' print ('mutable tuple', t) print ('-----------------------------------')
# Tuple defined:
  • T = ('A', 'B', ['A', 'B']). Because t contains a list element, tuple content is variable. Can I modify the above Code to make tuple content immutable?
T = ('A', 'B', ('A', 'B') print ('make tuple content unchangeable: You can change braces to parentheses ', t)

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.