1. Sequence Basis
Sequence: Python contains 6 built-in sequences, commonly used: lists, tuples, strings. Lists can be modified, tuples and strings cannot be modified.
Index: Increments from 0, gets an element by index, and can use a negative index, from right to left. The position number of the last 1 elements is-1;
s = ' Hello ' print (s[-1]) print (s[5])//error: Cannot get an element that does not exist by index; fourth = input (' year: ') [3]print (fourth)
Slices: Accesses a range of elements, returns a new sequence, does not change the original sequence, and separates two indexes by a colon.
"Left Index: Right index: Step" The left index cannot appear later than the right index. Does not include the index element on the right, the step default is 1, the element can be extracted from right to left for negative numbers.
S1 = ' Hello,world ' Print (s[0:5]) num = [1,2,3,4,5,6,7,8,9]print (num[:]) #通过切片的方式复制整个序列print (Num[::2]) print (Num[1::2]) Print (num[::-1]) #逆序显示序列
Sequence addition: Operations between sequences of the same type
Print (' Hello ' + ' world! ') Print ([1,2,3]+[4,5,6]) print ([1,2,3]+[' A ', ' B ', ' C ')
Multiplication:
Print (' * ' *10) print ([1,2,3]*10) print (*10)
Empty list: None is a python built-in value that represents null. Used to build an empty list and its initialization
LST = [None]*10print (LST)
Membership: In If True returns True, False for false
permissions = ' rw ' Print (' ro ' in permissions) Subject = ' $$$ Get rich now!!! $$$ ' Print (' $$$ ' not in subject) database = [[' Zyj ', ' a '], [' SL ', ' B ']]username = input ("User:") password = input (' pwd: ') I f [Username,password] in Database:print (' Login sucess! ')
Length, Min, max: Len, Min, max
NUM1 = [100,300,20]NUM2 = [1,2,3]t = (All-in-all) print (Len (NUM1)) print (min (NUM1)) print (min (num1,num2)) print (max (NUM1)) Print (max (t))
List function: Applies to all types of sequences;
Lst1 = List ("Hello") print (lst1) lst2 = List ("12345") print (LST2)
Basic list operations: changing lists, deleting elements
Element assignment, cannot be assigned to an element that does not exist
NUM3 = [1,5,6]num3[1] = 2print (num3)
Delete element: Del statement implementation
names = [' Zyj ', ' sl ', ' zyj ', ' py ']del names[2]print (names)
Shard Assignment:
name = List (' python ') name[2:]=list (' Game ') print (name)
Inserting a new element with a slice assignment: assigning values at the desired index position
name = [1,5]name[1:1] = [2,3,4]print (name)
Delete an element by assigning a value to a slice: Assign a null to the location you want to delete
name = [1,2,3,4,5]name[1:4] = []print (name)
In the above operation. When the step is not 1 o'clock, the insert operation cannot be performed and the non-equal length assignment cannot be made.
2. List Method
Basic Python Tutorial Chapter II