您的位置:首页 > 编程语言 > Python开发

[学习笔记]Python 常用函数

2017-11-25 23:54 821 查看
isinstance(…)

isinstance(object, class-or-type-or-tuple) -> bool

Return whether an object is an instance of a class or of a subclass thereof. With a type as second argument, return whether that is the object’s type. The form using a tuple, isinstance(x, (A, B, …)), is a shortcut for isinstance(x, A) or isinstance(x, B) or … (etc.).

for s in [u'abc', 'abc']:
print isinstance(s, str)
print isinstance(s, basestring)
print isinstance(s, unicode)
#输出结果
False
True
True
True
True
False


Random生成随机数

函数说明
choice(seq)从序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中随机挑选一个整数。
randrange ([start,] stop [,step])从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1
random()随机生成下一个实数,它在[0,1)范围内。
shuffle(list)将序列的所有元素随机排序
uniform(x, y)随机生成下一个实数,它在[x,y]范围内。
randint(self,a,b)
练习:生成8位数随机密码,要求大小写数字加下划线

import random
import string

list=[]
for i in range(1,4):
a=random.choice(string.digits)
b=random.choice(string.lowercase)
c=random.choice(string.uppercase)
list.append(a)
list.append(b)
list.append(c)
del list[0:2]          #取九个元素删掉两个
#print list
list.append("_")       #加上下划线
random.shuffle(list)   #随机排序
password=''.join(list) #拼接成字符

print password
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python isinstance random