8个实用的Python内置函数

大家好,Python是一种简单而强大的编程语言,作为Python编程的基础,掌握一些实用的内置函数对于初学者来说是至关重要的。本文将介绍8个适合初学者的实用Python内置函数。

1. print()

python 复制代码
print('apple')
print('orange')
print('pear')

# apple
# orange
# pear

将某些内容输入print()中时,它会被打印到终端(也称为标准输出)。

2. abs()

python 复制代码
print(abs(-5))  # 5
print(abs(6))   # 6

abs()给出了一个数的绝对值,负数变成正数,而正数保持不变。

3. input()

通过input()函数,可以要求用户在Python程序中输入某些内容。

python 复制代码
name = input('what is your name? ')
print('your name is ' + name)

# what is your name? tom
# your name is tom

注意,input()始终返回一个字符串值。因此,如果用户想要数字或其他任何值,需要自己进行必要的类型转换。

4. range()

range()作为for循环的一部分,允许多次重复代码操作,而不需要多次复制和粘贴代码。

python 复制代码
for i in range(5):
  print('apple')

# apple
# apple
# apple
# apple
# apple

5. dir()

python 复制代码
x = 'apple'
print(dir(x))

# ['__add__', '__class__', '__contains__', '__delattr__', 
# '__dir__', '__doc__', '__eq__', '__format__', '__ge__', 
# '__getattribute__', '__getitem__', '__getnewargs__', 
# '__getstate__', '__gt__', '__hash__', '__init__', 
# '__init_subclass__', '__iter__', '__le__', '__len__', 
# '__lt__', '__mod__', '__mul__', '__ne__', '__new__', 
# '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', 
# '__rmul__', '__setattr__', '__sizeof__', '__str__', 
# '__subclasshook__', 'capitalize', 'casefold', 
# 'center', 'count', 'encode', 'endswith', 'expandtabs', 
# 'find', 'format', 'format_map', 'index', 'isalnum', 
# 'isalpha', 'isascii', 'isdecimal', 'isdigit', 
# 'isidentifier', 'islower', 'isnumeric', 'isprintable', 
# 'isspace', 'istitle', 'isupper', 'join', 'ljust', 
# 'lower', 'lstrip', 'maketrans', 'partition', 
# 'removeprefix', 'removesuffix', 'replace', 'rfind', 
# 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 
# 'split', 'splitlines', 'startswith', 'strip', 
# 'swapcase', 'title', 'translate', 'upper', 'zfill']

将某个对象传递给dir()时,可以打印出该对象中存在的每个方法或属性,想要快速检查某个对象可能具有的方法/属性时会很有帮助(而无需上网查找文档)。

6. help()

python 复制代码
help(print())


# Help on NoneType object:

# class NoneType(object)
#  |  Methods defined here:
#  |
#  |  __bool__(self, /)
#  |      True if self else False
#  |
#  |  __hash__(self, /)
#  |      Return hash(self).
# ...

将某个内容传递给help()时,它会自动打印出其文档,如果不想上网查找某些文档,这也是很有用的。

7. len()

python 复制代码
print(len('apple'))    # 5
print(len('orange'))   # 6
print(len('pear'))     # 4

当我们将某个内容传递给len()时,可以得到该对象的长度(如果对象没有长度,例如整数,将得到一个错误)。

8. max()和min()

python 复制代码
print(max(1,3,2))  # 3
print(min(1,3,2))  # 1

max()查找最大的对象,而min()查找最小的对象。

本文简单介绍了8个适合初学者的实用Python内置函数,包括print()、abs()、input()、range()、dir()、help()、len()、max()和min()。这些函数是Python编程中不可或缺的工具,能够帮助大家执行各种常见任务,掌握这些函数能更好地理解和使用Python编程语言。

相关推荐
belldeep1 小时前
python:genesis-world, Genesis-Embodied-AI
人工智能·python·ai·genesis
ss2732 小时前
为什么PostgreSQL适合AI场景?
数据库·人工智能·postgresql
瑞克-桑切斯2 小时前
7、MySQL 慢查询排查与优化实战指南
java·服务器·数据库
benchmark_cc2 小时前
A 股、港股、美股如何建立统一的标的主数据格式?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
z06.3 小时前
MySQL索引
数据库·mysql
维克兜率天3 小时前
【维克】趋势型CTA:期货市场的“追涨杀跌“
人工智能·python
考虑考虑3 小时前
Posrgresql恢复.backup文件
数据库·后端·postgresql
Csvn3 小时前
性能监控
前端
云祺vinchin3 小时前
中小企业备份软件选型从恢复需求到总体成本评估
数据库·数据备份·中小企业备份·容灾备份软件
Zenova EdgeOS3 小时前
Go 工业边缘错误处理实战:用 errors 构建可追溯错误链
开发语言·数据库·golang