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
L. pop (4) L. pop (4) L. insert (4, 'Paul ') print (' replace element in List: (first) ', L) print ('-----------------------------------')
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)