Introduction to Common Built-in Functions in Python 常用内置函数
off999 2025-05-22 12:42 6 浏览 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,银行家舍入(一种四舍五入规则)
相关推荐
- 零基础怎么学Python,这些一定要明白
-
随着人工智能和大数据的兴起,Python这门语言也被越来越多人使用。为什么python这么火爆呢,一方面是由于其语言的核心设计思想,具备简洁、易读、高效等诸多优点,另一方面是其广泛的应用场景,分别在...
- 小学生Python编程入门-1.什么是编程?
-
第一阶段:编程初体验第1章:什么是编程?目标:像探险家发现新大陆一样认识编程!本章将带你解开生活中的“魔法密码”,认识Python的神奇力量,并搭建属于你的第一个魔法基地!1.1生活中的编程案例魔法...
- Python报错信息一目了然,3个技巧助你快速定位问题
-
点赞、收藏、加关注,下次找我不迷路是不是每次看到代码报错就慌得不行?一堆红红绿绿的英文报错信息,看得头都大了,完全不知道从哪儿下手。别担心,今天咱们就来好好聊聊怎么查看Python报错信息,学会...
- 如何理解Python类中的self?
-
许多python初学者,在接触到python面向对象的时候,就被类中包含的方法中的self打败了,不知道self是何物?既然写在方法中,是必须参数,为何在调用方法的时候不给它传参数还能正常运行?和我们...
- Python简介与开发环境搭建详细教程
-
1.1Python简介与开发环境搭建详细教程一、Python语言简介1.Python的核心特点2.Python的应用领域表1.1Python主要应用领域领域典型应用常用库Web开发网站后端D...
- Python 中 `r` 前缀:字符串处理的“防转义利器”
-
#Python中`r`前缀:字符串处理的“防转义利器”在Python编程过程中,处理字符串时经常会遇到反斜杠`\`带来的转义问题,而`r`前缀的出现有效解决了这一困扰。它不仅能处理...
- Python中去除字符串末尾换行符的方法
-
技术背景在Python编程中,处理字符串时经常会遇到字符串末尾包含换行符的情况,如从文件中读取每一行内容时,换行符会作为字符串的一部分被读取进来。为了满足后续处理需求,需要将这些换行符去除。实现步骤1...
- python利用正则提取字符串中的手机号
-
需求:利用正则提取字符串中的手机号(假设手机号为1开头的11为数字,要求手机号前后不为数字)待提取的字符串:str="15838477645dfdfdf15887988765dfdf1157...
- 史上最全正则详解
-
涉及到处理文本数据时,Python的正则表达式(RegularExpression)提供了一种强大而灵活的工具。下面详细讲解一些常见的正则表达式语法,并提供实例来说明它们的用法。正则表达式语法正则表...
- Python正则之glob库
-
0、glob模块和通配符glob模块最主要的方法有2个:1、glob()2、iglob()以上2种方法一般和通配符一起使用,常用的通配符有3个:*:匹配零个或多个字符...
- Python 中 字符串处理的高效方法,不允许你还不知道
-
以下是Python中字符串处理的高效方法,涵盖常用操作、性能优化技巧和实际应用场景,帮助您写出更简洁、更快速的代码:一、基础高效操作1.字符串拼接:优先用join()代替+原因:join()预...
- 正则表达式(Regex)在线调试工具-Regex101
-
前言在字符串查找处理程序中,正则表达式是一个不可忽略的处理方式。我们能够利用正则表达式轻松地做到检索、替换那些符合某个模(规则)的字符串。正则表达式有着很强的灵活性、逻辑性及功能性,可以迅速地用极简...
- pandas中使用excel的模糊匹配通配符,真香
-
前言在pandas中,实现如下的模糊匹配统计,要怎么做?简单:因为在pandas中可以把筛选和统计两种逻辑分开编写,所以代码清晰好用。问题在于pandas中要实现模糊匹配,只能使用正则表达...
- 5分钟掌握Python(十六)之正则表达式
-
1)引入re模块:eg:importre#设定一个常量a='两点水|twowater|liangdianshui|草根程序员|ReadingWithU'#正则表达式...
- 不允许你还不会的Python 文件与字符串处理高效技巧
-
掌握文件和字符串的高效处理技巧是Python编程中的重要能力。以下是一些专业级的优化技巧和实践方法:一、文件处理高效技巧1.文件读取优化1.1大文件逐行读取#标准方法(内存友好)withop...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (54)
- python安装路径 (54)
- python类型转换 (75)
- python进度条 (54)
- python的for循环 (56)
- python串口编程 (60)
- python写入txt (51)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python字典增加键值对 (53)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python qt (52)
- python人脸识别 (54)
- python斐波那契数列 (51)
- python多态 (60)
- python命令行参数 (53)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- centos7安装python (53)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)