"List" of Python 3 "zero foundation beginners from introduction to actual combat"

List

List is the most frequently used data type in Python language. List is one of ordered and variable sequences. The data types of elements in the same list can be different.

The list is a data set enclosed by []. Different data are separated by commas. Different data types can be in the list

For example:

songs = ["Thousands of miles away", 2020,("The first snow"), "That night",True,
         ["Hello","I'm fine","hello everyone"],{"name":"jinyan"}]

print(songs)

Output results of the above examples:

['Thousands of miles away', 2020, 'The first snow', 'That night', True, ['Hello', 'I'm fine', 'hello everyone'], {'name': 'jinyan'}]

Common list operations

(1) Get type -- type()

print(type(songs))

(2) Get list length -- len()

For example:

songs = ["Thousands of miles away", 2020,("The first snow"), "That night",True,
         ["Hello","I'm fine","hello everyone"],{"name":"jinyan"}]

print(len(songs))

Executing the above procedure will output the following results:

7

(3) Index to get a list element

For example:

songs = ["Thousands of miles away", 2020,("The first snow"), "That night",True,
         ["Hello","I'm fine","hello everyone"],{"name":"jinyan"}]

print(songs[0])
print(songs[-2])
print(songs[-1])
print(songs[999])

Executing the above procedure will output the following results:

Thousands of miles away
  File "E:/software/test/Pycharm-Workspace/lesson1/test.py", line 7, in <module>
    print(songs[999])
IndexError: list index out of range
['Hello', 'I'm fine', 'hello everyone']
{'name': 'jinyan'}

explain:

An error is reported when the index range is exceeded

(4) Slice gets multiple lists. The data type obtained by slicing: list

For example:

songs = ["Thousands of miles away", 2020,("The first snow"), "That night",True,
         ["Hello","I'm fine","hello everyone"],{"name":"jinyan"}]

print(songs[1:3])

Executing the above procedure will output the following results:

[2020, 'The first snow']

Operation of modifying list

Increase (increase)

 

 

(1) Indicates that an element -- append() is appended to the end of the list

For example:

songs = ["Thousands of miles away", 2020,("The first snow"), "That night",True,
         ["Hello","I'm fine","hello everyone"],{"name":"jinyan"}]
songs.append(["clap of thunder"])

print(songs)

Executing the above procedure will output the following results:

['Thousands of miles away', 2020, 'The first snow', 'That night', True, ['Hello', 'I'm fine', 'hello everyone'], {'name': 'jinyan'}, ['clap of thunder']]

(2) Indicates that the elements in one list are appended to the tail of another list (multiple elements are added at one time, and the list is merged) --- extend()

For example:

songs = ["Thousands of miles away", 2020,("The first snow"), "That night",True,["Hello","I'm fine","hello everyone"],{"name":"jinyan"}]

str = ["i","love","you"]

# songs.extend(["Jinglei", "I love Beijing Tiananmen Square"])
# print(songs)


songs.extend(str)
print(songs)

Executing the above procedure will output the following results:

['Thousands of miles away', 2020, 'The first snow', 'That night', True, ['Hello', 'I'm fine', 'hello everyone'], {'name': 'jinyan'}, 'i', 'love', 'you']

(3) Indicates where you can choose to add -- insert()

For example:

songs = ["Thousands of miles away", 2020,("The first snow"), "That night",True,
         ["Hello","I'm fine","hello everyone"],{"name":"jinyan"}]

songs.insert(1,"On the Jinshan mountain in Beijing")
print(songs)

Executing the above procedure will output the following results:

['Thousands of miles away', 'On the Jinshan mountain in Beijing', 2020, 'The first snow', 'That night', True, ['Hello', 'I'm fine', 'hello everyone'], {'name': 'jinyan'}]

Delete (delete)

(1) Clear the elements in the list -- clear()

For example:

['Thousands of miles away', 2020, 'The first snow', 'That night', True, {'name': 'jinyan'}]

Executing the above procedure will output the following results:

[]

(2) delete one by one according to the value -- remove()

For example:

songs = ["Thousands of miles away", 2020,("The first snow"), "That night",True,
         ["Hello","I'm fine","hello everyone"],{"name":"jinyan"}]

songs.remove( ["Hello","I'm fine","hello everyone"])
print(songs)

Executing the above procedure will output the following results:

['Thousands of miles away', 2020, 'The first snow', 'That night', True, {'name': 'jinyan'}]

(3) delete according to index -- pop()

For example:

songs = ["Thousands of miles away", 2020,("The first snow"), "That night",True,
         ["Hello","I'm fine","hello everyone"],{"name":"jinyan"}]

songs.pop(1)
print(songs)

Executing the above procedure will output the following results:

['Thousands of miles away', 'The first snow', 'That night', True, ['Hello', 'I'm fine', 'hello everyone'], {'name': 'jinyan'}]

Del has the same function as pop, but it can't be used now. Because del can't be restored after deletion, pop can be restored

 

Modify (modify) -- modify according to the index

For example:

#Modify element with index 0
songs = ["Thousands of miles away", 2020,("The first snow"), "That night",True,
         ["Hello","I'm fine","hello everyone"],{"name":"jinyan"}]

songs[0] = "ft HITA "
print(songs)

Executing the above procedure will output the following results:

['ft HITA ', 2020, 'The first snow', 'That night', True, ['Hello', 'I'm fine', 'hello everyone'], {'name': 'jinyan'}]

Query -- index()

The function is the same as that of find, except that if find cannot find, it returns - 1, and index cannot find an error

For example:

songs = ["Thousands of miles away", 2020,("The first snow"), "That night",True,
         ["Hello","I'm fine","hello everyone"],{"name":"jinyan"}]

print(songs.index(2020))

print(songs.index["kaixin"])

Executing the above procedure will output the following results:

1
Traceback (most recent call last):
  File "E:/software/test/Pycharm-Workspace/lesson1/test.py", line 6, in <module>
    print(songs.index["kaixin"])
TypeError: 'builtin_function_or_method' object is not subscriptable

Sort

(1) Ascending -- sort()

 

For example:

songs = [3,6,4,5]	
songs.sort()
print(songs)

Executing the above procedure will output the following results:

 Executing the above procedure will output the following results:

(2) reverse order -- reverse()

For example:

songs = [3,6,4,5]
songs.reverse()
print(songs)

Executing the above procedure will output the following results:

[5, 4, 6, 3]

Built in functions for lists

For example:

list1 =[12,45,99,23,40,67,34,11]
# num1 = max(list1)
print(max(list1))
list1 =[12,45,99,23,40,67,34,11]
# num1 = max(list1)
print(min(list1))
list1 =[12,45,99,23,40,67,34,11]
# num1 = max(list1)
print(sum(list1))
list1 =[12,45,99,23,40,67,34,11]
# num1 = max(list1)
print(len(list1))

Executing the above procedure will output the following results:

99
11
331
8

The list is very important in python. You must study hard. Next, let's learn from Yuanzu~

 

 

 

Tags: Python Programming software testing list

Posted by lutzlutz896 on Tue, 03 May 2022 10:24:43 +0300