Python笔记

目录

1 库函数

1.1python内置函数

enumerate(sequence, [start=0])

super()

format()

f-Strings

1.2 模块函数

【str】

str.split(str="", num=string.count(str))

str.strip(self, chars=None, /)

str.lstrip(self, chars=None, /)

str.rstrip(self, chars=None, /)

【file】

file.read(size=-1, /)

file.readlines(hint=-1, /)

file.readline(hint=-1, /)

2 模块

math

easydict

matplotlib

3 语法

块注释

函数注释

is 和 == 的区别

设置断点

列表与迭代器

迭代器与生成器

可变参数:一颗星(*)和两颗星(**)


【教程】

廖雪峰:https://www.liaoxuefeng.com/wiki/1016959663602400

菜鸟教程:https://www.runoob.com/python3/python3-tutorial.html

W3Cschool学院:https://www.w3cschool.cn/python3/python3-tutorial.html

1 库函数

1.1python内置函数

所有内置函数:https://www.runoob.com/python3/python3-built-in-functions.html

enumerate(sequence, [start=0])

  • sequence -- 一个序列、迭代器或其他支持迭代对象。
  • start -- 下标起始位置。

功能:enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。其实就是添加了一个索引值。

Python3 enumerate() 函数:https://www.runoob.com/python3/python3-func-enumerate.html

# eg:此处仅读取MNIST的测试集
>>> test_loader = torch.utils.data.DataLoader(datasets.MNIST(dataset_path, train=False, \
    download=False,transform=transforms.ToTensor()),batch_size=batch_size, shuffle=True)
>>> for x, y in test_loader:
...     print(x.size())    # x 表示数据 (batch_size, C, W, H)
...     print(y.size())    # y 表示标签 (batch_size)

# enumerate本质就是添加了一个索引index
>>> for index, img in enumerate(test_loader):
...     print(index)
...     print(img[1].size())
...     print(img[1].size())
...     break
0
torch.Size([64, 1, 28, 28])    # 此处batch_size = 64
torch.Size([64])

super()

功能:调用父类(超类)的一个方法

Python super() 函数:https://www.runoob.com/python/python-func-super.html

        super() 函数是用于调用父类(超类)的一个方法,而 Python3.x 和 Python2.x 的一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx 

class A:
     def add(self, x):
         y = x+1
         print(y)
class B(A):
    def add(self, x):
        super().add(x)
        # super(B, self).add(x)    # Python 2.x
b = B()
b.add(2)  # 3

关于 super() 的初始化功能:Python—子类构造函数调用super().__init__()

format()

功能:格式化字符

Python format 格式化函数:https://www.runoob.com/python/att-string-format.html

格式化输出(%用法和fomat用法)

print("{} {}".format('jack', 'tom'))	# jack tom
print("{1} {0} {1}".format('jack', 'tom'))	# tom jack tom
print("姓名:{name}, 年龄:{age}".format(name='jack', age=25))
person = {'name':'jack', 'age':25}
print("姓名:{name}, 年龄:{age}".format(**person))

person1 = ['jack', 25]
print("姓名1:{0[0]}, 年龄1:{0[1]}".format(person1))		# 0 不能省略
person2 = ['tom', 24]
print("姓名1:{0[0]}, 年龄1:{0[1]}\n姓名2:{1[0]}, 年龄2:{1[1]}".format(person1, person2))
person3 = [person1, person2]
print("姓名1:{0[0][0]}, 年龄1:{0[0][1]}\n姓名2:{0[1][0]}, 年龄2:{0[1][1]}".format(person3))

f-Strings

功能:格式化字符串(推荐使用这个)

name = "Eric"
age = 74
>>> print(f"Hello, {name}. You are {age}.")    # f大写也可以
'Hello, Eric. You are 74.'
>>> print(f'{2*23}')                # 运行时渲染,支持表达式
'46'
>>> name = 'Eric'
>>> f"{name.lower()} is funny."     # 当然也支持函数
'eric is funny.'

print(..., end='\r')

添加这一句可以让控制台定行输出

map( func, *

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值