Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python内置函数有哪些

作者:看不见相思意   发布日期:2025-10-25   浏览:95

# Python 内置函数示例

# 1. abs() - 返回绝对值
x = -5
print(abs(x))  # 输出: 5

# 2. all() - 检查可迭代对象中的所有元素是否为真
my_list = [True, True, False]
print(all(my_list))  # 输出: False

# 3. any() - 检查可迭代对象中是否有任意元素为真
my_list = [False, True, False]
print(any(my_list))  # 输出: True

# 4. bin() - 将整数转换为二进制字符串
num = 10
print(bin(num))  # 输出: 0b1010

# 5. bool() - 将值转换为布尔值
value = 0
print(bool(value))  # 输出: False

# 6. chr() - 返回对应 ASCII 值的字符
ascii_val = 65
print(chr(ascii_val))  # 输出: A

# 7. dict() - 创建字典
my_dict = dict(name="Alice", age=25)
print(my_dict)  # 输出: {'name': 'Alice', 'age': 25}

# 8. dir() - 列出对象的属性和方法
print(dir(list))  # 输出: list 对象的所有属性和方法

# 9. enumerate() - 枚举可迭代对象,返回索引和值
my_list = ['a', 'b', 'c']
for index, value in enumerate(my_list):
    print(index, value)  # 输出: 0 a, 1 b, 2 c

# 10. filter() - 过滤可迭代对象中的元素
def is_even(n):
    return n % 2 == 0

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(is_even, numbers))
print(even_numbers)  # 输出: [2, 4, 6]

# 11. float() - 将值转换为浮点数
value = "3.14"
print(float(value))  # 输出: 3.14

# 12. format() - 格式化字符串
formatted_string = "Pi is approximately {:.2f}".format(3.14159)
print(formatted_string)  # 输出: Pi is approximately 3.14

# 13. getattr() - 获取对象的属性值
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person = Person("Alice", 25)
print(getattr(person, 'name'))  # 输出: Alice

# 14. globals() - 返回当前全局符号表的字典
print(globals())  # 输出: 当前全局符号表

# 15. hasattr() - 检查对象是否有指定属性
print(hasattr(person, 'age'))  # 输出: True

# 16. id() - 返回对象的唯一标识符
print(id(person))  # 输出: 对象的唯一标识符

# 17. input() - 从用户获取输入
user_input = input("Enter your name: ")
print(user_input)

# 18. int() - 将值转换为整数
value = "100"
print(int(value))  # 输出: 100

# 19. len() - 返回对象的长度
my_list = [1, 2, 3, 4, 5]
print(len(my_list))  # 输出: 5

# 20. list() - 创建列表
my_tuple = (1, 2, 3)
my_list = list(my_tuple)
print(my_list)  # 输出: [1, 2, 3]

# 21. map() - 应用函数到可迭代对象的每个元素上
def square(n):
    return n ** 2

numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(square, numbers))
print(squared_numbers)  # 输出: [1, 4, 9, 16, 25]

# 22. max() - 返回最大值
numbers = [1, 2, 3, 4, 5]
print(max(numbers))  # 输出: 5

# 23. min() - 返回最小值
numbers = [1, 2, 3, 4, 5]
print(min(numbers))  # 输出: 1

# 24. next() - 获取迭代器的下一个元素
my_iterator = iter([1, 2, 3])
print(next(my_iterator))  # 输出: 1

# 25. open() - 打开文件
with open('example.txt', 'w') as file:
    file.write('Hello, World!')

# 26. pow() - 计算幂
base = 2
exponent = 3
print(pow(base, exponent))  # 输出: 8

# 27. print() - 打印输出
print("Hello, World!")

# 28. range() - 创建一个范围对象
for i in range(5):
    print(i)  # 输出: 0, 1, 2, 3, 4

# 29. reversed() - 反转可迭代对象
my_list = [1, 2, 3, 4, 5]
reversed_list = list(reversed(my_list))
print(reversed_list)  # 输出: [5, 4, 3, 2, 1]

# 30. round() - 四舍五入
value = 3.14159
print(round(value, 2))  # 输出: 3.14

# 31. set() - 创建集合
my_set = set([1, 2, 3, 3, 4, 5])
print(my_set)  # 输出: {1, 2, 3, 4, 5}

# 32. sorted() - 对可迭代对象进行排序
my_list = [3, 1, 4, 1, 5, 9]
sorted_list = sorted(my_list)
print(sorted_list)  # 输出: [1, 1, 3, 4, 5, 9]

# 33. str() - 将值转换为字符串
value = 123
print(str(value))  # 输出: '123'

# 34. sum() - 求和
numbers = [1, 2, 3, 4, 5]
print(sum(numbers))  # 输出: 15

# 35. tuple() - 创建元组
my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple)  # 输出: (1, 2, 3)

# 36. type() - 返回对象的类型
value = 123
print(type(value))  # 输出: <class 'int'>

# 37. zip() - 将多个可迭代对象打包成元组
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
zipped = zip(names, ages)
for name, age in zipped:
    print(f"{name} is {age} years old.")
# 输出: Alice is 25 years old.
#       Bob is 30 years old.
#       Charlie is 35 years old.

上一篇:python绘制简单花朵

下一篇:python 类属性

大家都在看

python时间格式

python读取文件路径

staticmethod在python中有

python开发windows应用程序

python中len是什么意思

python ord和chr

python中的yield

python自定义异常

python判断路径是否存在

python list.pop

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站