[Python Learning]—Python Basic Grammar (6)

1. Data container

Data container in Python: a data type that can hold multiple pieces of data. Each element contained can be any type of data, such as strings, numbers, Boolean, etc.

2. list list

Insert image description here
Insert image description here
Insert image description here
Insert image description here

subscript index of list

Insert image description here
Insert image description here
Insert image description here
Subscripts for nested lists
Insert image description here

my_list=['aaa',1,'True']
print(my_list[0])
print(my_list[1])
print(my_list[2])

Insert image description here

my_list=['aaa',1,'True']
print(my_list[-1])
print(my_list[-2])
print(my_list[-3])

Insert image description here
Insert image description here
Insert image description here

(1), index method

Insert image description here

(2) Modification of the list

列表【下标】=

Insert image description here

(3), insert method

Insert image description here

(4) Appending a single element at the end

Insert image description here

(5) Appending elements in batches at the end

Insert image description here
Insert image description here

(6) Element deletion

Insert image description here

my_list=['aaa','bbb','ccc']
del my_list[0]
print(my_list)

Insert image description here
Insert image description here
Insert image description here

(7) remove method

Remove the first occurrence of an element in the list

my_list=['aaa','bbb','ccc']
my_list.remove('aaa')
print(my_list)

(8) clear method

Insert image description here

(9) count method

Count the number of elements in a list

(10), len method

3. Overview of list methods

Insert image description here
Insert image description here

4. Practice

Insert image description here

list_mess=[21,25,21,23,22,20]
list_mess.append(31)
print(list_mess)

fir_list=list_mess[0]
print(fir_list)
last=list_mess[-1]
print(last)

index=list_mess.index(31)
print(index)

5. Traversal of lists

Insert image description here
Insert image description here

my_list=['beijing','shanghai','hangzou']
index=0
while index<len(my_list):
    ele=my_list[index]
    print(f"列表的元素{ele}")
    index+=1

my_list=[1,2,3,4]
for ele in my_list:
    print(f"列表的元素有{ele}")
    

Insert image description here

# list=[]
# list_num=[1,2,3,4,5,6,7,8,9,10]
# for ele in list_num:
#     if (ele%2==0):
#         list.append(ele)
#
# print(f"从列表{list_num}中取出偶数,组成新的列表{list}")

index=0
list=[]
list_num=[1,2,3,4,5,6,7,8,9,10]
while(index < len(list_num)):
    ele=list_num[index]
    index+=1
    if(ele%2==0):
        list.append(ele)
print(f"从列表{list_num}中取出偶数,组成新的列表{list}")

(f"从列表{list_num}中取出偶数,组成新的列表{list}")

6. Tuple

Tuple definition: Use parentheses to define a tuple, and use commas to separate each data. The data can be of different data types.

Insert image description here

7. Tuple related operations

Insert image description here


tu1=(1,2,3,3,3)
num=tu1.index(2)
print(num)

tot=tu1.count(3)
print(tot)

num2=len(tu1)
print(num2)

Insert image description here


tu1=(1,2,3,3,3)
num=tu1.index(2)
print(num)

tot=tu1.count(3)
print(tot)

num2=len(tu1)
print(num2)

index=0
while( index<len(tu1)):
    print(tu1[index])
    index+=1

for ele in tu1:
    print(ele)

Characteristics of tuples

Insert image description here

8. String

Insert image description here
Insert image description here
Insert image description here

Summary of commonly used operations on strings

Insert image description here
Insert image description here

Characteristics of strings

Insert image description here
Insert image description here

my_str="caicai studying"
result=my_str.count("caicai")
print(result)

resu1=my_str.replace(" ","|")

print(resu1)

resu2=resu1.split("|")
print(resu2)

9. Sequence

Insert image description here
Insert image description here
The operation on the sequence will not affect itself, but will result in a new sequence.

my_list=[0,1,2,3,4]
result1=my_list[1:4:1]
print(result1)

my_tuples=(1,2,3,4,5)
result2=my_tuples[::2]
print(result2)

my_str="1234556"
result3=my_str[::2]
print(result3)

Insert image description here

my_str="希望今年有个offer,希望好运"
result1=my_str[::-1][5:10]
print(result1)


result2=my_str[6:11][::-1]
print(result2)

10. Use of collections

Sets are unordered, so sets do not support subscript index access, but sets, like lists, are allowed to be modified.
Insert image description here

Add new element

Insert image description here

Remove element

Insert image description here

Pick out an element at random

my_set={
    
    "aaa","bbb","ccc"}
ele=my_set.pop()
print(ele)//aaa

Clear collection

Insert image description here

Difference of sets

Insert image description here

set1={
    
    1,2,3}
set2={
    
    2,3,4}
set3=set1.difference(set2)#集合1有而集合2没有的
print(set3)#{1}

Eliminate the difference set of sets

Insert image description here

set1={
    
    1,2,3}
set2={
    
    1,3,4}

set1.difference_update(set2)
print(set1)#{2}
print(set2)#{1,3,4}

Merge collection

set1={
    
    1,2,3}
set2={
    
    2,3,4}

set3=set1.union(set2)
print(set3)#{1,2,3,4}

Number of statistical collections

set1={
    
    1,2,3,4,5}
res=len(set1)
print(res)#5

Summary of commonly used functions of collection

Insert image description here

How to iterate over a collection

  • You can use a for loop to traverse
  • You cannot use a while loop because subscript indexing is not supported.

Collection features

Insert image description here

my_list1=[1,2,3,4,3,3,5]
list1=set()
for x in my_list1:
    list1.add(x)
    print(list1)#{1,2,3,4,5}

10. Dictionary

Definition of dictionary: {} is also used, but the stored elements are key-value pairs, as follows:
Insert image description here


my_dir={
    
    "张三":19,"李四":20,"王五":21}
age=my_dir["张三"]
print(age)//19

11. Common operations on dictionary

Insert image description here
Insert image description here
Insert image description here

Summary of commonly used dictionary operations

Insert image description here
Insert image description here

my_dir={
    
    
    "王力宏":
    {
    
    
       "部门":"科技部",
        "工资":3000,
        "级别":1
    },
"王力":
    {
    
    
       "部门":"科技部",
        "工资":3500,
        "级别":3
    },
"王宏":
    {
    
    
       "部门":"研发部",
        "工资":4000,
        "级别":2
    }

}
print(my_dir)

12. Classification and comparison of data containers

Insert image description here
Insert image description here
Insert image description here

13. General statistical functions of data containers

Insert image description here

Universal sorting function for containers (the sorted result will become a list object)

Insert image description here

my_lisy= [2,1,11,3,5]
tota=sorted(my_lisy)
print(tota)//[1, 2, 3, 5, 11]

Guess you like

Origin blog.csdn.net/m0_46374969/article/details/134047427