Python内置函数的介绍与案例

Python 提供了许多内置函数,这些函数可以直接使用,无需导入任何模块。以下是一些常用的 Python 内置函数:

1. print()

用途:将对象输出到控制台。
示例:
     ```python
     print("Hello, World!")  # 输出: Hello, World!
     print(1, 2, 3, sep='-')  # 输出: 1-2-3
     print("Hello", end=' ')  # 输出: Hello(不换行)
     ```

2. chr()

用途:将一个整数(Unicode码点)转换为对应的字符。
示例:
     ```python
     print(chr(65))  # 输出: A
     print(chr(97))  # 输出: a
     ```

3. ord()

用途:将一个字符转换为对应的Unicode码点。
示例:
     ```python
     print(ord('A'))  # 输出: 65
     print(ord('a'))  # 输出: 97
     ```

4. int()

用途:将一个字符串或数字转换为整数。
示例:
     ```python
     print(int("123"))  # 输出: 123
     print(int("1010", 2))  # 输出: 10(二进制转换)
     ```

5. float()

用途:将一个字符串或数字转换为浮点数。
示例:
     ```python
     print(float("123.45"))  # 输出: 123.45
     print(float("1e3"))  # 输出: 1000.0
     ```

6. str()

用途:将对象转换为字符串。
示例:
     ```python
     print(str(123))  # 输出: "123"
     print(str([1, 2, 3]))  # 输出: "[1, 2, 3]"
     ```

7. len()

用途:返回对象的长度或项目数。
示例:
     ```python
     print(len("Hello"))  # 输出: 5
     print(len([1, 2, 3]))  # 输出: 3
     ```

8. type()

用途:返回对象的类型。
示例:
     ```python
     print(type(123))  # 输出: <class 'int'>
     print(type("Hello"))  # 输出: <class 'str'>
     ```

9. input()

用途:从用户获取输入。
示例:
     ```python
     name = input("Enter your name: ")
     print("Hello, " + name)
     ```

10. range()

用途:生成一个整数序列。
示例:
      ```python
      for i in range(5):
          print(i)  # 输出: 0 1 2 3 4
      for i in range(1, 5):
          print(i)  # 输出: 1 2 3 4
      for i in range(1, 10, 2):
          print(i)  # 输出: 1 3 5 7 9
      ```

11. list()

用途:将对象转换为列表。
示例:
      ```python
      print(list("abc"))  # 输出: ['a', 'b', 'c']
      print(list((1, 2, 3)))  # 输出: [1, 2, 3]
      ```

12. tuple()

用途:将对象转换为元组。
示例:
      ```python
      print(tuple("abc"))  # 输出: ('a', 'b', 'c')
      print(tuple([1, 2, 3]))  # 输出: (1, 2, 3)
      ```

13. set()

 用途:将对象转换为集合。
 示例:
      ```python
      print(set("abc"))  # 输出: {'a', 'b', 'c'}
      print(set([1, 2, 2, 3]))  # 输出: {1, 2, 3}
      ```

14. dict()

 用途:创建一个字典。
 示例:
      ```python
      print(dict(a=1, b=2))  # 输出: {'a': 1, 'b': 2}
      print(dict([('a', 1), ('b', 2)]))  # 输出: {'a': 1, 'b': 2}
      ```

15. max()

用途:返回可迭代对象中的最大值。

示例:
      ```python
      print(max([1, 2, 3]))  # 输出: 3
      print(max('a', 'b', 'c'))  # 输出: 'c'
      ```

16. min()

用途:返回可迭代对象中的最小值。
示例:
      ```python
      print(min([1, 2, 3]))  # 输出: 1
      print(min('a', 'b', 'c'))  # 输出: 'a'
      ```

17. sum()

用途:返回可迭代对象中所有元素的和。
示例:
      ```python
      print(sum([1, 2, 3]))  # 输出: 6
      print(sum((1, 2, 3), 10))  # 输出: 16(起始值为10)
      ```

18. sorted()

用途:返回一个排序后的列表。
示例:
      ```python
      print(sorted([3, 1, 2]))  # 输出: [1, 2, 3]
      print(sorted(['c', 'a', 'b']))  # 输出: ['a', 'b', 'c']
      ```

19. reversed()

用途:返回一个反向迭代器。
示例:
      ```python
      print(list(reversed([1, 2, 3])))  # 输出: [3, 2, 1]
      print(list(reversed('abc')))  # 输出: ['c', 'b', 'a']
      ```

20. enumerate()

用途:返回一个枚举对象。
示例:
      ```python
      for i, value in enumerate(['a', 'b', 'c']):
          print(i, value)  # 输出: 0 a, 1 b, 2 c
      ```

21. zip()

用途:将多个可迭代对象打包成元组。
示例:
      ```python
      print(list(zip([1, 2, 3], ['a', 'b', 'c'])))  # 输出: [(1, 'a'), (2, 'b'), (3, 'c')]
      print(list(zip([1, 2], ['a', 'b', 'c'])))  # 输出: [(1, 'a'), (2, 'b')]
      ```

22. any()

用途:如果可迭代对象中任何元素为真,则返回 True。
示例:
      ```python
      print(any([False, True, False]))  # 输出: True
      print(any([False, False, False]))  # 输出: False
      ```

23. all()

用途:如果可迭代对象中所有元素为真,则返回 True。
示例:
      ```python
      print(all([True, True, False]))  # 输出: False
      print(all([True, True, True]))  # 输出: True
      ```

24. map()

用途:对可迭代对象中的每个元素应用函数。
示例:
      ```python
      print(list(map(lambda x: x * 2, [1, 2, 3])))  # 输出: [2, 4, 6]
      print(list(map(str, [1, 2, 3])))  # 输出: ['1', '2', '3']
      ```

25. filter()

用途:使用函数过滤可迭代对象中的元素。
示例:
      ```python
      print(list(filter(lambda x: x > 1, [1, 2, 3])))  # 输出: [2, 3]
      print(list(filter(None, [0, 1, False, True])))  # 输出: [1, True]
      ```

26. abs()

用途:返回数字的绝对值。
示例:
      ```python
      print(abs(-10))  # 输出: 10
      print(abs(10))  # 输出: 10
      ```

27. round()

用途:将数字四舍五入到指定的小数位数。
示例:
      ```python
      print(round(3.14159, 2))  # 输出: 3.14
      print(round(3.14159))  # 输出: 3
      ```

28. pow()

用途:返回 x 的 y 次幂。
示例:
      ```python
      print(pow(2, 3))  # 输出: 8
      print(pow(2, 3, 5))  # 输出: 3(2^3 % 5)
      ```

29. divmod()

用途:返回商和余数。
示例:
      ```python
      print(divmod(10, 3))  # 输出: (3, 1)
      print(divmod(8, 2))  # 输出: (4, 0)
      ```

30. isinstance()

用途:检查对象是否是某个类的实例。
示例:
      ```python
      print(isinstance(123, int))  # 输出: True
      print(isinstance("Hello", str))  # 输出: True
      ```

31. issubclass()

用途:检查一个类是否是另一个类的子类。
示例:
      ```python
      class A: pass
      class B(A): pass
      print(issubclass(B, A))  # 输出: True
      print(issubclass(A, B))  # 输出: False
      ```

32. id()

用途:返回对象的唯一标识符。
示例:
      ```python
      a = 123
      print(id(a))  # 输出: 某个整数
      b = 123
      print(id(b))  # 输出: 与 id(a) 相同
      ```

33. help()

用途:提供交互式的帮助信息。
示例:
      ```python
      help(print)  # 输出: print 函数的帮助信息
      help(list)  # 输出: list 类的帮助信息
      ```

34. dir()

用途:返回对象的属性和方法列表。
示例:
      ```python
      print(dir(list))  # 输出: list 类的属性和方法列表
      print(dir(str))  # 输出: str 类的属性和方法列表
      ```

35. format()

用途:格式化字符串。
示例:
      ```python
      print("Hello, {}!".format("World"))  # 输出: Hello, World!
      print("{} + {} = {}".format(1, 2, 3))  # 输出: 1 + 2 = 3
      print("{name} is {age} years old.".format(name="dandan", age=3))  # 输出: dandan is 3 years old.
      ```

这些内置函数在Python编程中非常常用,掌握它们可以大大提高你的编程效率和代码质量。希望这些详细的解释和示例对你有所帮助!

  • 25
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值