Introduction to Common Built-in Functions in Python 常用内置函数
off999 2025-05-22 12:42 19 浏览 0 评论
Hello, students! Today we will learn about some common built-in functions in Python. These functions are very useful and can help you solve many problems more easily. Let’s start with their names, meanings, and simple examples.
1. abs()
Function: Returns the absolute value (the non-negative value without considering the sign) of a number.
Example:
print(abs(-5)) # Output: 5
print(abs(3.14)) # Output: 3.14
2. ascii()
Function: Returns a string containing the printable representation of an object. It escapes non-ASCII characters with backslashes.
Example:
print(ascii("你好")) # Output: '\u4f60\u597d' (represents Chinese characters in Unicode)
3. bin()
Function: Converts an integer to a binary (base-2) string prefixed with "0b".
Example:
print(bin(10)) # Output: 0b1010
4. bool()
Function: Converts a value to a Boolean (logical) value, either True or False.
Common False values: 0, 0.0, "" (empty string), None, empty lists/dictionaries/sets.
Example:
print(bool(0)) # Output: False
print(bool("hello")) # Output: True
5. chr()
Function: Returns a character (a string) from an integer representing its Unicode code point.
Example:
print(chr(65)) # Output: A (65 is the Unicode code for 'A')
6. divmod()
Function: Takes two numbers and returns a tuple containing their quotient (商) and remainder (余数) when divided.
Example:
result = divmod(10, 3)
print(result) # Output: (3, 1) (10 ÷ 3 = 3 with remainder 1)
7. eval()
Function: Evaluates a string as a Python expression and returns the result.
Caution: Use carefully, as it can execute any code in the string!
Example:
print(eval("3 + 5 * 2")) # Output: 13
8. float()
Function: Converts a value to a floating-point (decimal) number.
Example:
print(float(5)) # Output: 5.0
print(float("2.718")) # Output: 2.718
9. hex()
Function: Converts an integer to a hexadecimal (base-16) string prefixed with "0x".
Example:
print(hex(255)) # Output: 0xff
10. id()
Function: Returns the unique identifier (memory address) of an object.
Example:
x = [1, 2, 3]
print(id(x)) # Output: a unique number (e.g., 140732227146240)
11. int()
Function: Converts a value to an integer (whole number).
Example:
print(int(3.9)) # Output: 3 (truncates decimals, does not round)
print(int("123")) # Output: 123
12. len()
Function: Returns the length (number of elements) of a sequence (like a string, list, or tuple).
Example:
print(len("python")) # Output: 6
print(len([1, 2, 3, 4])) # Output: 4
13. max()
Function: Returns the largest item in an iterable (like a list) or the largest of multiple arguments.
Example:
print(max(5, 10, 3)) # Output: 10
print(max([-2, -5, -1])) # Output: -1
14. min()
Function: Returns the smallest item in an iterable or the smallest of multiple arguments.
Example:
print(min(5, 10, 3)) # Output: 3
print(min([-2, -5, -1])) # Output: -5
15. oct()
Function: Converts an integer to an octal (base-8) string prefixed with "0o".
Example:
print(oct(10)) # Output: 0o12
16. ord()
Function: Returns the Unicode code point of a single character (the reverse of chr()).
Example:
print(ord('A')) # Output: 65
17. pow()
Function: Returns x raised to the power of y (x^y). Can also take a third argument for modulo (余数) operation.
Example:
print(pow(2, 3)) # Output: 8 (2^3 = 8)
print(pow(2, 3, 5)) # Output: 3 (2^3 % 5 = 8 % 5 = 3)
18. range()
Function: Generates a sequence of numbers, typically used in loops (循环).
Syntax: range(start, stop, step) (start is inclusive, stop is exclusive, step is optional).
Example:
for i in range(3): # 0, 1, 2
print(i)
19. round()
Function: Rounds a number to a specified number of decimal places (default is 0, rounding to the nearest integer).
Example:
print(round(3.1415, 2)) # Output: 3.14
print(round(2.5)) # Output: 2 (Note: Python uses "bankers rounding" for .5 cases)
20. sum()
Function: Sums the items of an iterable, starting from an optional initial value.
Example:
print(sum([1, 2, 3])) # Output: 6
print(sum([1, 2, 3], 10)) # Output: 16 (10 + 1 + 2 + 3)
21. type()
Function: Returns the type of an object (e.g., int, str, list).
Example:
print(type(5)) # Output: <class 'int'>
print(type("hello")) # Output: <class 'str'>
These are some of the most useful built-in functions in Python. Practice using them in your code, and you’ll become more familiar with how they work. Remember, the best way to learn is by doing!
Python常用内置函数介绍
同学们好!今天我们将学习Python中一些常用内置函数。这些函数非常实用,可以帮助你更轻松地解决许多问题。让我们从它们的名称、功能和简单示例开始吧。
1. abs()
功能:返回一个数的绝对值(不考虑符号的非负值)。
示例:
print(abs(-5)) # 输出:5
print(abs(3.14)) # 输出:3.14
2. ascii()
功能:返回对象的可打印字符串表示,用反斜杠转义非ASCII字符。
示例:
print(ascii("你好")) # 输出:'\u4f60\u597d'(用Unicode表示汉字)
3. bin()
功能:将整数转换为二进制(以2为底)字符串,前缀为"0b"。
示例:
print(bin(10)) # 输出:0b1010
4. bool()
功能:将值转换为布尔(逻辑)值,即True或False。
常见False值:0,0.0,""(空字符串),None,空列表/字典/集合。
示例:
print(bool(0)) # 输出:False
print(bool("hello")) # 输出:True
5. chr()
功能:根据表示Unicode代码点的整数返回对应的字符(字符串)。
示例:
print(chr(65)) # 输出:A(65是'A'的Unicode编码)
6. divmod()
功能:接受两个数,返回一个元组,包含它们相除的商(quotient)和余数(remainder)。
示例:
result = divmod(10, 3)
print(result) # 输出:(3, 1)(10除以3商3余1)
7. eval()
功能:将字符串作为Python表达式求值并返回结果。
注意:谨慎使用,因为它可以执行字符串中的任何代码!
示例:
print(eval("3 + 5 * 2")) # 输出:13
8. float()
功能:将值转换为浮点数(小数)。
示例:
print(float(5)) # 输出:5.0
print(float("2.718")) # 输出:2.718
9. hex()
功能:将整数转换为十六进制(以16为底)字符串,前缀为"0x"。
示例:
print(hex(255)) # 输出:0xff
10. id()
功能:返回对象的唯一标识符(内存地址)。
示例:
x = [1, 2, 3]
print(id(x)) # 输出:一个唯一的数字(如:140732227146240)
11. int()
功能:将值转换为整数(整数)。
示例:
print(int(3.9)) # 输出:3(截断小数,不四舍五入)
print(int("123")) # 输出:123
12. len()
功能:返回序列(如字符串、列表、元组)的长度(元素个数)。
示例:
print(len("python")) # 输出:6
print(len([1, 2, 3, 4])) # 输出:4
13. max()
功能:返回可迭代对象(如列表)中的最大项,或多个参数中的最大值。
示例:
print(max(5, 10, 3)) # 输出:10
print(max([-2, -5, -1])) # 输出:-1
14. min()
功能:返回可迭代对象中的最小项,或多个参数中的最小值。
示例:
print(min(5, 10, 3)) # 输出:3
print(min([-2, -5, -1])) # 输出:-5
15. oct()
功能:将整数转换为八进制(以8为底)字符串,前缀为"0o"。
示例:
print(oct(10)) # 输出:0o12
16. ord()
功能:返回单个字符的Unicode代码点(chr()的反向操作)。
示例:
print(ord('A')) # 输出:65
17. pow()
功能:返回x的y次幂(x^y)。也可以接受第三个参数进行取模(modulo)运算。
示例:
print(pow(2, 3)) # 输出:8(2的3次方等于8)
print(pow(2, 3, 5)) # 输出:3(2^3除以5的余数是3)
18. range()
功能:生成一个数字序列,通常用于循环(loop)中。
语法:range(start, stop, step)(start包含在内,stop不包含,step可选)。
示例:
for i in range(3): # 0, 1, 2
print(i)
19. round()
功能:将数字四舍五入到指定的小数位数(默认0位,即取整)。
示例:
print(round(3.1415, 2)) # 输出:3.14
print(round(2.5)) # 输出:2(注意:Python对.5的情况使用“银行家舍入”)
20. sum()
功能:对可迭代对象的元素求和,可指定可选的初始值。
示例:
print(sum([1, 2, 3])) # 输出:6
print(sum([1, 2, 3], 10)) # 输出:16(10 + 1 + 2 + 3)
21. type()
功能:返回对象的类型(如int,str,list)。
示例:
print(type(5)) # 输出:<class 'int'>
print(type("hello")) # 输出:<class 'str'>
这些是Python中最常用的一些内置函数。在代码中练习使用它们,你会更熟悉它们的工作方式。记住,最好的学习方法是实践!
专业词汇及不常用词汇表
- absolute value, /'aebslut 'vaelju/, n,绝对值
- Boolean, /'bulin/, adj/n,布尔(值)
- quotient, /'kwont/, n,商
- remainder, /r'mendr/, n,余数
- Unicode, /'junkod/, n,统一码(字符编码标准)
- hexadecimal, /heks'desml/, adj/n,十六进制(的)
- octal, /'ɑktl/, adj/n,八进制(的)
- modulo, /'mɑdlo/, n,模(运算)
- iterable, /'trbl/, n,可迭代对象
- truncates, /tr'kets/, v,截断
- bankers rounding, /'baekrz 'rand/, n,银行家舍入(一种四舍五入规则)
相关推荐
- pip的使用及配置_pip怎么配置
-
要使用python必须要学会使用pip,pip的全称:packageinstallerforpython,也就是Python包管理工具,主要是对python的第三方库进行安装、更新、卸载等操作,...
- Anaconda下安装pytorch_anaconda下安装tensorflow
-
之前的文章介绍了tensorflow-gpu的安装方法,也介绍了许多基本的工具与使用方法,具体可以看Ubuntu快速安装tensorflow2.4的gpu版本。pytorch也是一个十分流行的机器学...
- Centos 7 64位安装 python3的教程
-
wgethttps://www.python.org/ftp/python/3.10.13/Python-3.10.13.tgz#下载指定版本软件安装包tar-xzfPython-3.10.1...
- 如何安装 pip 管理工具_pip安装详细步骤
-
如何安装pip管理工具方法一:yum方式安装Centos安装python3和python3-devel开发包>#yuminstallgcclibffi-develpy...
- Python入门——从开发环境搭建到hello world
-
一、Python解释器安装1、在windows下步骤1、下载安装包https://www.python.org/downloads/打开后选择【Downloads】->【Windows】小编是一...
- 生产环境中使用的十大 Python 设计模式
-
在软件开发的浩瀚世界中,设计模式如同指引方向的灯塔,为我们构建稳定、高效且易于维护的系统提供了经过验证的解决方案。对于Python开发者而言,理解和掌握这些模式,更是提升代码质量、加速开发进程的关...
- 如何创建和管理Python虚拟环境_python怎么创建虚拟环境
-
在Python开发中,虚拟环境是隔离项目依赖的关键工具。下面介绍创建和管理Python虚拟环境的主流方法。一、内置工具:venv(Python3.3+推荐)venv是Python标准...
- 初学者入门Python的第一步——环境搭建
-
Python如今成为零基础编程爱好者的首选学习语言,这和Python语言自身的强大功能和简单易学是分不开的。今天千锋武汉Python培训小编将带领Python零基础的初学者完成入门的第一步——环境搭建...
- 全网最简我的世界Minecraft搭建Python编程环境
-
这篇文章将给大家介绍一种在我的世界minecraft里搭建Python编程开发环境的操作方法。目前看起来应该是全网最简单的方法。搭建完成后,马上就可以利用python代码在我的世界自动创建很多有意思的...
- Python开发中的虚拟环境管理_python3虚拟环境
-
Python开发中,虚拟环境管理帮助隔离项目依赖,避免不同项目之间的依赖冲突。虚拟环境的作用隔离依赖:不同项目可能需要不同版本的库,虚拟环境可以为每个项目创建独立的环境。避免全局污染:全局安装的库可...
- Python内置zipfile模块:操作 ZIP 归档文件详解
-
一、知识导图二、知识讲解(一)zipfile模块概述zipfile模块是Python内置的用于操作ZIP归档文件的模块。它提供了创建、读取、写入、添加及列出ZIP文件的功能。(二)ZipFile类1....
- Python内置模块pydoc :文档生成器和在线帮助系统详解
-
一、引言在Python开发中,良好的文档是提高代码可读性和可维护性的关键。pydoc是Python自带的一个强大的文档生成器和在线帮助系统,它可以根据Python模块自动生成文档,并支持多种输出格式...
- Python sys模块使用教程_python system模块
-
1.知识导图2.sys模块概述2.1模块定义与作用sys模块是Python标准库中的一个内置模块,提供了与Python解释器及其环境交互的接口。它包含了许多与系统相关的变量和函数,可以用来控制P...
- Python Logging 模块完全解读_python logging详解
-
私信我,回复:学习,获取免费学习资源包。Python中的logging模块可以让你跟踪代码运行时的事件,当程序崩溃时可以查看日志并且发现是什么引发了错误。Log信息有内置的层级——调试(deb...
- 软件测试|Python logging模块怎么使用,你会了吗?
-
Pythonlogging模块使用在开发和维护Python应用程序时,日志记录是一项非常重要的任务。Python提供了内置的logging模块,它可以帮助我们方便地记录应用程序的运行时信息、错误和调...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)