Getting started with Python

Keywords: Programming Python Java shell

Getting started with Python


1, What is a list

In order to store the operation data conveniently, python provides some built-in data structures.

For example: list, tuple, dictionary, set, etc. The so-called "built-in" means that they are part of the python language,

So they can be used directly in the program.


1. Structure of list

python's list, equivalent to arrays in other programming languages.

>>> mylist = [1,2,3,4]

>>> mylist

[1, 2, 3, 4]


2. Features of the list

● all the data in the list are arranged in order, that is to say, the list belongs to the order type

● all the data in the list are indexes of two integer types, which can always be mapped to the unique data through indexing

● there can be duplicate data in the list

● any type of data can be saved in the list, and multiple types of data can be mixed for storage.

● the list can be dynamically scaled as required, that is, the system will dynamically allocate and reclaim memory as required. Therefore, there is no need to declare the capacity of the list in advance.


2, List creation

● any type of object can be stored in the list

Two ways to create a list

1. Use bracket []

L = ['python',19,True]
print (L)
['python', 19, True]

Empty list creation

L = []

2. Using list construction method

L = list(range(0,8,2))
print (L)
[0, 2, 4, 6]

Creation of empty list

L = list()
print (L)
[]


3, Addition, deletion, modification and query of list

1. List query operation

a) , index of elements in the list

Each element in the list has two integer type indexes

0 1 2 3 4 positive index

--------------------------

|a | b | c | d | e | list

--------------------------

- 5 - 4 - 3 - 2 - 1 negative index


b) , get the index of the specified element in the list

● index method

Looks up the specified index of the element, and throws a value error if the index does not exist. You can specify start and stop for the lookup index.

L = [1,2,6,9,4,6,7]
print (L.index(2))  # 1
print (L.index(6))  #2. The same element returns the index of the first element
print (L.index(11)) #ValueError: 11 is not in list, which will throw a value error
print (L.index(2,0,5)) #1. Find the index of element 2, starting from index 0 and ending at 5, but excluding 5
print (L.index(6,3,6)) #5. View the index of element 6, starting from index 0 and ending at 6, but excluding 6

c) , use [n] to get the elements in the list, but only one element can be obtained at a time

L = [1, 2, 6, 9, 4, 6, 7]
print (L[0])  #1. Get the element with index 0
print (L[-3]) #4. Get the element with index - 3
print (L[-8]) #IndexError: list index out of range if index does not exist.


d) , use elements in slices or lists to get multiple elements at a time

● syntax format [start:stop:step]

1. The resulting slice is still a list, a copy of the original list fragment

2. The resulting slice does not include the element corresponding to the index stop

3. If step is not specified, its default value is 1, and the syntax format can be simplified to [start:stop]

4. When step is a positive number,

If start is not specified, the first element of the slice defaults to the first element of the list

If stop is not specified, the last element of the slice defaults to the last element of the list

Calculating slices from index start

5. When step is negative,

If start is not specified, the first element of the slice defaults to the last element of the list

If stop is not specified, the last element of the slice defaults to the first element of the list

Count slices forward from index start


L = [5,6,3,1,9,8,4]
print (L[1:5]) # [6, 3, 1, 9]
print (L[::])  #[5, 6, 3, 1, 9, 8, 4] list all values
print (L[::-1]) #[4, 8, 9, 1, 3, 6, 5] list all values reversed
print (L[:3:-1]) #When [4, 8, 9] step is negative, if start is not specified, it is the last element, if stop is 3, it is index 3, and the step is 1
print (L[-3::-1]) #When [9, 1, 3, 6, 5] step is negative, if stop is not specified, it is the last element, start is the value corresponding to Index-3, and step is 1


● index slices are allowed to cross boundary

L = [5, 6, 3, 1, 9, 8, 4]
print (L[:100]) #[5, 6, 3, 1, 9, 8, 4] #The starting index is not specified. It defaults to the first element. However, if 100 exceeds the maximum index, the entire list will be printed
print (L[-100:]) #[5, 6, 3, 1, 9, 8, 4] #The end index is not specified, the default is the last element, and the start index is - 100 (- 7 the last negative index). Print all the lists


● call slice construction method

slice() list[slice(start,stop,step)]
L = [5, 6, 3, 1, 9, 8, 4]
print (L[slice(1,5,2)]) # [6, 1]
print (L[1:5:2]) # [6, 1]
print (L[slice(None,None,None)]) #[5, 6, 3, 1, 9, 8, 4], the default value is None
print (L[::]) #[5, 6, 3, 1, 9, 8, 4]

e) , 'in' or 'not in' check whether the element exists in the list

L = [5, 6, 3, 1, 9, 8, 4]

in
print (3 in L) #True if present
True
print (11 in L) #Does not exist is False
False

not in
print (1 not in L) 1 In the list, not in Return to False
False
print (11 not in L) 11 Does not exist in the list, in Return to True
True


2. Add list

a) , using the append() function

'''
append()Method, adding a value at the end
'''
L = [1,2,3,4]
L.append(5)
print (L)
#[1, 2, 3, 4, 5]
L.append([7,8])
print (L)
#[1, 2, 3, 4, 5, [7, 8]]


b) , use the extend() method to add all elements to the end

'''
Call extend method, list.extend([]) parentheses with square brackets
'''
L = [1,2,3,4]
L.extend([1,2])
print (L)
[1, 2, 3, 4, 1, 2]


c) , call the insert method

'''
Call the insert method to insert the value into the specified location
'''
L = [1,2,3,4]
L.insert(1,6)
print (L)
#[1, 6, 2, 3, 4]
#At the end of the list
L.insert(len(L),10)
print (L)
#[1, 6, 2, 3, 4, 10]


d) , assign a new value to the specified slice

L = [2,3,4,1]
L[2:2] = [5,6]
print (L)
L = [2,3,4,1]
L[len(L):] = [7,8]
print (L)


3. List change operation

There are two common ways to modify elements in a list

a) , assign a new value to the element of the specified index (only one element can be modified at a time)

'''
Change the element value corresponding to index 1 to 9, and only modify one value at a time
'''
L = [3,2,0,1,2,1]
L[1] = 9
print (L)
[3, 9, 0, 1, 2, 1]


b) , specify multiple indexes, modify multiple elements at a time


'''
Modify the value of element index 1:4, and use []
The index value obtained on the left can be different from the number given on the right
'''
L = [3,2,0,1,2,1]
L[1:4] = [4,5,6]
print (L)
[3, 4, 5, 6, 2, 1]
L[1:4] = [3,7]
print (L)
[3, 3, 7, 2, 1]
L[2:3] = [9]
print (L)
[3, 3, 9, 2, 1]


4. Delete list

a) , call the method remove to delete only one element at a time. If there are the same elements, delete the first one

L = [2,3,4,5,6]
L.remove(3)
print (L)
L = [3,4,3,1,2]
L.remove(3)
print (L)


b) , call the method pop to delete only one element of the specified index at a time.

If the specified index does not exist, an index error will be thrown and the index is out of bounds. If you do not specify an index for pop deletion, the last element in the list is deleted.

L = ['a','b','c','d','e']
print (L.pop(1)) #b, pop actually has a return value
print (L) # ['a', 'c', 'd', 'e']
print (L.pop())
print (L) #['a', 'b', 'c', 'd']


c) , delete at least one or more elements at a time using the del statement

L = [1,2,3,4,5,6]
del L[1]
print (L)
del L[1:3]
print (L)


d) , assign an empty list to the specified slice, and delete at least one or more elements at a time

L = [1,2,3,4,5,6,7,8]
L[2:3] = []
print (L) #[1,2,3,5,6,7,8]
L[3:6] = []
print (L) ##[1,2,3,8]


e) , call the method clear to clear the list

L = [1,2,3,4,5]
L.clear()
print (L) #[]


4, Using addition and multiplication to operate on lists

1. Using addition to operate on the list will affect the list itself when using the + = operator

L1 = [1,2]
L2 = [3,4]
L3 = L1 + L2
print (L1,L2,L3) #Add between lists [1, 2] [3, 4] [1, 2, 3, 4]


L1 = L2 = [1,2]
L1 += [3,4]
print (L1,L2) #Using the + = operator also affects L2 [1, 2, 3, 4] [1, 2, 3, 4]
L1 = [1,2]
L2 = L1[:]
L1 += [3,4]
print (L1,L2) #[1, 2, 3, 4] [1, 2]


2. Use multiplication to operate on the list. Using the * = operator will affect the list itself

L1 = [1,2,3]
L2 = [4,5,6]
L1 = L1 * 2
print (L1) #[1, 2, 3, 1, 2, 3]

L1 = L2 = [1,2]
L1 *= 2
print (L1,L2) #[1, 2, 1, 2] [1, 2, 1, 2]

L1 = [0]
L1 *= 5
print(L1) #[0, 0, 0, 0, 0] can be used to create a list of specified elements


5, List inversion

1. Call the method reverse to reverse all elements in the list

L = [1,2,3,4,5]
L.reverse()
print (L)


2. Call the built-in function reversed. The list reversed by reversed does not change. The returned value of reversed is an iterator object

L = [1,2,3,4,5]
L1 = reversed(L)
print (L1)  #<list_reverseiterator object at 0x7f8f070f1390>
print (list(L1)) #[5, 4, 3, 2, 1]


6, Sort list

1. Call method sort()

#sort() affects the list itself
L2 = ['java','python','shell']
L2.sort(key = len)
print(L2)  #Sort by string length, ['java ',' shell ',' python ']
L2 = [1,3,4,2,6,9,8,7]
L2.sort()
print(L2) #[1, 2, 3, 4, 6, 7, 8, 9]


2. Call the built-in function sorted()

sorted(),No impact on the list itself
L2 = ['java','shell','python']
print(sorted(L2,key = str.upper)) #['java', 'python', 'shell']

L2 = [1,3,4,2,6,9,8,7]
print(sorted(L2)) #[1, 2, 3, 4, 6, 7, 8, 9]
print(L2) #[1, 3, 4, 2, 6, 9, 8, 7]


Posted by LacOniC on Sun, 29 Dec 2019 09:47:14 -0800