Python practices (four lists and tuples) and python practices
After a period of learning, I learned the list and tuples of python. This part is the basis of the subsequent functions, which is also the part of learning in any language. In fact, the process is very hard, but it is not a bad thing for me, I do not understand it anyway, it does not have any psychological burden. Think about what you have learned. The accumulation at this time is solid in every step.
List, tuples
A list is one of the most common data types in the future. You can use a list to conveniently store and modify data.
Definition list
Names = ["A", "B", "C", "D"]
Access the elements in the list by subscript. The subscript starts counting from 0.
Names [0] # The result is
Names [2] # The result is C.
Names [-1] # The result is D.
Names [-2] # The result is C.
Slice: Multiple Elements
>>> Names = ["Alex", "Tenglan", "Eric", "Rain", "Tom", "Amy"]
>>> Names [] # obtain numbers between subscript 1 and subscript 4, including 1, not 4
['Tenglanc', 'Eric ', 'rain']
>>> Names [1:-1] # obtain the values from subscript 1 to-1, excluding-1.
['Tenglanc', 'Eric ', 'rain', 'Tom']
>>> Names [0: 3]
['Alex ', 'tenglanc', 'Eric']
>>> Names [: 3] # If it is retrieved from the beginning, 0 can be ignored. The effect is the same as that in the previous sentence.
['Alex ', 'tenglanc', 'Eric']
>>> Names [3:] # If you want to get the last one, you must not write-1.
['Rain', 'Tom ', 'amy']
>>> Names [3:-1] # in this way,-1 will not be included
['Rain', 'Tom ']
>>> Names [0: 2] #2 is the representative. Every other element takes one
['Alex ', 'Eric', 'Tom ']
>>> Names [: 2] # the same effect as the previous sentence
['Alex ', 'Eric', 'Tom ']
Insert
Names = ["A", "B", "C", "D"]
Names. append ("E ")
Names. insert (1)
>>> A, E, B, C, D
Modify
Names = ["A", "B", "C", "D"]
Names [2] = "E"
>>> A, B, E, D
Delete
Names = ["A", "B", "C", "D"]
Method 1: names. remove ("")
Print (names)
The output values are B, C, and D.
Method 2: del names [0]
Print (names)
The output values are B, C, and D.
Names. pop (0)
Print (names)
The output value is B, C, D #. If pop () does not write a number, the last one is deleted by default.
Statistics
Names = ["A", "B", "C", "D"]
Print (nams. count (C ))
Clear
Names = ["A", "B", "C", "D"]
Names. clear ()
Print (names)
The list is cleared.
Reverse
Names = ["A", "B", "C", "D"]
Names. reverse ()
Pritnt (names)
>>> D, C, B,
Sort
>>> Names
['Alex ', 'amy', 'amy ', 'tenglanc', 'Tom', '1', '2', '3']
>>> Names. sort ()
>>> Names
['1', '2', '3', 'Alex ', 'amy', 'amy ', 'tenglanc', 'Tom']
Extension
>>> Names
['Alex ', 'tenglanc', 'rain', 'Tom', 'amy ']
>>> B = [1, 2, 3]
>>> Names. extend (B)
>>> Names
['Alex ', 'tenglanc', 'rain', 'Tom', 'amy ', 1, 2, 3]
Tuples
The number of tuples is similar to that of the list. It is also called the read-only list.
Syntax:
names
=
(
"A"
,
"B"
,
"C"
)It only has two methods: count and index. Program exercises
Program: Shopping Cart program
Requirements:
Product_list = [('phone ', 5800 ),
('Mac Pro', 9800 ),
('Bike', 800 ),
('Watch ', 10600 ),
('Coffee ', 31)]
Shopping_list = []
Salary = input ("input your salary :")
If salary. isdigit ():
Salary = int (salary)
While True:
For index, item in enumerate (product_list ):
Print (index, item)
User_choice = input ("Please Select Product :")
If user_choice.isdigit ():
User_choice = int (user_choice)
If user_choice <len (product_list) and user_choice> = 0
P_item = product_list [user_choice]
If p_item [1] <= salary: # Buy
Shopping_list.apeend (p_item)
Salary-= p_item (1)
Print ("Added % s into shopping cart, your current balance is \ 033 [31; 1 m % s \ 033 [0 m" % (p_item, salary ))
Else:
Print ("\ 033 [41; 1 m your balance is only [% s], you cannot purchase \ 033 [0 m" % alary)
Elif user_choice = 'Q'
Ptint ("-------- shopping list ------------")
For p in shopping_list:
Print (p)
Print ("Your current balance:", salary)
Exit ()
Else
Print ("invalid option ")
Summary: The focus of this chapter is the list. No matter what language you want to learn, it is essential to read the list several times. We recommend that you read the list several times, rather slow down and lay a solid foundation.