1.随机生成浮点数,0-1小数,整数
import numpy as np
import random
np.random.randn(1,3)
生成 1-3之间的浮点数
random.random()
生成 0-1之间的小数
random.randint(1,3)
生成 1-3之间的整数,闭区间
2.避免转义
在输出的字符串前加 r
一个 \ 表示转义,两个则不转义
3.super用法
class Parent(object):
def __init__(self, age):
self.age = age
print(age)
class Children(Parent):
def hello(self):
pass
Children('12').hello()
因为继承 parent类,所以 parent类被调用时就会初始化,打印 12
同理,对于类方法也是一样
class Parent(object):
def __init__(self, age):
self.age = age
def hello(self):
print('hi')
class Children(Parent):
def hello(self):
super(Children, self).hello()
print('hi')
Children('12').hello()
最终输出 hi hi
super用于继承父类方法
super.(子类名称,self).父类方法名
由上看出,子类可以调用父类方法
在调用继承父类方法的子类方法时,会先执行父类方法,再执行子类方法
若被继承方法有参数,直接传参
在super后的方法中加参数
4.异常
try except else finally
当 try 中无错误时,执行 try else finally
有错误时,执行 except finally
5.随机打乱列表中的元素
import random
a = [1,2,3,4,5,6]
random.shuffle(a)
列表a 就会被打乱
6.随机生成x个随机数
import random
a = '123433213'
result=(''.join(random.choice(a) for i in range(4)))
生成一个4位随机数