百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

Introduction to Common Built-in Functions in Python 常用内置函数

off999 2025-05-22 12:42 24 浏览 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中最常用的一些内置函数。在代码中练习使用它们,你会更熟悉它们的工作方式。记住,最好的学习方法是实践!


专业词汇及不常用词汇表

  1. absolute value, /'aebslut 'vaelju/, n,绝对值
  2. Boolean, /'bulin/, adj/n,布尔(值)
  3. quotient, /'kwont/, n,商
  4. remainder, /r'mendr/, n,余数
  5. Unicode, /'junkod/, n,统一码(字符编码标准)
  6. hexadecimal, /heks'desml/, adj/n,十六进制(的)
  7. octal, /'ɑktl/, adj/n,八进制(的)
  8. modulo, /'mɑdlo/, n,模(运算)
  9. iterable, /'trbl/, n,可迭代对象
  10. truncates, /tr'kets/, v,截断
  11. bankers rounding, /'baekrz 'rand/, n,银行家舍入(一种四舍五入规则)

相关推荐

win10激活在哪里查看(win10激活时间在哪里看)

在Windows10中,您可以通过以下方法查看激活状态:方法1:使用“设置”应用1.点击屏幕左下角的“开始”按钮,然后点击“设置”(齿轮图标)。2.在设置窗口中,点击“系统”图标。3.在“系统...

官方win10dll文件修复工具(官方win7dll文件修复工具)

当电脑丢失dll文件时,可以采用以下几种方法进行一键修复:从回收站还原:如果是不小心误删了一些计算机文件,导致电脑出现异常的情况时,首先就可以去回收站找回dll文件,如果文件还在,就可以通过还原操作来...

qq所有历史旧版本大全(qq历史版本一览表)

有2种方法。一种是:你是QQ会员。你可以把旧版打开,聊天记录上传。然后打开新的QQ,下载。第2种是:你在硬盘上装了QQ软件,然后你就点卸载(uninst),把原来的卸了。然后按原位置覆盖,装上06版。...

电脑显示器分辨率怎么调(显示分辨率无法调整)

1、以win7为例,首先右键点击桌面,在右键菜单中直接显示了屏幕分辨率的选项,用鼠标点击一下这个选项。2、在分辨率设置选项页面中,有一个分辨率的选项,点击一下这个选项,上面默认显示的数值是你现在的屏幕...

8系统(8系统点检控制包含什么)

WIndows8系统是微软目前最新的操作系统,Moto的图形界面设计,使很多已经习惯于早期windows系统的用户难以接受,Windows8是一个向平板和桌面系统妥协的产物,存在着相当多的利弊。...

电脑软件管家(电脑软件管家在哪里找到)

电脑管家有着最大的安全云库,全新的杀毒引擎,深度清理电脑垃圾,为电脑重回巅峰状态,更有账号宝专版,10倍提升QQ防盗号能力,是很好用的。就自己而言,在电脑上用的是腾讯电脑管家这个第三方系统安全软件,管...

office2010破解(office2010破解密钥)
  • office2010破解(office2010破解密钥)
  • office2010破解(office2010破解密钥)
  • office2010破解(office2010破解密钥)
  • office2010破解(office2010破解密钥)
迅雷种子搜索器(迅雷种子搜索器手机版下载)

    迅雷种子搜索方法:    1.在开始菜单栏或者到文件的安装路径文件夹中找到P2P种子搜索器。&nb...

手机怎么解压文件(苹果手机怎么解压文件)

手机解压文件方法:1、首先,在手机中找到文件管理,打开文件管理。2、打开文件管理之后找到压缩包,然后打开。3、打开安装包之后,选择需要的文件,。4、接下来找到“解压至”,点击“解压至”。5、点击之后,...

虚拟机安装centos7(虚拟机安装centos7图形界面)

安装CentOS7在虚拟机中,您可以按照以下步骤操作:1.下载CentOS7的ISO映像文件。2.打开虚拟机软件(如VMware、VirtualBox等)并创建一个新的虚拟机。3.在虚拟机创...

电脑老是重启(电脑老是重启什么原因造成的)

电脑由于工作环境积尘与空气湿度过大,经常使主板的接插件部分受潮产生氧化;特别是内存条插座、PCI扩展槽、键盘鼠标接口、LOT接口、CMOS电池压盒、ATX电源插座等。一旦它们出现接触不良现象,很容易出...

win8系统怎么重装系统(win8.1系统重装教程)

1、修改Cortana资源占用:按Windows按钮,输入regedit,打开注册表编辑器,找到以下路径:HKEY_LOCAL_MACHINE—SYSTEM—CurrentControlSet—Ser...

手机163邮箱app下载(163邮箱下载手机版官网 app)

163邮箱登录首页入口为http://mail.163.com/网易163免费邮箱--中文邮箱第一品牌.容量自动翻倍,支持50兆附件,免费开通手机号码邮箱赠送3G超大附件服务.支持各种客户端软件收发,...

win10此电脑怎么放在桌面上(wind10此电脑放桌面)
win10此电脑怎么放在桌面上(wind10此电脑放桌面)

步骤/方式1右键单击桌面空白处,点击个性化。步骤/方式2点击更改桌面图标。步骤/方式3把计算机勾选上。步骤/方式4即可把此电脑图标显示在桌面上。...

2025-11-08 14:03 off999

电脑配置怎么看在电脑上(电脑配置在电脑里怎么看)

查看电脑配置的方法有多种,以下是一些常见的方法:直接查看:在电脑桌面或操作系统中,找到“我的电脑”或“此电脑”,右键点击并选择“属性”,即可查看电脑的基本配置信息,包括CPU型号、内存大小、硬盘类型和...

取消回复欢迎 发表评论: