1 Star 0 Fork 0

xinban/Python的内置函数

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
f开头的库.py 4.28 KB
一键复制 编辑 原始数据 按行查看 历史
xinban 提交于 2021-09-16 09:15 +08:00 . test1
'''
fabs() 方法返回数字的绝对值,如math.fabs(-10) 返回10.0。
fabs() 函数类似于 abs() 函数,但是他有两点区别:
abs() 是内置函数。 fabs() 函数在 math 模块中定义。
fabs() 函数只对浮点型跟整型数值有效。 abs() 还可以运用在复数中
import math
math.fabs( x )
注意:fabs()是不能直接访问的,需要导入 math 模块,通过静态对象调用该方法。
x -- 数值表达式。
返回数字的绝对值。
'''
import math # 导入 math 模块
print ("math.fabs(-11.17) : ", math.fabs(-11.17))
print ("math.fabs(10.12) : ", math.fabs(10.12))
print ("math.fabs(10.72) : ", math.fabs(10.72))
print ("math.fabs(math.pi) : ", math.fabs(math.pi)) # 3.141592653589793
print('-'*10,"分割线","-"*10)
'''
filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,
如果要转换为列表,可以使用 list() 来转换。
该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判别,
然后返回 True 或 False,最后将返回 True 的元素放到新列表中。
filter(function, iterable)
function -- 判断函数。
iterable -- 可迭代对象。
返回一个迭代器对象
'''
def is_odd(n):
return n % 2 == 1
tmplist = filter(is_odd, [1,2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13])
print('打印看看这个迭代器对象',tmplist) # 打印看看这个迭代器对象 <filter object at 0x00000232B9DB8EB8>
newlist = list(tmplist)
print(newlist) # [1, 3, 5, 7, 9, 11, 13]
number_list = range(-5, 5)
less_than_zero = filter(lambda x: x < 0, number_list)
print(list(less_than_zero)) # [-5, -4, -3, -2, -1]
print('-'*10,"分割线","-"*10)
'''
float() 函数用于将整数和字符串转换成浮点数。
class float([x])
x -- 整数或字符串
返回浮点数。
'''
# 字符串
print(float('111') ) # 111.0
print(float(11)) # 11.0
print('-'*10,"分割线","-"*10)
'''
Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。
基本语法是通过 {} 和 : 来代替以前的 % 。冒号(:)用来格式化字符串;大括号({})用来匹配字符串
format 函数可以接受不限个参数,位置可以不按顺序。
'''
# 不设置指定位置,按默认顺序
print("{} {}".format("hello", "lidihuo"))
# 设置指定位置
print("{0} {1}".format("hello", "lidihuo"))
# 设置指定位置
print("{1} {0} {1}".format("hello", "lidihuo"))
#设置参数
# -*- coding: UTF-8 -*-
print("网站名:{name}, 地址 {url}".format(name="立地货", url="www.lidihuo.com"))
# 通过字典设置参数
site = {"name": "立地货", "url": "www.lidihuo.com"}
print("网站名:{name}, 地址 {url}".format(**site))
# 通过列表索引设置参数
my_list = ['立地货', 'www.lidihuo.com']
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list))
# 向str.format()传入对象
class AssignValue(object):
def __init__(self, value):
self.value = value
my_value = AssignValue(10)
print('value 为: {0.value}'.format(my_value)) # "0" 是可选的
print('-'*10,"分割线","-"*10)
# 数字格式化
print("{:.2f}".format(3.1415926))
print('-'*10,"分割线","-"*10)
'''
floor(x) 返回数字的下舍整数,小于或等于 x。
import math
math.floor( x )
注意:floor()是不能直接访问的,需要导入 math 模块,通过静态对象调用该方法。
x -- 数值表达式。
返回小于或等于 x 的整数。
'''
import math # 导入 math 模块
print("math.floor(-11.17) : ", math.floor(-11.17)) # -12
print("math.floor(10.12) : ", math.floor(10.12)) # 10
print("math.floor(10.72) : ", math.floor(10.72)) # 10
print("math.floor(math.pi) : ", math.floor(math.pi)) # 3
print('-'*10,"分割线","-"*10)
'''
frozenset() 返回一个冻结的集合,冻结后集合不能再添加或删除任何元素。
class frozenset([iterable])
iterable -- 可迭代的对象,比如列表、字典、元组等等。
返回新的 frozenset 对象,如果不提供任何参数,默认会生成空集合。
'''
a = frozenset(range(10)) # 生成一个新的不可变集合
print(a) # frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
# a.add(1) # AttributeError: 'frozenset' object has no attribute 'add'
b = frozenset('lidihuo')
print(b) # frozenset({'l', 'o', 'i', 'd', 'u', 'h'})
print('-'*10,"分割线","-"*10)
'''
'''
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qitu1024/built-in-functions-of-python.git
git@gitee.com:qitu1024/built-in-functions-of-python.git
qitu1024
built-in-functions-of-python
Python的内置函数
master

搜索帮助

371d5123 14472233 46e8bd33 14472233