08常见内置函数

1,exec 执行字符串类型的代码

s = "print ('你好啊,我是赛利亚')"
exec (s) 

你好啊,我是赛利亚

exec ("a = 20")
print (a)

20

2,eval 执行字符串类型的代码,并返回最终结果

s = "1+1"
eval (s)

2

s = "[1,2,3,4]"
ret = eval(s) # 可以把一个字符串代码执行,通常用来还原列表或者字典
print (ret)
print (type(ret))

[1, 2, 3, 4]
<class ‘list’>

3,input输入

aa = input("请输入你的内容:")
print (aa)

请输入你的内容:好好学习,天天向上
好好学习,天天向上
4,print输出

print ("i love python")

i love python
5,内存id

s1 = "[1,2,3,4]"
s2 = "[1,2,3,4]"
print (id(s1))
print (id(s2))

2414673903408
2414674063664
6,hash值

s = "“I love you so much,I just don‘t like you anymore.”"
print (hash(s))

135942013824091963

7,文件操作 open
r:只读 read
w: 只写 write
a : 追加写 append
#相对路径:相对于当前程序所在的文件夹
#绝对路径: c:/work/123.txt

#            读取文件
 f = open ("D:/py/文件读取/123.txt",mode = "r" ,encoding="utf-8")
f = open ("D:/py/文件读取/1234.txt",mode = "w" ,encoding="utf-8")
f.write("笑傲江湖")
#  a  追加写入文件
f = open ("D:/py/文件读取/12345.txt",mode = "a" ,encoding="utf-8")
f.write("令狐冲
")

mode :
r+ 读写操作
w+ 写读
a+ 追加写读

**8,模块相关 __ import __

from matplotlib import pyplot as plt #as 把pyplot修改为plt
import requests

9,帮助 help**

help (print)

Help on built-in function print in module builtins:

print(…)
print(value, …, sep=’ ‘, end=’ ’, file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

10, callable 调用相关

a = 10
def func():
    pass
print (callable(func))#判断能否调用
print (callable(a))

True
False

print (dir(str))#查看字符串能执行那些操作
print (dir(list))#查看列表能执行那些操作

[‘add’, ‘class’, ‘contains’, ‘delattr’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘getitem’, ‘getnewargs’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘iter’, ‘le’, ‘len’, ‘lt’, ‘mod’, ‘mul’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘rmod’, ‘rmul’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘capitalize’, ‘casefold’, ‘center’, ‘count’, ‘encode’, ‘endswith’, ‘expandtabs’, ‘find’, ‘format’, ‘format_map’, ‘index’, ‘isalnum’, ‘isalpha’, ‘isascii’, ‘isdecimal’, ‘isdigit’, ‘isidentifier’, ‘islower’, ‘isnumeric’, ‘isprintable’, ‘isspace’, ‘istitle’, ‘isupper’, ‘join’, ‘ljust’, ‘lower’, ‘lstrip’, ‘maketrans’, ‘partition’, ‘replace’, ‘rfind’, ‘rindex’, ‘rjust’, ‘rpartition’, ‘rsplit’, ‘rstrip’, ‘split’, ‘splitlines’, ‘startswith’, ‘strip’, ‘swapcase’, ‘title’, ‘translate’, ‘upper’, ‘zfill’]
[‘add’, ‘class’, ‘contains’, ‘delattr’, ‘delitem’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘getitem’, ‘gt’, ‘hash’, ‘iadd’, ‘imul’, ‘init’, ‘init_subclass’, ‘iter’, ‘le’, ‘len’, ‘lt’, ‘mul’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘reversed’, ‘rmul’, ‘setattr’, ‘setitem’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘append’, ‘clear’, ‘copy’, ‘count’, ‘extend’, ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, ‘sort’]

12,数据类型 bool值 ,int ,float
13,进制转换

print (hex(100))#十六进制 0-f(0-9 a-f)
print (oct(100))#八进制0-7
print (bin(100))#二进制 0-1
a = 0b1100100
print (a) #默认就是十进制

0x64
0o144
0b1100100
100

14,数学运算 abs , divmod , round , pow , sum , min , max
15,序列
列表和元组:list , tuple

#相关内置函数 : reversed , slice
print (list (reversed([1,2,3,4,5,6,7,8,9,10])))

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

s = slice (1,4)#切片
ss = "love"
print (ss[s])

ove
16,字符串 str , format , bytes , bytearry , memoryview , ord , chr , ascii , repr

print (ord("葛"))
print (chr(33883))

33883

验证码

import random print (chr(random.randint(ord(“a”),old(“z”))))
17,数据集合 dict , set
18,相关内置函数 len , sortend , enumerate , all ,any ,zip , fiter , map

s = ["呵呵","哈哈","嚯嚯"]
for i in enumerate(s):
    print (i)

(0, ‘呵呵’)
(1, ‘哈哈’)
(2, ‘嚯嚯’)

lst1 = ["a","b","c","d"]
lst2 = [1,2,3,4]
lst3 = ["一","二","三","四"]
# 拉链函数
print (list(zip(lst1,lst2,lst3)))

[(‘a’, 1, ‘一’), (‘b’, 2, ‘二’), (‘c’, 3, ‘三’), (‘d’, 4, ‘四’)]

print (any([1,0,True,[],False])) #or
print (all([1,0,True,[],False]))# and

True
False

原文地址:https://www.cnblogs.com/gemoumou/p/13635357.html