码迷,mamicode.com
首页 > 编程语言 > 详细

python内置函数

时间:2017-10-03 13:12:47      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:eval   font   htm   ==   style   ack   false   tuple   else   

https://docs.python.org/3/library/functions.html#next

bin()二进制

oct()八进制

int()十进制

str()字符串

print(hex(15))
i = int(‘11‘,base=8)
print(i)
#============
i = int(‘0xe‘,base=16)
print(i)
 
i=int(‘0b11‘,base=2)
print(i)
 
i=int(‘0o11‘,base=8)
print(i)
==================================
ord()将字母转换成数字
chr()将数字转换成字母
t=ord(‘a‘)
print(t,type(t))
n = chr(65)
print(n,type(n))
运行结果
97 <class ‘int‘>
A <class ‘str‘>
==================================
随机验证码(含数字和字母大写六位)
import random
temp=""
for i in range(6):
    num = random.randrange(0,4)#生成0到3的随机数4种可能
    if num ==3 or num ==1:#1或3就生成数字
         rad2 = random.randrange(0,10)
         temp = temp + str(rad2)
    else:
         rad1=random.randrange(65,91)
         temp +=chr(rad1)#生成随机字母
print(temp)
=============================================
dict() 生成字典
list()  列表
tuple() 元组
int()   数字
str()   字符串
============================================
运算len()计算长度
sum()求和
eval()计算
max()求最大
min()求最小的
t=max([11,22,33])
print(t)
n=min([11,22,33])
print(n)
m=len([11,22,33])
print(m)
sum=sum([11,22,33])
print(sum)
sum1=eval(‘1 + 2 + 3‘)
print(sum1)
运行结果
33
11
3
66
6
==========================================
isinstance()为判断是否是一个字符串,数组,元组,字典等,为真为True为假为False
ret = isinstance([11,22,33],str)
print(ret)
运行结果为False
ret = isinstance([11,22,33],list)
print(ret)
运行结果为True
=============================================================
next()可迭代反回下一下元素直到耗尽,StopIteration报错
s=iter([11,22,33])
t=next(s)
print(t)
t1=next(s)
print(t1)
t2=next(s)
print(t2)
t3=next(s)
print(t3)
运行结果
11
22、
33
Traceback (most recent call last):
File "H:/python17/s29.py", line 135, in <module>
t3=next(s)
StopIteration
========================
globals()列出全局变量
locals()局部变量
====================================
zip()
x=[1,2,3]
y=[4,5,6]
zipped=zip(x,y)
print(list(zipped))
x1,y1=zip(*zip(x,y))
print(x1)
print(y1)
运行结果
[(1, 4), (2, 5), (3, 6)]
(1, 2, 3)
(4, 5, 6)




 
 
 

python内置函数

标签:eval   font   htm   ==   style   ack   false   tuple   else   

原文地址:http://www.cnblogs.com/wang43125471/p/7623557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!