1.数学函数
import math
print('-1的绝对值是{}'.format(abs(-1)))
print('divmod函数返回商和余数的元祖{}'.format(divmod(9, 3))) # ****
print('sum函数求和{}'.format(sum([1, 2, 3])))
print('round四舍五入函数{}'.format(round(math.pi, 9)))
print('round四舍五入函数{}'.format(round(math.pi, 9)))
print('min计算最小值函数{}'.format(min(3, 2, 1, -2)))
print('max计算最大值函数{}'.format(max(3, 2, 1, -2)))
2.对象相关函数
创建字典
dict(**kwargs)
dict(mapping, **kwargs) 比如元祖列表
dict(iterable, **kwargs)
seriesList = [('pythonSeries', '零基础学python'), ('javaSeries', '零基础学java'), ('goSeries', '零基础学go')]
seriesDict = dict(seriesList)
print(seriesDict)
print('range创建一个列表{}'.format(range(10)))
print('set创建不重复且无序的元素集合{}'.format(set(range(10))))
3.操作迭代器相关函数
1. all 重要 所有都true则true
2. any 重要 所有都False则False
print('all判断序列元素是否为True: 结果是{}'.format(all(range(10)))) # false
print('all判断序列元素是否为True: 结果是{}'.format(all(range(1, 10)))) # true
print('any判断序列元素是否为False: 结果是{}'.format(any([0, 0, 0]))) # false
print('any判断序列元素是否为False: 结果是{}'.format(any([0, 1, 0]))) # true
# create
class add:
def __init__(self, _start, _end):
self.start = _start
self.end = _end
def get_next(self):
s = self.start
if (self.start < self.end):
self.start += 1
else:
raise StopIteration
print('start: {}'.format(self.start))
return s
a = add(1, 5)
iterator = iter(a.get_next, 3) # 第二个参数sentinel(翻译邵兵,执行__next__方法次数)有值时,第一个参数需为可调用对象
for i in iterator:
print('i:{}'.format(i))
# print('获取迭代器的下一个元素{}'.format(next()))
iteror = iter([1, 2, 3, 4, 5])
while 1:
try:
x = next(iteror)
print(x, end=' ')
except StopIteration:
print("\n迭代器运行结束!")
break
# print('对可迭代对象进行排序{}'.format(sorted(3)))
students = [('john', 'A', 15), ('Tom', 'B', 12), ('Sam', 'B', 13)]
# 按第三项对students排序 sorted key 关键字
new_sorted_students = sorted(students, key=lambda x: x[2], reverse=False) # 不降序
print(new_sorted_students)
# print('将可遍历的数据对象组合成带有数据和数据下标的索引序列{}'.format(enumerate()))
lista = ['puu', 'xyy', 'klo', 'ujl', 'sbn']
listb = ['foot', 'month', 'hands', 'nose', 'arms']
en1 = enumerate(lista)
en2 = enumerate(listb, start=len(lista))
listc = list(en1) + list(en2)
print(listc)
# print('通过指定条件过滤序列并返回新的列表对象{}'.format(filter()))
def find(item):
age = int(item[1])
if age >= 8 and age < 10:
return True
return False
list1 = [('jim', 20), ('jim2', 9), ('jim2', 21)]
res = filter(find, list1)
print(list(res))
# print('将可迭代对象打包成元祖并返回元祖组成的对象{}'.format(zip()))
# 打包
a = [1, 2, 3]
b = [1, 2, 3]
c = [1, 2, 3, 4]
print(list(zip(a, b, c))) # [(1, 1, 1), (2, 2, 2), (3, 3, 3)]
# print('通过自定义函数实现对序列元素映射操作,并返回操作后的结果{}'.format(map()))
def _add(x):
return x * 2
print(list(map(_add, [1, 2, 3])))
# print('对列表元素进行反向排序{}'.format(reversed()))
print(list(reversed(c)))
4.数据转换函数整理
1.hex
2.oct
3.bin
4.ord 重要
5.chr 重要
print('hex十进制转十六进制函数{}'.format(hex(9)))
print('oct十进制转八进制函数{}'.format(oct(9)))
print('bin十进制转二进制函数{}'.format(bin(9)))
print('ord获取ASCII或者Unicode值函数{}'.format(ord('a')))
#
print('int字符串转整型函数{}'.format(int('3')))
print('str对象转字符串函数{}'.format(str(3)))
print('float浮点数值转换函数{}'.format(float(3)))
print('bool将参数转换为bool类型函数{}'.format(bool('')))
print('bool将参数转换为bool类型函数{}'.format(bool(0)))
print('bool将参数转换为bool类型函数{}'.format(bool(None)))
#
print("="*100)
print('tuple列表转元祖函数{}'.format(tuple([1,2,4])))
print('chr获取整数对应字符{}'.format(chr(97)))
print('chr+ord获取整数对应字符{}'.format(chr(ord('a'))))
print('list元祖转列表函数{}'.format(list((1,2,3)))
5.object属性操作
# '设置对象属性值{}' setattr(object , name , value)
# setattr()函数 可以给对象属性赋新值,
class MyObj:
name = 'default'
def __init__(self, initValue=''):
print(self.name)
self.name = initValue
print(self.name)
obj = MyObj('tom')
setattr(MyObj, 'name', 'zhangsan')
print(obj.name)
print(MyObj.name)
obj1 = MyObj()
# print('在新式类中定义获取、设置、删除及描述操作的属性实现函数{}'.format(property()))
# 第一种写法
class MyClass(object):
def __init__(self):
self._name = 'default name'
def _getx(self):
return self._name
def _setx(self, value):
self._name = value
def _delx(self):
del self._name
property(_getx, _setx, _delx, "hello")
myclass = MyClass()
print("first get name", myclass._name)
myclass._name = 'new name'
print('after set name:', myclass._name)
del myclass._name
try:
print('after del name:', myclass._name)
except AttributeError:
print("'MyClass' object has no attribute '_name'")
# 第二种写法 装饰器
class Screen(object):
def __init__(self):
self._width = None
self._height = None
self._area = None
@property
def area(self):
return self._width * self._height
@property
def width(self):
return self._width
@width.setter
def width(self, new_width):
self._width = new_width
@property
def height(self):
return self._height
@height.setter
def height(self, new_height):
self._height = new_height
s = Screen()
s.width = 1024
s.height = 768
print('area is {}'.format(s.area))
if s.area == 1024 * 768:
print('测试通过')
else:
print('fail')
# print('以字典形式返回对象的属性和属性值{}'.format(var(3)))
class myVarClass(object):
name = 'lilei'
age = 12
sex = '男'
id = 2
student = vars(myVarClass)
print(student)
import enum
class TestEn(enum.Enum):
name = 'lilei'
age = 12
sex = '男'
id = 2
student1 = TestEn
print(student1)
print(student1.name.name)
print(student1.name.value)
# print('返回对象的属性值{}'.format(getattr()))
class myNewClass(object):
name = 'lilei'
age = 12
sex = '男'
id = 2
6.基本常用函数
# print('构造切片对象函数{}'.format(slice()))
# 通过指定的位置和切片构造一个新对象
A = [1,2,3,4,5,6,7,8,9]
B = (1,2,3,4,5,6,7,8,9)
C = '12345679'
print(slice(3))
new_a = A[slice(3)]
new_b = B[slice(3)]
new_c = C[slice(3)]
new_d = C[slice(0,2,1)] # start 开始截取,end结束截取,不包括end位的值
print(new_a)
print(new_b)
print(new_c)
print(new_d)
标签:函数,format,python,self,print,._,整理,name
From: https://www.cnblogs.com/hikk/p/17110088.html