Introduction to Common Built-in Functions in Python 常用内置函数
itomcoil 2025-05-02 18:56 15 浏览
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,银行家舍入(一种四舍五入规则)
相关推荐
- selenium(WEB自动化工具)
-
定义解释Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7,8,9,10,11),MozillaF...
- 开发利器丨如何使用ELK设计微服务中的日志收集方案?
-
【摘要】微服务各个组件的相关实践会涉及到工具,本文将会介绍微服务日常开发的一些利器,这些工具帮助我们构建更加健壮的微服务系统,并帮助排查解决微服务系统中的问题与性能瓶颈等。我们将重点介绍微服务架构中...
- 高并发系统设计:应对每秒数万QPS的架构策略
-
当面试官问及"如何应对每秒几万QPS(QueriesPerSecond)"时,大概率是想知道你对高并发系统设计的理解有多少。本文将深入探讨从基础设施到应用层面的解决方案。01、理解...
- 2025 年每个 JavaScript 开发者都应该了解的功能
-
大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发。1.Iteratorhelpers开发者...
- JavaScript Array 对象
-
Array对象Array对象用于在变量中存储多个值:varcars=["Saab","Volvo","BMW"];第一个数组元素的索引值为0,第二个索引值为1,以此类推。更多有...
- Gemini 2.5编程全球霸榜,谷歌重回AI王座,神秘模型曝光,奥特曼迎战
-
刚刚,Gemini2.5Pro编程登顶,6美元性价比碾压Claude3.7Sonnet。不仅如此,谷歌还暗藏着更强的编程模型Dragontail,这次是要彻底翻盘了。谷歌,彻底打了一场漂亮的翻...
- 动力节点最新JavaScript教程(高级篇),深入学习JavaScript
-
JavaScript是一种运行在浏览器中的解释型编程语言,它的解释器被称为JavaScript引擎,是浏览器的一部分,JavaScript广泛用于浏览器客户端编程,通常JavaScript脚本是通过嵌...
- 一文看懂Kiro,其 Spec工作流秒杀Cursor,可移植至Claude Code
-
当Cursor的“即兴编程”开始拖累项目质量,AWS新晋IDEKiro以Spec工作流打出“先规范后编码”的系统工程思维:需求-设计-任务三件套一次生成,文档与代码同步落地,复杂项目不...
- 「晚安·好梦」努力只能及格,拼命才能优秀
-
欢迎光临,浏览之前点击上面的音乐放松一下心情吧!喜欢的话给小编一个关注呀!Effortscanonlypass,anddesperatelycanbeexcellent.努力只能及格...
- JavaScript 中 some 与 every 方法的区别是什么?
-
大家好,很高兴又见面了,我是姜茶的编程笔记,我们一起学习前端相关领域技术,共同进步,也欢迎大家关注、点赞、收藏、转发,您的支持是我不断创作的动力在JavaScript中,Array.protot...
- 10个高效的Python爬虫框架,你用过几个?
-
小型爬虫需求,requests库+bs4库就能解决;大型爬虫数据,尤其涉及异步抓取、内容管理及后续扩展等功能时,就需要用到爬虫框架了。下面介绍了10个爬虫框架,大家可以学习使用!1.Scrapysc...
- 12个高效的Python爬虫框架,你用过几个?
-
实现爬虫技术的编程环境有很多种,Java、Python、C++等都可以用来爬虫。但很多人选择Python来写爬虫,为什么呢?因为Python确实很适合做爬虫,丰富的第三方库十分强大,简单几行代码便可实...
- pip3 install pyspider报错问题解决
-
运行如下命令报错:>>>pip3installpyspider观察上面的报错问题,需要安装pycurl。是到这个网址:http://www.lfd.uci.edu/~gohlke...
- PySpider框架的使用
-
PysiderPysider是一个国人用Python编写的、带有强大的WebUI的网络爬虫系统,它支持多种数据库、任务监控、项目管理、结果查看、URL去重等强大的功能。安装pip3inst...
- 「机器学习」神经网络的激活函数、并通过python实现激活函数
-
神经网络的激活函数、并通过python实现whatis激活函数感知机的网络结构如下:左图中,偏置b没有被画出来,如果要表示出b,可以像右图那样做。用数学式来表示感知机:上面这个数学式子可以被改写:...
- 一周热门
- 最近发表
- 标签列表
-
- ps图案在哪里 (33)
- super().__init__ (33)
- python 获取日期 (34)
- 0xa (36)
- super().__init__()详解 (33)
- python安装包在哪里找 (33)
- linux查看python版本信息 (35)
- python怎么改成中文 (35)
- php文件怎么在浏览器运行 (33)
- eval在python中的意思 (33)
- python安装opencv库 (35)
- python div (34)
- sticky css (33)
- python中random.randint()函数 (34)
- python去掉字符串中的指定字符 (33)
- python入门经典100题 (34)
- anaconda安装路径 (34)
- yield和return的区别 (33)
- 1到10的阶乘之和是多少 (35)
- python安装sklearn库 (33)
- dom和bom区别 (33)
- js 替换指定位置的字符 (33)
- python判断元素是否存在 (33)
- sorted key (33)
- shutil.copy() (33)