The second part of Python basics: list + tuple + dictionary + set, the basics of learning have to be this....

Python Basics Chapter 2

Python Basics Chapter 1 The knowledge of this chapter follows the previous chapter, and the zero-based friends can learn from the above

A chapter to learn. Of course, you can also collect it and learn it slowly. Learning should not be rushed...

insert image description here

list

Python学习交流Q群:906715085###
print("-------------创建列表-------------");
list1 = ['JAVA', 'Hello', 'Python', 'VS', 1, 2, 3]
print(list1)
list2 = list('Python')
print(list2)
list3 = []
print(list3)
print("-------------访问列表中的值-------------");
print("list1[0]: ", list1[0])
print("list2[1:5]: ", list2[1:5])
print("-------------列表函数-------------");
list1.append('XYZ') #向 list1 增加'XYZ'对象
print(list1.count('Python')) #返回'Python'出现次数
list1.extend(list2) #将 list2 加到 list1 后面
print(list1)
list1.remove('XYZ') #删除对象'XYZ'
print(list1.pop()) # 删除列表的最后位置上的对象并返回
print(list1)
D:\工作空间\Python\venv\Scripts\python.exe D:/工作空间/Python/main.py
-------------创建列表-------------
['JAVA', 'Hello', 'Python', 'VS', 1, 2, 3]
['P', 'y', 't', 'h', 'o', 'n']
[]
-------------访问列表中的值-------------
list1[0]:  JAVA
list2[1:5]:  ['y', 't', 'h', 'o']
-------------列表函数-------------
1
['JAVA', 'Hello', 'Python', 'VS', 1, 2, 3, 'XYZ', 'P', 'y', 't', 'h', 'o', 'n']
n
['JAVA', 'Hello', 'Python', 'VS', 1, 2, 3, 'P', 'y', 't', 'h', 'o']

进程已结束,退出代码0

insert image description here

tuple

print("-------------创建访问元祖-------------");
#coding=utf-8
tuple = ( 'Java', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print(tuple) # 输出完整元组
print(tuple[0])# 输出元组的第一个元素
print(tuple[1:3]) # 输出第二个至第三个的元素
print(tuple[2:])# 输出从第三个开始至列表末尾的所有元素
print(tinytuple * 2) # 输出元组两次
print(tuple + tinytuple) # 打印组合的元组
D:\工作空间\Python\venv\Scripts\python.exe D:/工作空间/Python/main.py
-------------创建访问元祖-------------
('Java', 786, 2.23, 'john', 70.2)
Java
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('Java', 786, 2.23, 'john', 70.2, 123, 'john')

进程已结束,退出代码0

insert image description here

insert image description here

dictionary

Python学习交流Q群:906715085###
print("-------------创建字典-------------");
d1 = {
    
    } # 空字典
d2={
    
    "id":10,"tel":123456,"name":"小明"}
print(d1)
print(d2)
print("-------------访问字典-------------");
dict2 = {
    
    'name': '小明','id':1, 'dept': '计算机'}
print(dict2['dept'])
print(dict2['name'])
print("-------------修改添加字典-------------");
dict1 = {
    
    'Name':'小明', 'Age':19, 'major':'计算机'};
dict1['Age'] = 18; # 字典中有"Age"键,更新现有元素
dict1['college'] = "Tech"; # 字典中无"college"键,执行添加操作
print("dict1['Age']: ",dict1['Age'])
print("dict1['college']: ",dict1['college'])
print("-------------删除字典-------------");
dict1={
    
    "stu_name":"小明","stu_id":1,"stu_age":24}
del dict1["stu_id"] # 删除键为"stu_id"的键值对
print(dict1)
dict1.clear() # 删除所有键值对
print(dict1)
D:\工作空间\Python\venv\Scripts\python.exe D:/工作空间/Python/main.py
-------------创建字典-------------
{
    
    }
{
    
    'id': 10, 'tel': 123456, 'name': '小明'}
-------------访问字典-------------
计算机
小明
-------------修改添加字典-------------
dict1['Age']:  18
dict1['college']:  Tech
-------------删除字典-------------
{
    
    'stu_name': '小明', 'stu_age': 24}
{
    
    }

进程已结束,退出代码0

insert image description here

gather

Python学习交流Q群:906715085###
print("-------------创建集合-------------");
s = set() # 空集
print(s)
print(type(s))
s = {
    
    1,2,3} # 直接写入集合元素
print(type(s))
s=set(["ABC",'XYZ','xyz','123','1',1,1.0])
print(s)
s=set(i for i in range(10))
print(s)
s=frozenset("Python 3.3.3")
print(s)
s= dict((i,0) for i in {
    
    1, 'ABC', 'XYZ', 'xyz', '1', '123'})
print(s)
s= dict((i,0) for i in frozenset({
    
    'n', 'o', 'h', ' ', '.', 'y', 't', 'P', '3'}))
print(s)
print("-------------访问集合-------------");
s = set(['A', 'B', 'C', 'D'])
# s = {'A', 'B', 'C', 'D'}
print('A' in s)
print('a' not in s)
for i in s:
    print(i,end='\t')
print("-------------更新集合-------------");
s = set(['A', 'B', 'C', 'D'])
s = s|set('Python') # 使用操作符"|"
print(s)
s.add('ABC') # add()方法
print(s)
s.remove('ABC') # remove()方法
s.update('JAVAEF') # update()方法
print(s)

insert image description here

D:\工作空间\Python\venv\Scripts\python.exe D:/工作空间/Python/main.py
-------------创建集合-------------
set()
<class 'set'>
<class 'set'>
{
    
    1, '1', 'xyz', 'XYZ', '123', 'ABC'}
{
    
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
frozenset({
    
    't', 'o', 'y', 'n', '3', '.', 'h', ' ', 'P'})
{
    
    1: 0, '1': 0, 'xyz': 0, 'XYZ': 0, '123': 0, 'ABC': 0}
{
    
    't': 0, 'o': 0, 'n': 0, '.': 0, 'y': 0, '3': 0, 'h': 0, ' ': 0, 'P': 0}
-------------访问集合-------------
True
True
B	D	C	A	-------------更新集合-------------
{
    
    'B', 't', 'o', 'y', 'n', 'D', 'C', 'h', 'P', 'A'}
{
    
    'B', 't', 'o', 'ABC', 'y', 'n', 'D', 'C', 'h', 'P', 'A'}
{
    
    'B', 'E', 't', 'o', 'y', 'n', 'J', 'D', 'C', 'h', 'F', 'P', 'V', 'A'}

进程已结束,退出代码0
D:\工作空间\Python\venv\Scripts\python.exe D:/工作空间/Python/main.py
-------------创建字典-------------
{
    
    }
{
    
    'id': 10, 'tel': 123456, 'name': '小明'}
-------------访问字典-------------
计算机
小明
-------------修改添加字典-------------
dict1['Age']:  18
dict1['college']:  Tech
-------------删除字典-------------
{
    
    'stu_name': '小明', 'stu_age': 24}
{
    
    }

进程已结束,退出代码0

insert image description here

At last

That's all for today's basic knowledge. We can't remember how much we share too much at one time. And, since today is the weekend, we still have to let everyone have a good time, right? Base

Knowledge is to prepare for learning Python better and implementing Python cases later. Everyone must work hard when learning! ! See you in the next chapter~~

insert image description here

Guess you like

Origin blog.csdn.net/xff123456_/article/details/124364852