当前位置:首页>python>从零开始学python-内置函数

从零开始学python-内置函数

  • 2026-09-22 13:42:42
从零开始学python-内置函数

Hi~新朋友,记得点上面蓝字(网虫root)关注哟~

前言:

   主要分享的是Python版本为Python3的内容。

一、什么是内置函数?

内置函数是Python语言自带的、无需导入任何模块即可直接使用的函数。Python提供了丰富的内置函数,可以完成各种常见任务,如输入输出、类型转换、数学运算等。

二、常用内置函数详解

1. print() - 输出函数

作用:将内容输出到控制台

语法:

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

参数说明:

  • value:要输出的值,可以有多个

  • sep:多个值之间的分隔符,默认为空格

  • end:输出结束时的字符,默认为换行符

  • file:输出目标,默认为标准输出(控制台)

  • flush:是否立即刷新输出,默认为False

示例:

基本输出print("Hello, Python!")  # 输出:Hello, Python!输出多个值print("苹果", "香蕉", "橙子")  # 输出:苹果 香蕉 橙子自定义分隔符print("2023", "10", "15", sep="-")  # 输出:2023-10-15不换行输出print("正在加载", end="...")print("完成")  # 输出:正在加载...完成

2. input() - 输入函数

作用:从控制台获取用户输入

语法:

input([prompt])

参数说明:

  • prompt:提示信息,可选

示例:

基本输入name = input("请输入你的名字:")print(f"你好,{name}!")获取数字输入(注意:input()返回的是字符串)age_str = input("请输入你的年龄:")age = int(age_str)  # 转换为整数print(f"明年你将 {age + 1} 岁")

3. len() - 长度函数

作用:返回对象的长度或元素个数

语法:

len(object)

示例:

字符串长度text = "Python"print(len(text))  # 输出:6列表长度fruits = ["苹果", "香蕉", "橙子"]print(len(fruits))  # 输出:3元组长度numbers = (1, 2, 3, 4, 5)print(len(numbers))  # 输出:5

4. type() - 类型函数

作用:返回对象的类型

语法:

type(object)

示例:

查看不同类型print(type(10))           # 输出:<class 'int'>print(type(3.14))         # 输出:<class 'float'>print(type("Hello"))      # 输出:<class 'str'>print(type([1, 2, 3]))    # 输出:<class 'list'>print(type(True))         # 输出:<class 'bool'>

5. int(), float(), str() - 类型转换函数

作用:在不同数据类型之间转换

语法:

int(x)      # 转换为整数float(x)    # 转换为浮点数str(x)      # 转换为字符串

示例:

转换为整数print(int("123"))      # 输出:123print(int(3.9))        # 输出:3(向下取整)转换为浮点数print(float("3.14"))   # 输出:3.14print(float(5))        # 输出:5.0转换为字符串print(str(100))        # 输出:"100"print(str(True))       # 输出:"True"综合示例price = "12.5"quantity = "3"total = float(price) * int(quantity)print(f"总价:{total}元")  # 输出:总价:37.5元

6. min() 和 max() - 最值函数

作用:返回序列中的最小值和最大值

语法:

min(iterable, *[, default=obj, key=func])max(iterable, *[, default=obj, key=func])

示例:

数字列表numbers = [5, 2, 8, 1, 9]print(min(numbers))  # 输出:1print(max(numbers))  # 输出:9字符串(按字母顺序)words = ["apple", "banana", "cherry"]print(min(words))    # 输出:appleprint(max(words))    # 输出:cherry直接比较多个值print(min(10, 20, 5, 30))  # 输出:5

7. sum() - 求和函数

作用:计算序列中所有元素的和

语法:

sum(iterable, start=0)

示例:

数字列表求和scores = [85, 92, 78, 90]total = sum(scores)print(f"总分:{total}")      # 输出:总分:345print(f"平均分:{total/len(scores)}")  # 输出:平均分:86.25从指定值开始求和numbers = [1, 2, 3]result = sum(numbers, 10)  # 10 + 1 + 2 + 3print(result)  # 输出:16

8. abs() - 绝对值函数

作用:返回数字的绝对值

语法:

abs(x)

示例:

print(abs(10))     # 输出:10print(abs(-10))    # 输出:10print(abs(0))      # 输出:0print(abs(-3.14))  # 输出:3.14

9. round() - 四舍五入函数

作用:对数字进行四舍五入

语法:

round(number, ndigits=None)

示例:

四舍五入到整数print(round(3.14))    # 输出:3print(round(3.75))    # 输出:4保留指定小数位数print(round(3.14159, 2))  # 输出:3.14print(round(2.675, 2))    # 输出:2.67负数也可以四舍五入print(round(-3.5))    # 输出:-4

10. sorted() - 排序函数

作用:返回排序后的新列表

语法:

sorted(iterable, key=None, reverse=False)

示例:

数字排序numbers = [3, 1, 4, 1, 5, 9, 2]sorted_numbers = sorted(numbers)print(sorted_numbers)  # 输出:[1, 1, 2, 3, 4, 5, 9]字符串排序(按字母顺序)fruits = ["banana", "apple", "cherry", "date"]sorted_fruits = sorted(fruits)print(sorted_fruits)  # 输出:['apple', 'banana', 'cherry', 'date']降序排序numbers = [3, 1, 4, 1, 5]desc_numbers = sorted(numbers, reverse=True)print(desc_numbers)  # 输出:[5, 4, 3, 1, 1]

【微语】简单的生活,不简单的幸福。

网虫root

期待你的

分享

点赞

在看

最新文章

随机文章