Python编程的函数—内置函数

简介: Python编程的函数—内置函数

Python编程的函数—内置函数
内置函数
Python提供大量功能强大的内置函数,即数学函数、随机函数、三角函数和字符串函数。下面进行详细讲解。

数学函数
数学函数用于各种数学运算。数学函数及意义如下表所示:

数学函数及意义

image.png

示例代码如下:

import math  # 导入math标准库
print('-123的绝对值:', abs(-123))
print('15.2的上入整数:', math.ceil(15.2))
print('15.2的下舍整数:', math.floor(15.2))
print('15.2的四舍五入整数:', round(15.2))
print('e的3次幂:', math.exp(3))
print('以10为基数的1000的对数:', math.log10(1000))
print('4的5次方:', math.pow(4, 5))
print('64的平方根:', math.sqrt(64))
print('96、6、13、68数中的最大数:', max(96, 6, 13, 68))
print('96、6、13、68数中的最小数:', min(96, 6, 13, 68))

运行结果如下:

-123的绝对值: 123
15.2的上入整数: 16
15.2的下舍整数: 15
15.2的四舍五入整数: 15
e的3次幂: 20.08553692318766810为基数的1000的对数: 3.0
45次方: 1024.0
64的平方根: 8.0
9661368数中的最大数: 96
9661368数中的最小数: 6

随机函数
随机函数可以用于数学、游戏、安全等领域中,还经常被嵌入算法中,用于提高算法效率,并提高程序的安全性。随机函数及意义如下表所示:

随机函数及意义
image.png
示例代码如下:

import random  # 导入random标准库
print('从一个列表中随机返回一个元素:', random.choice(['红色', '蓝色', '绿色', '黄色']))
print('从一个元组中随机返回一个元素:', random.choice(('A级', 'B级', 'C级', 'D级', 'E级')))
print('从字符串中随机返回一个字符:', random.choice('Python!'))
print()
print('从一个列表中随机返回两个元素:', random.sample(['红色', '蓝色', '绿色', '黄色'], 2))
print('从一个元组中随机返回三个元素:', random.sample(('A级', 'B级', 'C级', 'D级', 'E级'), 3))
print('从字符串中随机返回四个字符:', random.sample('Python!', 4))
print()
print('从1~100之间随机产生一个整数:', random.randint(1, 100))
print('从80~100之间随机产生一个整数:', random.randint(80, 100))
print()
print('从1~100之间随机产生一个浮点数:', random.uniform(1, 100))
print('从80~100之间随机产生一个浮点数:', random.uniform(80, 100))
print()
print('从1~100之间随机产生一个整数:', random.randrange(1, 100))
print('从1~99之间随机产生一个奇数:', random.randrange(1, 99, 2))
print('从3~99之间随机产生一个3的倍数:', random.randrange(3, 99, 3))
print()
print('在0~1之间产生一个随机数:', random.random())
print()
random.seed()
print('属于默认种子生成随机数:', random.random())
random.seed(10)
print('使用整数种子生成随机数:', random.random())
random.seed('hello', 2)
print('使用字符串种子生成随机数:', random.random())
print()
list1 = [12, 15, 11, 99,32,86,52,33]
print('列表中原来的数据:', list1)
random.shuffle(list1)
print('随机排序列表后的数据:', list1)

运行结果如下:

从一个列表中随机返回一个元素: 蓝色
从一个元组中随机返回一个元素: C级
从字符串中随机返回一个字符: h

从一个列表中随机返回两个元素: ['黄色', '绿色']
从一个元组中随机返回三个元素: ['B级', 'A级', 'C级']
从字符串中随机返回四个字符: ['y', '!', 't', 'o']1100之间随机产生一个整数: 5480100之间随机产生一个整数: 991100之间随机产生一个浮点数: 9.62325134906032280100之间随机产生一个浮点数: 82.711855098339611100之间随机产生一个整数: 18199之间随机产生一个奇数: 1399之间随机产生一个3的倍数: 4501之间产生一个随机数: 0.589324529626653

属于默认种子生成随机数: 0.7931462265273019
使用整数种子生成随机数: 0.5714025946899135
使用字符串种子生成随机数: 0.3537754404730722

列表中原来的数据: [12, 15, 11, 99, 32, 86, 52, 33]
随机排序列表后的数据: [52, 32, 99, 86, 12, 11, 33, 15]

三角函数
​ 三角函数及意义
image.png
输入代码如下:

import math  # 导入math标准库
print('acos(0.64)的值是:', math.acos(0.64))
print('acos(0)的值是:', math.acos(0))
print('asin(-1)的值是:', math.asin(-1))
print('asin(1)的值是:', math.asin(1))
print('atan(10)的值是:', math.atan(10))
print('atan2(5, 5)的值是:', math.atan2(5, 5))
print('atan2(-10, 10)的值是:', math.atan2(-10, 10))
print()
print('cos(3)的值是:', math.cos(3))
print('cos(-3)的值是:', math.cos(-3))
print('hypot(3,2)的值是:', math.hypot(3,2))
print('hypot(-3,3)的值是:', math.hypot(-3,3))
print('sin(3)的值是:', math.sin(3))
print('sin(-3)的值是:', math.sin(-3))
print('(tan(3)的值是:)', math.tan(3))
print('(tan(-3)的值是:)', math.tan(-3))
print()
print('degrees(3)的值是:', math.degrees(3))
print('degrees(-3)的值是:', math.degrees(-3))
print('radians(3)的值是:', math.radians(3))
print('radians(-3)的值是:', math.radians(-3))

运行结果如下:

acos(0.64)的值是: 0.8762980611683406
acos(0)的值是: 1.5707963267948966
asin(-1)的值是: -1.5707963267948966
asin(1)的值是: 1.5707963267948966
atan(10)的值是: 1.4711276743037345
atan2(5, 5)的值是: 0.7853981633974483
atan2(-10, 10)的值是: -0.7853981633974483

cos(3)的值是: -0.9899924966004454
cos(-3)的值是: -0.9899924966004454
hypot(3,2)的值是: 3.605551275463989
hypot(-3,3)的值是: 4.242640687119285
sin(3)的值是: 0.1411200080598672
sin(-3)的值是: -0.1411200080598672
(tan(3)的值是:) -0.1425465430742778
(tan(-3)的值是:) 0.1425465430742778

degrees(3)的值是: 171.88733853924697
degrees(-3)的值是: -171.88733853924697
radians(3)的值是: 0.05235987755982989
radians(-3)的值是: -0.05235987755982989

字符串函数
​ 字符串函数及意义
image.png
image.png
image.png
示例代码如下:

str = 'how are you?'
print('将字符串的第一个字母转换为大写:',str.capitalize())
str = 'www.baidu.com'
print('指定的宽度50并且居中的字符串:',str.center(50, '*'))
str = 'www.qq.com'
sub = 'q'
print('返回字符串中某字符出现的次数:', str.count(sub))
print()
print('this is \tstring example....wow!!!')
print('原始字符串:', str)
print('替换 \\t 符号:', str.expandtabs(16))
print()
str1 = 'I like python'
str2 = 'Pyth';
print('在str1字符串中查找str2:', str1.find(str2))
print('在str1字符串中查找str2, 从第6个字符串开始:', str1.find(str2, 5))
print('在str1字符串中查找str2,从第11个字符开始:', str1.find(str2, 10))
print()
str = 'qd2019' # 字符串只有字母和数字
print(str.isalnum())
str = 'www.163.com' # 字符串除了字母和数字,还有小数点
print(str.isalnum())
str = 'python' #字符串只有字母
print(str.isalpha()) 
str = 'www.baidu.com' # 字符串除了字母,还有别的字符
print(str.isalpha())
str = '123456'
print(str.isdigit())
str = 'I like Python!'
print(str.isdigit())
str = 'GOOD, python'    # 字符串有大小写字母
print(str.islower())

str = 'good, python' # 字母中只有小写字母
print(str.islower())
str = '        '
print(str.isspace())
str = 'I like python!'
print(str.isspace())
str = 'I LIKE PYTHON' 
print(str.isupper())
str = 'I Like Python!'
print(str.isupper())
print()
s1 = '-'
s2 = ''
seq = ('p', 'y', 't', 'h', 'o', 'n') #字符串序列
print(s1.join(seq))
print(s2.join(seq))
print()
str = 'python'
print('字符串长度:', len(str))
l = [1, 2, 3, 4, 5]
print('列表元素个数:', len(l))
print()
str = 'I like python!'
print('左对齐:', str.ljust(50, '*'))
print('右对齐:', str.rjust(50, '*'))
print()
str = '         I like python!      '
print('删除字符串左边的空格:', str.lstrip())
print('删除字符串右边的空格:', str.rstrip())
print('删除字符串左右两边的空格:', str.strip())
print()

str = 'python'
print('最大字符:', max(str))
print('最小字符:', min(str))
str = 'www.gdlike.com'
print('网站原来的网址:',str)
print('网站新的网址:', str.replace('www.qdlike.com', 'www.chinalike.com'))
str = 'I like python!'
print(str.split())
print('ab c\n\nde fg\rkl\r\n'.splitlines())
print()
str = 'I like python!'
print('将字符串中大写转为小写,小写转为大写:',str.swapcase())
print('转换字符串中的小写字母为大写:', str.upper())
print('转换字符串中的大写字母为小写:', str.lower())

运行结果如下:

将字符串的第一个字母转换为大写: How are you?
指定的宽度50并且居中的字符串: ******************www.baidu.com*******************
返回字符串中某字符出现的次数: 2

原始字符串: this is     string example....wow!!!
替换 \t 符号: this is         string example....wow!!!
使用16个空格替换 \t 符号: this is         string example....wow!!!

在str1字符串中查找str2: 7
在str1字符串中查找str2, 从第6个字符串开始: 7
在str1字符串中查找str2,从第11个字符开始: -1

True
False
True
False
True
False
False
True
True
False
True
False

p-y-t-h-o-n
python

字符串长度: 6
列表元素个数: 5

左对齐: I like python!************************************
右对齐: ************************************I like python!

删除字符串左边的空格: I like python!      
删除字符串右边的空格:          I like python!
删除字符串左右两边的空格: I like python!

最大字符: y
最小字符: h
网站原来的网址: www.gdlike.com
网站新的网址: www.gdlike.com
['I', 'like', 'python!']
['ab c', '', 'de fg', 'kl']

将字符串中大写转为小写,小写转为大写: i LIKE PYTHON!
转换字符串中的小写字母为大写: I LIKE PYTHON!
转换字符串中的大写字母为小写: i like python!
相关文章
|
1天前
|
数据挖掘 索引 Python
Python数据挖掘编程基础3
字典在数学上是一个映射,类似列表但使用自定义键而非数字索引,键在整个字典中必须唯一。可以通过直接赋值、`dict`函数或`dict.fromkeys`创建字典,并通过键访问元素。集合是一种不重复且无序的数据结构,可通过花括号或`set`函数创建,支持并集、交集、差集和对称差集等运算。
14 9
|
1天前
|
人工智能 小程序 API
文字转语音神器+Python编程搞定语音报时小程序
文字转语音神器+Python编程搞定语音报时小程序
10 2
|
1天前
|
Python
Python编程的循环结构小示例(二)
Python编程的循环结构小示例(二)
|
1天前
|
存储 索引 Python
Python编程的常用数据结构—列表
Python编程的常用数据结构—列表
|
1天前
|
数据挖掘 Python
Python数据挖掘编程基础8
在Python中,默认环境下并不会加载所有功能,需要手动导入库以增强功能。Python内置了诸多强大库,例如`math`库可用于复杂数学运算。导入库不仅限于`import 库名`,还可以通过别名简化调用,如`import math as m`;也可指定导入库中的特定函数,如`from math import exp as e`;甚至直接导入库中所有函数`from math import *`。但需注意,后者可能引发命名冲突。读者可通过`help('modules')`查看已安装模块。
6 0
|
1天前
|
人工智能 数据挖掘 Serverless
Python数据挖掘编程基础
函数式编程中的`reduce`函数用于对可迭代对象中的元素进行累积计算,不同于逐一遍历的`map`函数。例如,在Python3中,计算n的阶乘可以使用`reduce`(需从`funtools`库导入)实现,也可用循环命令完成。另一方面,`filter`函数则像一个过滤器,用于筛选列表中符合条件的元素,同样地功能也可以通过列表解析来实现。使用这些函数不仅使代码更加简洁,而且由于其内部循环机制,执行效率通常高于普通的`for`或`while`循环。
5 0
|
1天前
|
分布式计算 数据挖掘 Serverless
Python数据挖掘编程基础6
函数式编程(Functional Programming)是一种编程范型,它将计算机运算视为数学函数计算,避免程序状态及易变对象的影响。在Python中,函数式编程主要通过`lambda`、`map`、`reduce`、`filter`等函数实现。例如,对于列表`a=[5,6,7]`,可通过列表解析`b=[i+3 for i in a]`或`map`函数`b=map(lambda x:x+3, a)`实现元素加3的操作,两者输出均为`[8,9,10]`。尽管列表解析代码简洁,但其本质仍是for循环,在Python中效率较低;而`map`函数不仅功能相同,且执行效率更高。
5 0
|
1天前
|
数据挖掘 Python
Python数据挖掘编程基础5
函数是Python中用于提高代码效率和减少冗余的基本数据结构,通过封装程序逻辑实现结构化编程。用户可通过自定义或函数式编程方式设计函数。在Python中,使用`def`关键字定义函数,如`def pea(x): return x+1`,且其返回值形式多样,可为列表或多个值。此外,Python还支持使用`lambda`定义简洁的行内函数,例如`c=lambda x:x+1`。
7 0
|
1天前
|
数据挖掘 Python
Python数据挖掘编程基础
判断与循环是编程的基础,Python中的`if`、`elif`、`else`结构通过条件句来执行不同的代码块,不使用花括号,依赖缩进区分代码层次。错误缩进会导致程序出错。Python支持`for`和`while`循环,`for`循环结合`range`生成序列,简洁直观。正确缩进不仅是Python的要求,也是一种良好的编程习惯。
5 0
|
1天前
|
Java C++ Python
30天拿下Python之函数
30天拿下Python之函数
下一篇
无影云桌面