?? 博客主頁:水滴技術
?? 訂閱專欄:Python 入門核心技術
?? 支持水滴:點贊?? + 收藏? + 留言??
大家好,我是水滴~~
Python是一種高級編程語言,具有很多強大的特性,其中之一就是內(nèi)置函數(shù)。Python內(nèi)置函數(shù)是指在Python解釋器中可以直接使用的函數(shù),無需導入任何模塊或庫。Python內(nèi)置函數(shù)包含了很多常用的函數(shù),可以快速地完成各種操作。本文將介紹Python內(nèi)置函數(shù)的用法,幫助初學者更好地掌握Python編程。
一、數(shù)學相關函數(shù)
Python內(nèi)置函數(shù)中有很多與數(shù)學相關的函數(shù),它們可以對數(shù)字進行各種處理。下面是一些常用的數(shù)學函數(shù):
-
abs(x):返回x的絕對值。
-
divmod(x, y):返回x除以y的商和余數(shù)。
-
round(x, n):返回x的四舍五入值,n表示保留的小數(shù)位數(shù)。
-
pow(x, y):返回x的y次方。
-
sum(iterable [, start]):返回可迭代對象中所有元素的和,start表示可選的起始值。
下面是一些數(shù)學函數(shù)的使用示例:
# abs函數(shù)
print(abs(-10)) # 輸出:10
# divmod函數(shù)
print(divmod(10, 3)) # 輸出:(3, 1)
# round函數(shù)
print(round(3.1415926, 2)) # 輸出:3.14
# pow函數(shù)
print(pow(2, 3)) # 輸出:8
# sum函數(shù)
print(sum([1, 2, 3, 4, 5])) # 輸出:15
二、序列相關函數(shù)
Python中有很多序列相關的函數(shù),它們可以對序列進行各種處理。下面是一些常用的序列函數(shù):
-
len(s):返回序列s(字符串、列表或元組)的長度。
-
max(iterable):返回可迭代對象中的最大值。
-
min(iterable):返回可迭代對象中的最小值。
-
sorted(iterable [, key][, reverse]):返回一個排序后的序列,key表示可選的排序函數(shù),reverse表示可選的排序方向。
-
reversed(seq):返回一個反轉(zhuǎn)后的序列。
-
enumerate(iterable, start=0):返回一個枚舉對象,包含每個元素的索引和值。
下面是一些序列函數(shù)的使用示例:
# len函數(shù)
print(len("hello")) # 輸出:5
# max函數(shù)
print(max([1, 2, 3, 4, 5])) # 輸出:5
# min函數(shù)
print(min([1, 2, 3, 4, 5])) # 輸出:1
# sorted函數(shù)
print(sorted([3, 1, 4, 1, 5, 9, 2, 6, 5])) # 輸出:[1, 1, 2, 3, 4, 5, 5, 6, 9]
# reversed函數(shù)
print(list(reversed([1, 2, 3, 4, 5]))) # 輸出:[5, 4, 3, 2, 1]
# enumerate函數(shù)
for i, v in enumerate(["apple", "banana", "orange"]):
print(i, v)
# 輸出:
# 0 apple
# 1 banana
# 2 orange
三、類型轉(zhuǎn)換函數(shù)
Python中有很多類型轉(zhuǎn)換函數(shù),可以將一個類型轉(zhuǎn)換為另一個類型。下面是一些常用的類型轉(zhuǎn)換函數(shù):
-
int(x):將x轉(zhuǎn)換為整數(shù)。
-
float(x):將x轉(zhuǎn)換為浮點數(shù)。
-
str(x):將x轉(zhuǎn)換為字符串。
-
bool(x):將x轉(zhuǎn)換為布爾值。
-
list(iterable):將可迭代對象轉(zhuǎn)換為列表。
-
tuple(iterable):將可迭代對象轉(zhuǎn)換為元組。
-
set(iterable):將可迭代對象轉(zhuǎn)換為集合。
-
dict(iterable):將可迭代對象轉(zhuǎn)換為字典。
下面是一些類型轉(zhuǎn)換函數(shù)的使用示例:
# int函數(shù)
print(int("123")) # 輸出:123
# float函數(shù)
print(float("3.14")) # 輸出:3.14
# str函數(shù)
print(str(123)) # 輸出:"123"
# bool函數(shù)
print(bool("")) # 輸出:False
# list函數(shù)
print(list("hello")) # 輸出:['h', 'e', 'l', 'l', 'o']
# tuple函數(shù)
print(tuple([1, 2, 3])) # 輸出:(1, 2, 3)
# set函數(shù)
print(set([1, 2, 3, 2, 1])) # 輸出:{1, 2, 3}
# dict函數(shù)
print(dict([("apple", 1), ("banana", 2), ("orange", 3)])) # 輸出:{"apple": 1, "banana": 2, "orange": 3}
四、文件操作函數(shù)
Python中有一些文件操作函數(shù),可以對文件進行讀寫操作。下面是一些常用的文件操作函數(shù):
-
open(file, mode=‘r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True):打開一個文件并返回文件對象。
-
close():關閉文件。
-
read(size=-1):從文件中讀取指定字節(jié)數(shù)的數(shù)據(jù)。
-
write(s):將數(shù)據(jù)寫入文件。
下面是一些文件操作函數(shù)的使用示例:
# 打開文件
f = open("test.txt", "w")
# 寫入數(shù)據(jù)
f.write("hello\n")
f.write("world\n")
# 關閉文件
f.close()
# 打開文件并讀取數(shù)據(jù)
f = open("test.txt", "r")
print(f.read())
# 輸出:
# hello
# world
f.close()
五、輸入輸出函數(shù)
Python中有一些輸入輸出函數(shù),可以處理控制臺輸入輸出。下面是一些常用的輸入輸出函數(shù):
-
print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False):將多個對象以指定分隔符連接,并輸出到控制臺。
-
input(prompt=None):從控制臺讀取用戶輸入的字符串。
下面是一些輸入輸出函數(shù)的使用示例:
# print函數(shù)
print("hello", "world", sep="-") # 輸出:hello-world
# input函數(shù)
name = input("請輸入您的姓名:")
print("您好,", name)
六、其他常用函數(shù)
除了以上分類,Python中還有一些其他常用的函數(shù),例如:
-
range(stop):返回一個包含0到stop-1之間所有整數(shù)的序列。
-
zip(*iterables):將多個可迭代對象中的元素按照對應位置組合成元組。
-
map(function, iterable):對可迭代對象中的每個元素應用函數(shù),返回一個迭代器。
-
filter(function, iterable):對可迭代對象中的每個元素應用函數(shù),返回一個包含符合條件元素的迭代器。
-
reduce(function, iterable[, initializer]):對可迭代對象中的元素應用函數(shù),返回一個累加的結(jié)果。
下面是一些其他常用函數(shù)的使用示例:
# range函數(shù)
for i in range(5):
print(i)
# 輸出:
# 0
# 1
# 2
# 3
# 4
# zip函數(shù)
a = [1, 2, 3]
b = ["apple", "banana", "orange"]
for x, y in zip(a, b):
print(x, y)
# 輸出:
# 1 apple
# 2 banana
# 3 orange
# map函數(shù)
def square(x):
return x ** 2
print(list(map(square, [1, 2, 3, 4, 5]))) # 輸出:[1, 4, 9, 16, 25]
# filter函數(shù)
def is_even(x):
return x % 2 == 0
print(list(filter(is_even, [1, 2, 3, 4, 5]))) # 輸出:[2, 4]
# reduce函數(shù)
from functools import reduce
def add(x, y):
return x + y
print(reduce(add, [1, 2, 3, 4, 5])) # 輸出:15
系列文章
?? Python 基礎(一):初識 Python
?? Python 基礎(二):搭建 Python 開發(fā)環(huán)境
?? Python 基礎(三):Python 集成開發(fā)工具 IDLE
?? Python 基礎(四):基礎語法
?? Python 基礎(五):變量與常量
?? Python 基礎(六):基本數(shù)據(jù)類型
?? Python 基礎(七):常用運算符
?? Python 基礎(八):流程控制語句
?? Python 基礎(九):列表
?? Python 基礎(十):元組
?? Python 基礎(十一):集合
?? Python 基礎(十二):字典
?? Python 基礎(十三):函數(shù)
?? Python 基礎(十四):類和對象
?? Python 基礎(十五):模塊
?? Python 基礎(十六):包
?? Python 基礎(十七):庫
?? Python 基礎(十八):異常處理文章來源:http://www.zghlxwxcb.cn/news/detail-665071.html
熱門專欄
?? 《Python入門核心技術》
?? 《IDEA 教程:從入門到精通》
?? 《Java 教程:從入門到精通》
?? 《MySQL 教程:從入門到精通》
?? 《大數(shù)據(jù)核心技術從入門到精通》文章來源地址http://www.zghlxwxcb.cn/news/detail-665071.html
到了這里,關于Python 基礎(十九):內(nèi)置函數(shù)大全的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!