Python Daily Note punch in_ day4

Posted by wcl99 on Sat, 22 Jan 2022 10:43:51 +0100

Day 4 Tuesday, January 18, 2022 - basic method of ordered sequence

I Addition of list elements

append() method -- the new elements of this method are added at the end of the list

List name Append (new data)

Chestnuts:

>>> name = ['Xia Xia','Yaqi']
>>> name.append("like")
>>> print(name)
['Xia Xia', 'Yaqi', 'like']

extend() method -- the new elements of this method are added to the end of the list

List name Extend (new data)

Chestnuts:

>>> name = ["Yaqi","like"]
>>> name.extend('Xia Xia')
>>> print(name)
['Yaqi', 'like', 'summer', 'summer']
>>> 

The difference between append and extend

append treats our new elements as a complete element for addition, while extend subdivides the separable elements for addition

insert() method -- it can be added according to our specified location

List name Insert (subscript value, new data)

Chestnuts:

>>> name = ["Yaqi","Xia Xia"]
>>> name.insert(1,'like')
>>> print(name)
['Yaqi', 'like', 'Xia Xia']

II Deletion of list elements

remove() method -- when the data in the list is repeated, only the data with the smallest subscript value is deleted

List name Remove (data)

Chestnuts:

>>> grade = [666,555,444,333,222,111]
>>> grade.remove(555)
>>> print(grade)
[666, 444, 333, 222, 111]

pop() method -- delete by subscript

List name Pop (subscript)

If the subscript is not written, the last element in the list will be deleted by default.

Chestnuts:

>>> grade = [666,555,444,333,222,111]
>>> grade.pop()
111
>>> print(grade)
[666, 555, 444, 333, 222]

clear() -- this method will delete all the elements and make them an empty list

List name clear()

Chestnuts:

>>> grade = [666,555,444,333,222,111]
>>> grade.clear()
>>> print(grade)
[]

del() -- this function can delete a section of elements

del list name [start value: end value] (closed on the left and open on the right)

When there is no parameter operation after del function, the whole list will be deleted

Chestnuts:

>>> name = ["Xia Xia","Yaqi","like","Ah Chen"]
>>> del name[0:2]
>>> print(name)
['like', 'Ah Chen']

III Modification of list elements

3.1 modification of single elements in the list

List name [subscript] = new element

Chestnuts:

name = ['Wang Yaqi','like','Xia Xia','Brother Chen']

print(name)
name[3]='It's also Xueqing'
print(name)
====================== RESTART: E:/code/Py/2022-1-21.py ======================
['Wang Yaqi', 'like', 'Xia Xia', 'Brother Chen']
['Wang Yaqi', 'like', 'Xia Xia', 'It's also Xueqing']

3.2 modification of elements in the first paragraph of the list

List name [start value: end value] = new element

Chestnuts:

name = ['Xia Xia','like','Yaqi','Brother Chen','Ah Chen']
print(name)
name[2:]='love','like'
print(name)
====================== RESTART: E:/code/Py/2022-1-21.py ======================
['Xia Xia', 'like', 'Yaqi', 'Brother Chen'	, 'Ah Chen']
['Xia Xia', 'like', 'Yaqi', 'love', 'like']

IV Query of list elements

1. index() method -- the result is the subscript of the element

List name Index (data, start value, end value)

name = []
name.extend("I like summer best")
print(name.index("summer"))
print(name.index("happiness",0,5))
print(name.index("I",0,2))
======================= RESTART: E:/code/Py/chaxun.py =======================
5
2
0

2. count () method -- count the number of times the element appears in the list

List name Count (data)

Chestnuts:

name1 = ["Xia Xia","Xia Xia","Xia Xia","Xia Xia","Xia Xia"]
print(name1.count("Xia Xia"))
name2 = ["like","like"]
print(name2.count("like"))
name3 = ["Yaqi"]
print(name3.count("Yaqi"))
======================= RESTART: E:/code/Py/chaxun.py =======================
5
2
1

V List length

len() -- this function counts the length of the list

Len (list name)

Chestnuts:

name1 = ['Yaqi','yes',"Xia Xia","of","baby"]
print(len(name1))
name2 = ['most','like']
print(len(name2))
name3 = ['Xia Xia']
print(len(name3))
======================= RESTART: E:/code/Py/chaxun.py =======================
5
2
1

Topics: Python Back-end