Python3-笔记-C-001-函数-内置函数

import os

def fun():
a1 = all([True, False]) # 与的关系,返回bool
a2 = any([True, False]) # 或的关系,返回bool

num = abs(-1.23) # 绝对值
num = pow(5, 3) # 幂次方 x**y =>125
num = pow(5, 3, 3) # 幂次方再取余 (x**y) % z => 2
num = round(1.23) # 返回浮点数近似值,默认保留0,且四舍五入, 1
num = round(1.23, 5) # 5为保留小数点后位数, 1.23
num = sum([1, 2, 3, 4, 5]) # 对列表数字求和, 15
strs = ascii('中文') # 返回对象的ascii字符串形式 , '\\'\\u4e2d\\u6587\\''
strs = bin(10) # 整数转为二进制字符串(若被转对象非int类型可在__index__里定义), '0b1010'
boolean = bool(0) # 获取真假(:None / False / 任何类型的0 / ""()[]{} / __bool__返回False / __len__返回0), False
bytesss = bytearray("You were, are and will be in my heart!", "utf-8")
# 字符串(字符串,编码):按编码转为字节序列 / 数字(数字):生成相应大小的空数组 / 无参():0数组
# bytearray(b'You were, are and will be in my heart!')

strs = chr(123) # 整数(Unicode)转为char类型(范围[0, 1 114 111]) , '{'
num = ord("{") # char类型转为整数(Unicode), 123
num = complex("1+2j") # 将字符串或数字转为复数(不能含有空格), (1+2j)
num = float('1.1') # 构建浮点数, 1.1
strs = format(123,'f') # 格式化(类型:x,b,f... / 位移:>(>10),<,^ / 长度:5 / E), '123.000000'
strs = hex(123) # 将整数转为16进制字符串, '0x7b'
strs = oct(123) # 十进制转成8进制字符串, '0o173'
num = int(123.1) # 转为整数, 123
strs = str(123) # 将对象转为字符串, 123
strs = str(b"123", "utf-8"), '123'
elem = max([1,2,3,4], [2,4,6]) # 可接收1iterable,或多个元素, [2, 4, 6]
elem = max(1,2,3,4) # => 4
elem = max([1,2,3,4, 6], key = lambda x : x == 2) # => 2
elem = min(1,2,3,4) # max相反

exe = compile("print('O_O')", filename = 'strs', mode='exec') # 编译为代码, mode(编译模式:可执行语句'exec',单个语句'eval',交互式语句'single')
exec(exe) # 执行已编译代码
exec("print('O_O')") # 执行String未编译代码
eval(compile("print('O_O')", filename = 'strs', mode='eval')) # 不接受字符串类型

strs = input("请输入数据:") # 输入数据
print("字符串%d" % 123) # 打印字符 => 字符串123
print("", "", "", sep="-") # sep为分隔 => --
print("", "", "", sep="-", end="\r\n") # end为尾部 => --/r/n

dics = dict() # 创建字典, {}
tups = divmod(10, 5) # 取商和余数,返回元组,((10/5),余数(10%5)), <class 'tuple'>: (2, 0)
num = len([1, 2, 3]) # 长度 3
lists = list((1, 'a')) # 转为list类型, <class 'list'>: [1, 'a']
tups = tuple([1, 2, 3]) # 转为tuple元组类型, <class 'tuple'>: (1, 2, 3)
ran = range(5) # 不可变序列
ran = range(0, 5)
ran = range(0, 5, 2) # (起始,结束,增加量)
sets = set([1, 2, 3]) # 返回set集合, {1, 2, 3}
lists = sorted([2, 5, 3, 1, 4]) # 排序 => [1, 2, 3, 4, 5]
lists = [1, 2, 3, 4, 5][slice(3)] # 没有none的形式
lists = [1, 2, 3, 4, 5][slice(None, 3, None)] # 切片 => [1, 2, 3]
lists = [1, 2, 3, 4, 5][slice(1, 3)] # <class 'list'>: [2, 3]
lists = [1, 2, 3, 4, 5][slice(1, 3, None)] # <class 'list'>: [2, 3]
posted @ 2017-10-25 17:19  魏桐  阅读(130)  评论(0编辑  收藏  举报