Python的内置函数:使用方法和示例
Python是一种简单、易学、面向对象、高级语言。它具有高效和快速的开发功能,因此很受广大程序员的追捧。Python内置函数是Python内置的函数库,在Python中拥有很高的使用频率,可以提高编程效率和开发速度。本文将列举Python内置函数的使用方法和示例。
### len()
函数len()用于返回对象的长度或项目个数。长度可用于字符串,列表、元组和字典等数据类型,并返回其项目的数量或个数。
**使用方法:**
len(Object)
**示例:**
str = "Python"
print(len(str)) # 输出 6
list = [1, 2, 3, 4, 5]
print(len(list)) # 输出 5
tuple = (1, 2, 3, 4, 5)
print(len(tuple)) # 输出 5
dict = {'name': '张三', 'age': 18, 'gender': '男'}
print(len(dict)) # 输出 3
### range()
函数range()生成一个指定范围的数字序列。它经常用于可迭代对象的循环(如for循环)中。可用于生成整数列表。
**使用方法:**
range(start, end, step)
start:数字序列的起始值,默认值为0。
end:数字序列的结束值,不包含在数字序列中。
step:数字序列的步长,默认值为1。
**示例:**
# 构建从0到5(不包含)的整数序列
for i in range(0, 5, 1):
print(i) # 输出 0,1,2,3,4
# 构建从5到10(不包含)的偶数序列
for i in range(5, 10, 2):
print(i) # 输出 5,7,9
### type()
函数type()用于返回对象的类型。
**使用方法:**
type(Object)
**示例:**
a = 5
print(type(a)) # 输出 <class 'int'>
b = "Python"
print(type(b)) # 输出 <class 'str'>
c = [1, 2, 3, 4, 5]
print(type(c)) # 输出 <class 'list'>
d = (1, 2, 3, 4, 5)
print(type(d)) # 输出 <class 'tuple'>
e = {'name': '张三', 'age': 18, 'gender': '男'}
print(type(e)) # 输出 <class 'dict'>
### print()
函数print()用于在屏幕上输出指定的文本或变量值。
**使用方法:**
print(*objects, sep=' ', end='
', file=sys.stdout, flush=False)
*objects:表示要输出的一个或多个对象,用逗号隔开。
sep:表示输出多个对象时使用的分割符,默认是一个空格。
end:表示输出完所有对象后的结束符号,默认是一个换行符。
file:表示输出的文件,如sys.stdout表示给标准输出打印。
flush:表示是否冲洗输出流。
**示例:**
a = "Hello"
b = "World!"
print(a, b) # 输出 Hello World!
print(1, 2, 3, 4, 5, sep=",") # 输出 1,2,3,4,5
print("Python", end=" ") # 输出 Python,后面没有换行
print("Hello", "World!", file=open('output.txt', 'w')) # 输出到文件output.txt
### input()
函数input()用于从用户处获取输入,并返回一个字符串。
**使用方法:**
input(prompt)
prompt:表示用于提示的字符串。
**示例:**
name = input("请输入你的姓名:")
print("你的姓名是:", name)
### int()
函数int()用于将指定的数据类型转换为整数类型。
**使用方法:**
int(Object, base)
Object:表示要转换的对象,可以是字符串或数值。
base:表示要使用的进制类型,默认值为10。
**示例:**
string = "123" print(int(string)) # 输出 123 float_num = 3.14 print(int(float_num)) # 输出 3 octal_num = '0o10' print(int(octal_num, 8)) # 输出 8,将八进制数转换为十进制整数 hex_num = '0x10' print(int(hex_num, 16)) # 输出 16,将十六进制数转换为十进制整数
### str()
函数str()用于将指定的对象转换为字符串。
**使用方法:**
str(Object)
Object:表示要转换的对象。
**示例:**
num = 123 print(str(num)) # 输出 '123' list = [1, 2, 3, 4, 5] print(str(list)) # 输出 '[1, 2, 3, 4, 5]'
### float()
函数float()用于将指定的数据类型转换为浮点数。
**使用方法:**
float(Object)
Object:表示要转换的对象,可以是整数或字符串。
**示例:**
int_num = 123 print(float(int_num)) # 输出 123.0 string = "3.14" print(float(string)) # 输出 3.14
### min()
函数min()用于返回给定序列或参数的最小值。
**使用方法:**
min(Object1, Object2, ... , ObjectN)
Object1-ObjectN:表示要比较的一系列对象。
**示例:**
list = [1, 2, 3, 4, 5] print(min(list)) # 输出 1 str = "Python" print(min(str)) # 输出 'P' tuple = (1, 2, 3, 4, 5) print(min(tuple)) # 输出 1
### max()
函数max()用于返回给定序列或参数的最大值。
**使用方法:**
max(Object1, Object2, ... , ObjectN)
Object1-ObjectN:表示要比较的一系列对象。
**示例:**
list = [1, 2, 3, 4, 5] print(max(list)) # 输出 5 str = "Python" print(max(str)) # 输出 'y' tuple = (1, 2, 3, 4, 5) print(max(tuple)) # 输出 5
### abs()
函数abs()返回指定数字的绝对值。
**使用方法:**
abs(Object)
Object:表示要取绝对值的数字。
**示例:**
num1 = -1 num2 = 2.5 print(abs(num1)) # 输出 1 print(abs(num2)) # 输出 2.5
### sum()
函数sum()返回给定数值序列或列表的总和。
**使用方法:**
sum(Object)
Object:表示要计算总和的数值序列或列表。
**示例:**
list1 = [1, 2, 3, 4, 5] print(sum(list1)) # 输出 15 tuple = (1, 2, 3, 4, 5) print(sum(tuple)) # 输出 15
### pow()
函数pow()用于返回指定数字的指定次方值。
**使用方法:**
pow(x, y)
x:表示基数。
y:表示指数。
**示例:**
num1 = 2 num2 = 3 print(pow(num1, num2)) # 输出 8
### round()
函数round()返回一个浮点数字四舍五入后的值。
**使用方法:**
round(number, ndigits)
number:表示要四舍五入的数字。
ndigits:表示要保留的小数位数,默认值为0。
**示例:**
num1 = 3.1415926 print(round(num1, 2)) # 输出 3.14 num2 = 3.645 print(round(num2)) # 输出 4
### sorted()
函数sorted()用于对序
