021.Python的内置函数
内置函数
1 abs 绝对值函数
res = abs(-9.9867)
print(res)
执行
[root@node10 python]# python3 test.py
9.9867
2 round 四舍六入五凑偶
- n.5 n为偶数则舍去 n.5 n为奇数,则进一
- 奇进偶不进
res = round(3.5)
print(res)
res = round(12.5)
print(res)
执行
[root@node10 python]# python3 test.py
4
12
3 sum 计算一个序列得和
listvar = [1,2,34,34,43,32,5]
total = 0
for i in listvar:
total += i
print(total) res = sum(listvar)
print(res)
执行
[root@node10 python]# python3 test.py
151
151
4 max获取一个序列里边的最大值
listvar = [1,2,34,34,43,32,5]
res = max(listvar)
print(res)
执行
[root@node10 python]# python3 test.py
43
5min获取一个序列里边的最小值
listvar = [1,2,34,34,43,32,5]
res = min(listvar)
print(res)
执行
[root@node10 python]# python3 test.py
1
6 sort找出最大值和最小值
listvar = [1,2,34,34,43,32,5]
listvar.sort()
print(listvar)
res_min = listvar[0]
res_max = listvar[-1]
print(res_min)
print(res_max)
执行
[root@node10 python]# python3 test.py
[1, 2, 5, 32, 34, 34, 43]
1
43
找出岁数最小的元组
listvar = [("张三",45),("李四",38),("王五",18),("田七",120)]
def func(n):
#("张三",45)
return n[-1]
res = min(listvar,key=func)
print(res)
执行
[root@node10 python]# python3 test.py
('王五', 18)
执行过程
("王五",18 扔到func当中 return 18
首先把("张三",45) 扔到func当中 return 45
("李四",38) 扔到func当中 return 38
("田七",120) 扔到func当中 return 120
按照右侧实际年龄进行排序,找出最小的那个数,直接把元组返回.
7 pow 计算某个数值的x次方
res = pow(2,3)
print (res)
res = pow(4,20)
print(res)
'''pow(可以接受第三个可选参数 , 第三个参数是用来取余的)'''
res= pow(2,3,3)
print(res)
执行
[root@node10 python]# python3 test.py
8
1099511627776
2
8 range产生指定范围数据的可迭代对象
res = range(10)
print(res)
for i in range(10):
print(i)
for i in range(1,8):
print(i)
for i in range(1,16,3):
print(i) for i in range(10,0,-1):
print(i)
执行
[root@node10 python]# python3 test.py
range(0, 10)
0
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
1
4
7
10
13
10
9
8
7
6
5
4
3
2
1
9 bin将10进制数据转化为二进制
res = bin(15)
print(res)
执行
[root@node10 python]# python3 test.py
0b1111
10 oct将10进制数据转化为八进制
res = oct(8)
print(res)
执行
[root@node10 python]# python3 test.py
0o10
11 hex将10进制数据转化为16进制
res = hex(16)
print(res)
执行
[root@node10 python]# python3 test.py
0x10
12 chr将ASCII编码转换为字符
res = chr(97)
print(res)
执行
[root@node10 python]# python3 test.py
a
13 ord将字符转换为ASCII编码
res = ord("A")
print(res)
执行
[root@node10 python]# python3 test.py
65
14 eval 将字符串当作python代码执行
res = "print(123)"
print(res)
eval(res)
res = "print('sdfsdf')"
print(res)
eval(res)
res = "print(\"sdfsdf\")"
print(res)
eval(res)
执行
[root@node10 python]# python3 test.py
print(123)
123
print('sdfsdf')
sdfsdf
print("sdfsdf")
sdfsdf
15 exec将字符串当作python代码执行(功能更强大)
res = """
for i in range(10):
print(i)
"""
# 要注意小心sql注入 delete from 数据 where id = 1 exec或者eval两个函数慎用
exec(res)
执行
[root@node10 python]# python3 test.py
0
1
2
3
4
5
6
7
8
9
16 epr 不转义字符输出字符串
listvar = [1,2,3]
res = repr(listvar)
print(res,type(res))
strvar = "123"
print(repr(strvar))
strvar = "111\n\t\r"
print(repr(strvar))
执行
[root@node10 python]# python3 test.py
[1, 2, 3] <class 'str'>
'123'
'111\n\t\r'
17 hash生成哈希值
字典的键 和 集合当中的值 需要使用哈希算法
哈希算法在存储上是无序的散列,经过计算之后会产生具有固定长度的唯一值
通过字符串和内存地址加在一起计算出来的 通过hash计算出来
res1 = "今天天气好"
res2 = "今天天气好"
res3 = "132"
print(hash(res1))
print(hash(res2))
print(hash(res3))
执行
[root@node10 python]# python3 test.py
-8085451974024421996
-8085451974024421996
-5108487044744349521
可哈希数据:Number(int bool complex float) str () [不可变数据]
不可哈希数据:dict list set [可变数据]
021.Python的内置函数的更多相关文章
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- 如何查看Python的内置函数
经常调用的时候不知道python当前版本的内置函数是哪些,可以用下面的指令查看: C:\Users\Administrator>python Python 2.7.11 (v2.7.11:6d1 ...
- python_way,day3 集合、函数、三元运算、lambda、python的内置函数、字符转换、文件处理
python_way,day3 一.集合 二.函数 三.三元运算 四.lambda 五.python的内置函数 六.字符转换 七.文件处理 一.集合: 1.集合的特性: 特性:无序,不重复的序列 如果 ...
- python基础——内置函数
python基础--内置函数 一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highl ...
- Python的内置函数
python的内置函数一共有68个,下面将简单介绍各个函数的功能. abs() dict() help() min() setattr() all() dir() hex() next() slice ...
- python print()内置函数
啦啦啦啦啦啦,我又来了,学习任何东西都得坚持,我一定的好好加油!!! 今天来说说print()函数,前边我们已经用过好好多次啦,现在来学习哈吧!!! Python的内置函数,print() print ...
- Python入门-内置函数一
什么是内置函数?就是python给你提供的拿来直接用的函数,比如print,input等等,截止到python版本3.6.2 python一共提供了68个内置函数,他们就是python直接提供给我们的 ...
- Python 集合内置函数大全(非常全!)
Python集合内置函数操作大全 集合(s).方法名 等价符号 方法说明 s.issubset(t) s <= t 子集测试(允许不严格意义上的子集):s 中所有的元素都是 t 的成员 s ...
- Python字典内置函数和方法
Python字典内置函数和方法: 注:使用了 items.values.keys 返回的是可迭代对象,可以使用 list 转化为列表. len(字典名): 返回键的个数,即字典的长度 # len(字典 ...
随机推荐
- PYTHON工业互联网应用实战12—客户端操作
本章节我们将实现与admin里类似的列操作"下达"功能,演示客户端是如何实现操作功能,同时,演示也会强调一点,何时合并你的功能代码,避免相同功能使用不同的代码段来实现,在企业开发中 ...
- Apache Hudi C位!云计算一哥AWS EMR 2020年度回顾
1. 概述 成千上万的客户在Amazon EMR上使用Apache Spark,Apache Hive,Apache HBase,Apache Flink,Apache Hudi和Presto运行大规 ...
- 1.PreparedStatement VS Statement
两者都是Sun公司定义的接口,PreparedStatement属于Statement的子接口.二者类似信使,向数据库中执行sql语句: Statement存在拼串的操作,比较繁琐:存在SQL注入问题 ...
- 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像制作fedora27-18
自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像制作fedora27-18 欢迎加QQ群:1026880196 进行交流学习 制作OpenSt ...
- mariadb_1 数据库介绍及基本操作
数据库介绍 1.什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来管理 ...
- 我自横刀向天笑,手写Spring IOC容器,快来Look Look!
目录 IOC分析 IOC是什么 IOC能够带来什么好处 IOC容器是做什么工作的 IOC容器是否是工厂模式的实例 IOC设计实现 设计IOC需要什么 定义接口 一:Bean工厂接口 二:Bean定义的 ...
- 《深入理解计算机系统》学习笔记整理(CSAPP 学习笔记)
简介 本笔记目前已包含 CSAPP 中除第四章(处理器部分)外的其他各章节,但部分章节的笔记尚未整理完全.未整理完成的部分包括:ch3.ch11.ch12 的后面几小节:ch5 的大部分. 我在整理笔 ...
- 007-Java中方法的使用(基础篇)
目录 一.方法的基本概述 一.方法的概述 二.方法的语法机制 三.方法的调用 一.方法的基本概述 一.方法的概述 方法其实就是一段可以完成某个特定功能的并且可以被重复利用的代码片段,方法的出现,让 ...
- 拿到蚂蚁金服的offer是一种什么体验?3年Java程序员分享面经
前言:我是一名三年的Java程序员,之前一直是在外包公司工作的.在这个月五号的时候,通过我的学长做内推,有了去蚂蚁金服面试的机会.我是在12号接到的电话面试的,因为蚂蚁金服需要7天的简历评估的.还有就 ...
- pyqt5 菜单栏+信息提示框
前言 使用pyqt5 添加菜单栏 单击菜单栏 弹出信息框(MessageBox用法) 菜单栏 功能 Action是Qt中单独引入的一个对象,对应QAction类.Action表示一个独立的操作,是将界 ...