python 内置函数

一、作用域相关
1.globals:查看全局作用域里的内容

a = 10
def func():
    a = 10

print(globals())

打印结果:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x00000205433B9240>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:/Users/DELL/PycharmProjects/pythonProject1/c/test1.py', '__cached__': None, 'a': 10, 'func': <function func at 0x0000020543A449D8>}

2.locals():查看当前作用域中的内容

a = 10
def func():
    b = 8
    print(locals())
func()

打印结果:

{'b': 8}

二、迭代器,生成器相关
1.range(start, end, step) 区间:[start, end)
2.iter() 取出迭代器
3.next()取出下一个元素

lst = [1, 2, 3]
it = iter(lst)
print(next(it))
print(next(it))
print(next(it))

打印结果:

1
2
3

三、其他:
1.dir:查看数据类型能执行的操作

print(dir(list))

打印结果:

['__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']

2.callable(参数):判断参数能否被调用,在调用函数前,先用callable函数判断能否能否被调用,避免报错。

def func():
    pass
#func函数能被调用
print(callable(func))
#数字无法直接调用
print(123)

打印结果:

True
False

3.help:输出数据结构的源代码

print(help(str))

打印结果:

class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str
 |  
 |  Create a new string object from the given object. If encoding or
 |  errors is specified, then the object must expose a data buffer
 |  that will be decoded using the given encoding and error handler.
 |  Otherwise, returns the result of object.__str__() (if defined)
 |  or repr(object).
 |  encoding defaults to sys.getdefaultencoding().
 |  errors defaults to 'strict'.
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __format__(...)
 |      S.__format__(format_spec) -> str
 |      
 |      Return a formatted version of S as described by format_spec.

4.import(参数):动态导出参赛名称的模块

mo = input(">>>") #"os"
__import__(mo)

5.open: 打开文件。
具体操作:open文件操作
`
6.id(参数):查看参数的内存地址

x = 3
print(id(x))

打印输出:

1798746368

7.hash(参数):输出参数的hash值。可变的数据结构不可hash,例如列表。

print(hash("AAA"), hash("AAA"))

打印结果:

-6981976277823355042 -6981976277823355042

8.eval:可以把字符串当成代码执行,有返回值。

print(eval("1 + 1"))

输出:

2

9.exec(参数):执行参数里的代码,但是没有返回值
存在安全性问题。

s = "a = 100"
exec(s)
print(a)#a标红,报错,但是正常打印

打印:

100

10.compile() :把一段字符串代码加载,后面可以通过exec和eval执行
compile(”parament1“, ”parament2“,”parament3“)
parament1:需要执行的字符串
parament2:代码所在的文件
parament3:exec:不需要返回值;eval:需要返回值;single:需要输入

c = compile("n = int(input('>>>:'))","","single")
exec(c)
print(n)

打印输出:

>>>:55
55

四、基础数据类型:
1.float:小数类型
2.int:整数类型
3.complex:复数
4.bool:布尔数据类型
5.bin(x):将数字转为二进制,例如:0b1001
6.oct(x):将数字转为八进制,例如:0o1772
7.hex(x):将数字转为十六进制,例如:0x15ab
8.abs(x):将数字转为绝对值
9.divmod(x, y):得到除法的商和余数构成的元组
10.round(x):得到x变为整数后的四舍五入值
11.pow(x, y)和c++一样
12.sum(x):计算列表,元组等的和
13.max(x):计算列表,元组等的最大值
14.min(x):计算列表,元组等的最小值
15.dict():得到集合
16.frozenset():建立集合后,不能更改
17.enumerate:枚举列表等。
18.any:列表等里的元素做or运算。

lst = [0, "换行", True]
print(any(lst))

打印:

True

19.all(x):列表里的元素做and运算。

lst = [0, "换行", True]
print(all(lst))

打印:

False

20.zip(x1,x2,x3):把对于位置的元素拉链链接到一起

lst1 = [1, 2, 3]
lst2 = [2, 4, 6]
lst3 = [3, 6, 9]
a = zip(lst1, lst2, lst3)
#直接打印是a的地址,要找迭代器看具体的值
print(a)
it = iter(a)
print(next(it))
print(next(it))
print(next(it))

打印结果:

<zip object at 0x0000020EBDCC3648>
(1, 2, 3)
(2, 4, 6)
(3, 6, 9)

tip:
利用zip快速构建字典。

lst1 = [1, 2, 3]
lst2 = [2, 4, 6]

# 快速构建字典
d = dict(zip(lst1, lst2))
print(d)

打印得到:

{1: 2, 2: 4, 3: 6}

21.reversed(参数):将参数里的数反转,并生成一个迭代器,注意原来参数里的数没有变化

lst1 = [1, 2, 3]
r = reversed(lst1)
print(lst1)
it = iter(r)
while True:
     try:
          print(next(it))
     except StopIteration:
          break
[1, 2, 3]
3
2
1

22.切片slice:在切片中适用,方便一次更改多个列表等的切片方式。

k = "上单流浪法师瑞兹"
m = "中单疾风剑豪"
s = slice(0, 6, 2)
print(k[s])
print(m[s])

打印:

上流法
中疾剑

23.ord (c ):返回字符c编码的值
chr(num):返回编码值为num的字符
可以帮助生成验证码

print(ord("中"))
print(chr(20013))

打印

20013

24.repr:显示字符串里所有的字符,包括转义字符等。

print(str("你好,'我\t来了"))
print(repr("你好,'我\t来了"))

打印输出:

你好,'我	来了
"你好,'我\t来了"

25.format(num, “08d”) :将num变为8位数的数字,前面的空余部分用 ‘0’ 填充。
format(num, “.8f”) 小数点后保留8位小数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值