VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • Python基础(十二)(2)

  1. all()

    • 函数定义:all(*args, **kwargs)
    • 函数说明:填入参数为可迭代对象,如果可迭代对象中每一项元素的布尔值为真,则返回True,若可迭代对象为空,返回True
    print(all([1, 2, 3, 4, 5]))
    print(all([0, 1, 2, 3, 4]))
    print(all([]))
    
    运行结果:
    True
    False
    True
    
  2. any()

    • 函数定义:any(*args, **kwargs)
    • 函数说明:填入参数为可迭代对象,如果可迭代对象中只要有一项元素的布尔值为真,则返回True,若可迭代对象为空,返回True
    print(any([0, "", 1, False]))
    print(any([0, "", [], False]))
    print(any([]))
    
    运行结果:
    True
    False
    False
    
  3. bytes()

    • 函数定义:bytes(value=b'', encoding=None, errors='strict')
    • 函数说明:将填入对象转换为字节形式,需要指定编码方式
    print("张旭东".encode("utf-8"))
    print(bytes("张旭东", encoding="utf-8"))
    
    运行结果:
    b'\xe5\xbc\xa0\xe6\x97\xad\xe4\xb8\x9c'
    b'\xe5\xbc\xa0\xe6\x97\xad\xe4\xb8\x9c'
    
  4. callable()

    • 函数定义:callable(i_e_, some_kind_of_function)
    • 函数说明:判断对象是否可以调用(类也是可调用的,类的实例可调用__call__()方法)
    def func():
    	print("hello world")
    	
    print(callable(func))
    
    运行结果:
    True
    
  5. chr()

    • 函数定义:chr(*args, **kwargs)
    • 函数说明:返回Unicode对应的字符串
    print(chr(65))
    
    运行结果:
    A
    
  6. ord()

    • 函数定义:ord(*args, **kwargs)
    • 函数说明:返回一个字符串对应的Unicode编码值
    print(ord("A"))
    
    运行结果:
    65
    
  7. complex()

    • 函数定义:complex(real, imag=None)
    • 函数说明:返回一个数的复数形式
    print(complex(20))
    
    运行结果:
    (20+0j)
    
  8. divmid()

    • 函数定义:divmod(x, y)
    • 函数说明:返回元组(x // y, x % y)
    print(divmod(10, 3))
    
    运行结果:
    (3, 1)
    
  9. eval()(不推荐使用)

    • 函数定义:eval(*args, **kwargs)
    • 函数说明:可以计算给定的参数,参数只能是简单表达式
    print(eval("5 + 3"))
    
    运行结果:
    8
    
  10. exec()(不推荐使用)

    • 函数定义:exec(*args, **kwargs)
    • 函数说明:可以计算给定的参数,参数可以是代码块
    exec("for i in range(10):print(i)")
    
    运行结果:
    0
    1
    2
    3
    4
    
  11. frozenset()

    • 函数定义:frozenset(obj)
    • 函数说明:将对象转换为不可变集合
    print(frozenset((1,2,3)))
    
    运行结果:
    frozenset({1, 2, 3})
    
  12. help()

    • 函数定义:help(args)
    • 函数说明:查看帮助
    help(list.append)
    
    运行结果:
    Help on method_descriptor:
    
    append(...)
        L.append(object) -> None -- append object to end
    
  13. globals()

    • 函数定义:globals(*args, **kwargs)
    • 函数说明:返回包含当前作用域的全局变量的字典
    a = 5
    b = 10
    def func():
        print("hello world")
    print(globals())
    
    运行结果:
    {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x00000221ED9D69E8>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/python_S26/day12/exercise.py', '__cached__': None, 'a': 5, 'b': 10, 'func': <function func at 0x00000221ED8D1EA0>}
    
  14. locals()

    • 函数定义:locals(*args, **kwargs)
    • 函数说明:返回一个包含当前作用域的局部变量的字典
    a = 5
    def func():
        b = 10
        print(locals())
    
    func()
    
    运行结果:
    {'b': 10}
    
  15. hash()

    • 函数定义:hash(*args, **kwargs)
    • 函数说明:返回对象的哈希值
    print(hash("zxd"))
    
    运行结果:
    5236597812272808709
    
  16. id()

    • 函数定义:id(*args, **kwargs)
    • 函数说明:返回对象的id,在CPython中返回的是对象的内存地址
    print(id(10))
    
    运行结果:
    1864728976
    
  17. iter()

    • 函数定义:iter(iterable, sentinel=None)
    • 函数说明:传入可迭代对象,获取迭代器
    lst = [1, 2, 3, 4, 5]
    t = iter(lst)
    print(t)
    
    运行结果:
    <list_iterator object at 0x00000181FB2CA9B0>
    
  18. next()

    • 函数定义:next(iterator, default=None)
    • 函数说明:返回迭代器的下一项,若提供了默认值,并且迭代器已耗尽,则返回它而不是引发StopIteration
    t = iter([1, 2, 3, 4, 5])
    print(next(t, None))
    print(next(t, None))
    print(next(t, None))
    print(next(t, None))
    print(next(t, None))
    print(next(t, None))
    
    运行结果:
    1
    2
    3
    4
    5
    None
    
  19. bin()

    • 函数定义:bin(*args, **kwargs)
    • 函数说明:返回整数的二进制形式
    print(bin(10))
    
    运行结果:
    0b1010
    
  20. oct()

    • 函数定义:oct(*args, **kwargs)
    • 函数说明:返回整数的八进制形式
    print(oct(10))
    
    运行结果:
    0o12
    
  21. int()

    • 函数定义:int(x, base=10)
    • 函数说明:返回对应进制的十进制,需填入对应进制数字符串形式,并填入进制数
    print(int("0b1010", 2))
    print(int("0o12", 8))
    
    运行结果:
    10
    10
    
  22. hex()

    • 函数定义:hex(*args, **kwargs)
    • 函数说明:返回整数的十六进制形式
    print(hex(20))
    
    运行结果:
    0x14
    
  23. pow()

    • 函数定义:pow(*args, **kwargs)
    • 函数说明:填入两个参数时,求x的y次幂;填入三个参数时,求x的y次幂在与z取余
    print(pow(2, 4))
    print(pow(2, 4, 3))
    
    运行结果:
    16
    1
    
  24. repr()

    • 函数定义:repr(obj)
    • 函数说明:返回规范字符串的表现形式
    s = "zxd"
    print(repr(s))
    
    运行结果:
    'zxd'
    
  25. round()

    • 函数定义:round(number, ndigits=None)
    • 函数说明:将数字四舍五入为十进制整数
    print(round(3.5))
    
    运行结果:
    4
    

(二)常用函数(会用)

  • abs() format() enumerate() open()
    range() print() input() len()
    list() dict() str() set()
    tuple() float() reversed sum()
    dir() type() zip() bool()
  1. abs()

    • 函数定义:abs(*args, **kwargs)
    • 函数说明:返回参数的绝对值
    print(abs(-1))
    
    运行结果:
    1
    
  2. format()

    • 函数定义:format(*args, **kwargs)
    • 函数说明:返回格式化后的字符串
    s = "你好"
    print(format(s, ">20"))  # 右对齐20位,多余的部分用空格补齐
    print(format(s, "<20"))  # 左对齐20位,多余的部分用空格补齐
    print(format(s, "^20"))  # 居中共20位,多余的部分用空格补齐
    
    运行结果:
                      你好
    你好                  
             你好         
    
    s = 18
    print(format(s, "08b"))  # 共八位,转换为二进制
    print(format(s, "08o"))  # 共八位,转换为八进制
    print(format(s, "08x"))  # 共八位,转换为十六进制
    print(format(s, "08d"))  # 共八位,转换为十进制
    
    运行结果:
    00010010
    00000022
    00000012
    00000018
    
  3. enumerate()

    • 函数定义:enumerate(iterable, start=0)
    • 函数说明:枚举,遍历可迭代对象并给定对应遍历的数字,以元组的形式返回,第二个参数可指定遍历数字的起始值
    lst = [1, 2, 3, 4, 5]
    for i in enumerate(lst):
        print(i)
        
    运行结果:
    (0, 1)
    (1, 2)
    (2, 3)
    (3, 4)
    (4, 5)
    
    lst = [1, 2, 3, 4, 5]
    for i in enumerate(lst, 10):  # 指定起始值
        print(i)
        
    运行结果:
    (10, 1)
    (11, 2)
    (12, 3)
    (13, 4)
    (14, 5)
    
  4. open()

    • 函数定义:open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True)
    • 函数说明:打开一个文件,指定文件路径file 、文件打开模式mode、文件编码encoding,返回一个文件句柄用于操作文件,其余参数暂时忽略
    f = open("text.txt", mode="r", encoding="utf-8")
    f.read()
    f.close()
    
  5. range()

    • 函数定义:range(start, stop[, step])
    • 函数说明:返回一个对象,该对象可以从开始一个一个返回一个递增的整数序列一直到结束,如果有步长参数,则按照步长返回
    for i in range(5):
    	print(i)
    	
    运行结果:
    0
    1
    2
    3
    4
    
  6. print()

    • 函数定义:print(self, *args, sep=' ', end='\n', file=None)
    • 函数说明:在控制台打印参数,可填入参数sep来替代元素之间的逗号,可填入参数end来控制结尾的换行,可填入参数file将打印的内容写入文件
    print("zxd", "znb", sep="-", end="!")
    
    运行结果:
    zxd-znb!
    
  7. input()

    • 函数定义:input(*args, **kwargs)
    • 函数说明:将输入内容转换为字符串,可填出参数用来提示用户
    name = input("请输入姓名:")
    print(name)
    
    运行结果:
    zxd
    
  8. len()

    • 函数定义:len(*args, **kwargs)
    • 函数说明:返回容器中的项数
    lst = [1, 2, 3, 4, 5]
    print(len(lst))
    
    运行结果:
    5
    
  9. list()

    • 函数定义:
      • list()
      • list(iterable)
    • 函数说明:
      • 默认创建空列表
      • 将可迭代对象转换为列表
    print(list())
    print(list(range(5)))
    
    运行结果:
    []
    [0, 1, 2, 3, 4]
    
  10. dict()

    • 函数定义:
      • dict()
      • dict(mapping)
      • dict(iterable)
      • dict(**kwargs)
    • 函数说明:
      • 默认创建空字典
      • 可填入映射创建字典
      • 可填入二元组列表创建字典
      • 可填入关键字参数常见列表
    print(dict())
    print(dict({"k1": 1, "k2": 2}))
    print(dict([("k1", 1), ("k2", 2)]))
    print(dict(k1=1, k2=2))
    
    运行结果:
    {}
    {'k1': 1, 'k2': 2}
    {'k1': 1, 'k2': 2}
    {'k1': 1, 'k2': 2}
    
  11. str()

    • 函数定义:

      • str()

      • str(value='', encoding=None, errors='strict')

    • 函数说明:

      • 默认创建空字符串

      • 可将参数转换为字符串类型,若填入参数为字节类型,需指定编码方式

    print(repr(str(123)))
    print(str(b'\xe5\xbc\xa0\xe6\x97\xad\xe4\xb8\x9c',encoding="utf-8"))
    
    运行结果:
    张旭东
    '123'
    
  12. set()

    • 函数定义:
      • set()
      • set(iterable)
    • 函数说明:
      • 默认创建空集合
      • 将可迭代对象转换为集合(自动去重)
    print(set())
    print(set([1,2,3,4,5]))
    
    运行结果:
    set()
    {1, 2, 3, 4, 5}
    
  13. tuple()

    • 函数定义:
      • tuple()
      • tuple(iterable)
    • 函数说明:
      • 默认创建空元组
      • 将可迭代对象转换为元组
    print(tuple())
    print(tuple([1, 2, 3, 4, 5]))
    
    运行结果:
    ()
    (1, 2, 3, 4, 5)
    
  14. float()

    • 函数定义:float(x)
    • 函数说明:将参数转换为浮点型
    print(float(123))
    
    运行结果:
    123.0
    
  15. reversed

    • 函数定义:reversed(sequence)
    • 函数说明:将传入的可迭代对象反向迭代,返回一个迭代器
    lst = [1, 2, 3, 4, 5]
    print(reversed(lst))
    print(list(reversed(lst)))
    
    运行结果:
    <list_reverseiterator object at 0x000002043A55A9B0>
    [5, 4, 3, 2, 1]
    
  16. sum()

    • 函数定义:sum(*args, **kwargs)
    • 函数说明:将填入的参数求和,填入的参数必须是可迭代对象
    print(sum([1, 2, 3, 4, 5]))
    
    运行结果:
    15
    
  17. dir()

    • 函数定义:dir([object])
    • 函数说明:查看对象的方法
    print(dir(list))
    
    运行结果:
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__'