Python 是一种广泛使用的高级编程语言,其丰富的内置函数和模块大大简化了编程工作。以下是一些常用的 Python 函数和模块。

常用内置函数

  • len(): 返回对象的长度
    • 示例:len("hello") 返回 5
  • print(): 打印输出
    • 示例:print("hello, world!")
  • input(): 从用户获取输入
    • 示例:user_input = input("请输入您的名字:")

常用模块

  • math: 提供数学运算函数
    • 示例:import math
    • math.sqrt(x): 返回 x 的平方根
    • math.pi: π 的值
  • random: 提供随机数生成器
    • 示例:import random
    • random.randint(a, b): 生成一个介于 a 和 b 之间的随机整数
    • random.random(): 生成一个 [0.0, 1.0) 之间的随机浮点数

图片示例

Python

更多 Python 教程