Python learning content. 04

Source: Internet
Author: User

The main contents of this section:
1. List 2. List of additions and deletions to change 3. Nested 4 of the list. Tuples and tuples are nested 5. Range
I. The list of List 1.1 lists is one of the underlying data types of Python, and other programming languages have similar data types. such as the array in JS, the array in Java, and so on. It is enclosed in [], each element is separated by ', ' and can hold various data types: LST = [1, ' haha ', ' roar ', [1,8,0, ' Baidu '], ("I", "called", "Yuan", "group"), "abc", {"My Name": "Dict Dictionary"},         {"My name is set", "collection"}] The list is compared to a string. Not only can you store different data types. And can store a lot of data. 32-bit Python can be stored in: 536,870,912 elements, 64 bits can be stored: 1.,152,921,504,606,85e,+18 elements. And the list is ordered (in the order you saved), indexed, and can be sliced for easy value.
The index of the 2.2 list and the slice list have the same index as the string: LST = ["Twist Vine", "Wang Jianlin", "Ma Yun", "Zhou Medical", "Huaqiang"] print (lst[0]) # Get first element print (Lst[1]) pr Int (lst[2])
LST[3] = "Flow Strong" # Note. The list can be changed. This is not the same as the string. Print (LST) # [' Twist Vine ', ' Wang Jianlin ', ' Ma Yun ', ' Flow strong ', ' to Huaqiang ']
S0 = "to Huaqiang" s0[1] = "Beauty" # TypeError: ' str ' object does not the support item assignment does not allow changes to the print (S0) List of slices: LST = ["Twist Vine", "Wang Jianlin", "Ma Yun", "Zhou Medical", "Huaqiang"] print (Lst[0:3]) # [' Twist Cane ', ' Wang Jianlin ', ' Ma Yun '] print (Lst[:3]) # [' Twist Vine ', ' Wang Jianlin ', ' Ma Yun ']
Print (Lst[1::2]) # [' Wang Jianlin ', ' Zhou Medical '] also has step print (Lst[2::-1]) # [' Ma Yun ', ' Wang Jianlin ', ' twist cane '] can also be inverted
Print (lst[-1:-3:-2]) # Backward with step two. List additions and deletions
1. Increase, note that list and STR are not the same. LST can change. So it's working directly on the original object.
LST = ["Twist Vine", "JJ Lin", "Chow Yun-Fat", "Zhou Zhijo"]
Print (LST) lst.append ("Wusir") print (LST)
LST = [] While true:content = input ("Please enter the employee information you want to input, enter Q to exit:") if content.upper () = = ' Q ': Break Lst.append (c ontent) print (LST)
LST = ["Twist Vine", "Zhangdezhong", "Condefort"] lst.insert (1, "Andy Lau") # Insert Andy Lau in 1 position. The original element moves backward one print (LST)
# iteration Add lst = ["Wang Zhiwen", "Zhang Shan", "The Abyss of Misery"] lst.extend (["Twist Cane", "Twist No Pain"]) print (LST)
2. Delete
Pop, remove, clear, del lst = ["Twist Vine", "Wang Jianlin", "Li Ka-shing", "King Rich"] Print (LST) deleted = Lst.pop () # Delete last print ("deleted", D eleted) print (LST)
el = Lst.pop (2) # delete element number 2nd print (EL) print (LST)
Lst.remove ("Twist Vine") # delete the specified element print (LST) # lst.remove ("haha") # Delete the nonexistent element will error # # print (LST)
Lst.clear () # Empty list print (LST)
# slice Delete del Lst[1:3] Print (LST)
3. Modifications

Index Tile Modification # Modify LST = ["Taibai", "Too Black", "colored", "Silver King", "Day Day"] lst[1] = "Too dirty" # change the number 1th element to too dirty print (LST)
Lst[1:4:3] = ["Twist Cane", "wow"] # slice modification is OK. If the step size is not 1, be careful. Number of elements print (LST)
Lst[1:4] = ["Li Ka-shing turtle Son"] # if the slice has no step size or the stride size is 1. Do not care about the number of print (LST)
4. Query, list is an iterative object, so you can make a for loop
For El in Lst:print (EL)
5. Other operations
LST = ["Taibai", "Too Black", "colored", "Silver King", "Day Day", "Taibai"] C = Lst.count ("Taibai") # Query the number of occurrences of Taibai print (c)
LST = [1, one, 2] lst.sort () # Sort. Default ascending print (LST) lst.sort (reverse=true) # Descending print (LST)
LST = ["Taibai", "Too Black", "colored", "Silver King", "Day Day", "Taibai"] Print (LST) lst.reverse () print (LST)
L = Len (LST) # List length print (l)
Three. The list is nested with a reduced-dimension operation. One layer at a glance.

LST = [1, "Taibai", "Wusir", ["Sloppy Pain", ["Coca-Cola"], "Wang Jianlin"]
# Find Wusir print (lst[2])
# Find Taibai and Wusir print (Lst[1:3])
# Find White white print (lst[1][1])
# Get the Wusir. Then capitalize the initial letter. Throw it back. S = lst[2] s = s.capitalize () lst[2] = s print (LST) # abbreviated LST[2] = Lst[2].capitalize () print (LST)
# change Taibai to too black lst[1] = Lst[1].replace ("White", "Black") print (LST)
# change sloppy pain into a horse-ache
Lst[3][0] = Lst[3][0].replace ("Tiger", "the") print (Lst[3][0])
Lst[3][1].append ("Sprite") print (LST)
Four. Tuples and tuples nested tuples: Commonly known as immutable lists. Also be read-only list, tuple is also one of Python's basic data types, enclosed in parentheses, which can put any data type of data, query can. Loops are also possible. Slices are also available. But it just can't be changed.
Tu = (1, "Taibai", "Li Bai", "Too Black", "how Black") Print (TU)
Print (tu[0]) print (tu[2]) print (Tu[2:5]) # after slice or tuple
# for Loop traversal tuple for El in Tu:print (EL)
# try to modify tuple # Tu[1] = "Sloppy Pain" # error ' tuple ' object does not support item assignment
Tu = (1, "haha", [], "hehe") # Tu[2] = ["Fdsaf"] # so change it.
Tu[2].append ("Twist Cane") # can be changed. No error Tu[2].append ("Wang Jianlin") print (TU) about immutable, note: The immutable meaning of tuples here is that the child elements are immutable. Child elements inside a child element can be changed, depending on whether the child element is a mutable object.
If there is only one element in the tuple. Be sure to add a comma, otherwise it's not a tuple
Tu = (1,) the Print (Type (TU)) tuple also has count (), index (), Len (), and other methods. You can test it yourself.
Five. Range range can help us get to a set of data. This data can be obtained through a for loop.
For NUM in range: print (num)
For NUM in range (1, 2): Print (num)
For NUM in range (10, 1,-2): # reverse, same as slice print (num)

Job Content:

1, write code, such as the following list, as required to achieve each function

Li = ["Alex", "Wusir", "Ritian", "Barry", "Wenzhou"]
#1) Calculates the length of the list and outputs
#print (Len (LI))
#2) Append the element "seven" to the list and output the added list
#li. Append ("seven")
#print (LI)
3) Insert the element "Tony" in the 1th position of the list and output the added list
Li.insert (1, "Tony")
Print (LI)
4) Please modify the element in the 2nd position of the list to "Kelly" and output the modified list
li[1]= "Kelly"
5) Add each element of the list L2 = [1, "A", 3, 4, "heart"] to the list Li, one line of code is implemented, and the loop is not allowed to add.
L2 = [1, "A", 3, 4, "Heart"]
Li.extend ([L2])
Print (LI)
6) Add each element of the string s = "Qwert" to the list Li, one line of code is implemented, and the loop is not allowed to add.
s = "Qwert"
Li.extend ([s])
Print (LI)
7) Remove the element "Eric" from the list and output the added list
Li.remove ("Eric")
Print (LI)
8) Delete the 2nd element in the list and export the deleted element and the list after the element is deleted
S=li.pop (2)
Print (s)
Print (LI)
9) Delete the 2nd to 4th element in the list and output the list after the delete element
Del Li[2:4]
Print (LI)
10) Invert all elements of the list and output the inverted list
Li.reverse ()
Print (LI)
11) Calculate the number of occurrences of the "Alex" element in the list Li and output the number of times.
Print (Li.count ("Alex")) 1 plays
2, write code, such as the following table, using slices to achieve each function

Li = [1, 3, 2, "a", 4, "B", 5, "C"]
1) Create a new list by slicing the Li list l1, L1 = [1, 3, 2]
L1=li[0:3]
Print (L1)
2) Create a new list by slicing the Li list L2, L2 = ["A", 4, "B"]
L1=li[3:6]
Print (L1)
3) Create a new list L3 by slicing the Li list, L3 = ["1,2,4,5]
L1=LI[0::2]
Print (L1)
4) Create a new list by slicing the Li list l4, L4 = [3, "A", "B"]
L1=li[1:6:2]
Print (L1)
5) Create a new list L5 by slicing the Li list, l5 = ["C"]
L1=LI[7]
Print (list (L1))
6) Create a new list by slicing the Li list L6, L6 = ["B", "A", 3]
L1=li[5:0:-2]
Print (L1)
3, write code, such as the following table, as required to achieve each function.
Lis = [2, 3, "K", ["qwe", [+], ["K1", ["TT", 3, "1"]], [], "AB", "ADV"]
1) Capitalize the "TT" in the LIS list (in two ways).
01..lis[3][2][1][0]=lis[3][2][1][0].upper ()
Print (LIS)
02.lis[3][2][1][0]=lis[3][2][1][0].swapcase ()
Print (LIS)

2) Change the number 3 in the list to the string "100" (in two ways).
01.lis[1]= "100"
lis[3][2][1][1]= "100"
Print (LIS)
02.lis=str (LIS). Replace ("3", "' 100 '")
Print (LIS)

3) Change the string "1" in the list to the number 101 (in two ways).
LIS[3][2][1][2]=101: ""
Print (LIS)
‘‘‘
02:lis=str (LIS). Replace ("' 1 '", "101")
Print (LIS)
4, please use code: LI = ["Alex", "Eric", "Rain"]
Use underscores to stitch each element of a list into a string "Alex_eric_rain"
li[0:3]=["Alex_eric_rain"]
Print (LI)
5, use the For loop and range to print out the index of the following list. Li = ["Alex", "Wusir", "Ritian", "Barry", "Wenzhou"]
A=len (LI)
For Li in range (0,a):
Print (LI)
6, use the For loop and range to find all the even numbers within 100 and insert those even into a new list.
for s in range (2,100,2):
A = List (range (2, 100, 2))
Print (a)
7, using the For loop and range
Find the number within 50 that can be divisible by 3 and insert these numbers into a new list.
for s in range (3,50,3):
A=list (range (3,50,3))
Print (a)
8, using the For loop and range from 100
, reverse print.
for s in range (100,1,-1):
A=list (range (100,1,-1))
A.reverse ()
Print (a),
For LIS in range (100,10,-2):
9, using the For loop and range from 100~10, reverse the addition of all the even to a new list and then filter the list of elements, will be divisible by 4 of the number left.
A=list (range (100,10,-2))
Index=0
While Index<len (a):
If a[index]%4==0:
Print (A[index])
Index=index+1
Else
Index=index+1
10, using the For loop and range, 1-30
The number is added to a list at once, and the list is looped, and the number divisible by 3 is changed to *.
For LIS in range (1,30):
Lis=list (range (1,30))
Index=0
While Index<len (LIS):
If lis[index]%3==0:
Print (Lis[index])
Index=index+1
Else
Index=index+1





11, find the elements in the list Li, remove the spaces for each element, and find all the elements that start with "a" or "a" and End with "C", and add them to a new list.
The final loop prints the new list. Li = ["Taibai", "Alexc", "AbC", "Egon", "Ritian", "Wusir", "AQC"]
B=[]
For a in Li:
S=a.strip ()
if (S.startswith ("a") or S.startswith ("a")) and S.endswith ("C"):
B.append (s)
For x in B:
Print (x)

12, develop the sensitive word filter program, prompting the user to enter the comment content, if the user input content contains special characters:
List of sensitive words
Li = ["Cang teacher", "Tokyo Fever", "Enrique", "Yui Hatano")
Replace the sensitive words in the user input with the same length of * (replace * *) and add them to a list, and add them directly to the list above if the user enters the content without a sensitive word.


13, like the following table
Li = [1, 3, 4, "Alex", [3, 7, 8, "Taibai"], 5, "Ritian"]
Loop through each element in the list, and then loop through the elements in the list.
The result I want is:
1
3
4
"Alex"
3
7,
8
"Taibai"
5

Python learning content. 04

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.