# 1 print
print('123',end='\n')
print('1','2',sep='*') # 默认空格分割
print(f'{x}') # f-strings
print # 可以写入文件
# 2 字典
dw_set = set() # 集合-元素唯一性
for i in 'datawhale':
dw_set.add(i)
# 键值对组成的集合就是字典
dw_dict = {'d':'data','w':'whale'} # key:value
print(dw_dict['d'])
# 更新
dw_dict['w'] = 'Whale'
'1,2,3,4'.split(',') # 字符串切分得到列表
# 3 函数
# 3.1 自定义函数与return
def relu(x):
if x > 0:
return x # 一旦执行return就结束
return 0
# 3.2 匿名函数
relu1 = lambda x:max(x,0)
# 3.3 回调函数
# 4 复制
# 4.1 直接复制
a = [1,2,3,[4,5]]
b = a
id(a) == id(b),id(a) # id相等说明是同一个
# 4.2 浅度复制
c = a.copy()
a.append(6) # c不变,b变
a[3].append(7) # c变,b变
# 4.3 深度抄袭
import copy
d = copy.deepcopy(a) #内容一模一样但完全独立
# 5 面向对象
import time
from tqdm import tqdm
from IPython.display import display,HTML
class Train:
def __init__(self,name,*goods,naughty=True): # *goods 不定长度 打包成元组
self.name = name
self.goods = goods
self.naughty = naughty
def __getitem__(self,idx):
if self.naughty:
return self.goods[idx] # choice(self.goods)
return self.goods
def __len__(self):
return len(self.goods)
def info(self):
if self.name == '托马斯小火车':
return f'hi,我是{self.name},HHHH'
return f'hi,我是{self.name}'
def 发车(self,string):
print(f'{string},上山!')
for _ in tqdm(range(30)):
time.sleep(0.1)
display(HTML('<p>加油加油</p>'))
dtzz = Train('大头种子',1,2.5,9,114,514,naughty=True) # 先实例化
# good = ['a','b','c','d']
# dtzz = Train('大头种子',good,naughty=True)
dtzz.info()
dtzz.__getitem__(2)
# 解包与打包 5,10 => (5,10) => a,b
a,b = dtzz[2],dtzz[3]
0722选修直播:Python语法讲解
于 2023-07-22 21:50:24 首次发布