14 built-in function (a)

 

13. The front high-energy - built a function

 

 A main contents of this section:

 

1. Built-in functions

 

. What brought directly a function of what is built-in function? Python is provided to you, such as print., Input, etc. As of version 3.6.2 python python provided a total of 68 built-in functions. They are directly python to us. we've already encountered some of the. Some have not used. there are some over the need to learn object-oriented in order to continue learning. today we know about python's built-in functions.

 

abs()

 

dict()

 

help()

(I)

setattr()

 

 

 

 

 

 

 

all()

 

to you()

 

hex()

next()

slice()

 

 

 

 

 

 

 

any()

 

divmod()

 

id()

object()

sorted()

 

 

 

 

 

 

 

ascii()

 

enumerate()

 

input()

oct()

staticmethod(

 

 

 

 

 

 

 

bin()

 

eval()

 

int()

open()

str()

 

 

 

 

 

 

 

bool()

 

exec()

 

isinstance()

words()

sum()

 

 

 

 

 

 

bytearray()

filter()

 

issubclass()

pow()

super()

 

 

 

 

 

 

 

bytes()

 

float()

 

process ()

print()

tuple()

 

 

 

 

 

 

 

callable()

 

format()

 

Only ()

property()

type()

 

 

 

 

 

 

 

chr()

 

frozenset()

 

list()

range()

whose()

 

 

 

 

 

 

classmethod()

getattr()

 

locals()

repr()

zip()

 

 

 

 

 

 

 

compile()

 

globals()

 

map()

reversed()

__import__()

 

 

 

 

 

 

 

complex()

 

Hsattr ()

 

max()

round()

 

 

 

 

 

 

 

 

delattr()

 

hash()

 

memoryview()

set()

 

 

 

 

 

 

 

 

Scope Related:

 

 

 

 

 

 

locals()

Returns the name of the current scope

 

 

 

 

 

globals () Returns the name of the global scope

 

Related iterator: range () generates data

next () iterator performs down a row, the practical use inside the __next __ () method returns the next item iterator

 

ITER () Gets the iterator, inside the actual use of the __iter __ () method to obtain the iterator

 

String String type code to perform row of

 

eval () performs line string string types. returns the final result, and

 

print(eval("2+2"))                   # 4

 

n = 8

 

print(eval("2+n"))                   # 10

 

def   func(): print(666)

 

eval("func()")                # 666

 

exec () performs line string code string type

 

exec("""

 

for    i in range(10): print(i)

 

""")

 

exec("""

 

def func():

 

print ( "I'm Jay") func ()

 

""")

 

compile () code string string type mutation. line code object can be performed or eval () request performed by the exec statement sentence row

 

value

 

'''

 

Parameters :

 

  1. 1.     Resource code to execute the line , dynamic code fragment

 

  1. 2. The     file name , the code stored in the file name , when introduced to the first argument of the time , this parameter will be able to empty the 

 

  1. 3.     mode , the value there . 3 th ,

 

  1. 1.    Exec: general put some sentences flow statement when 

 

  1. 2.    eval: Resource stored only a evaluated expression .

 

  1. 3.    single: Resource code is stored when there is interaction . Mode should be single

 

'''

 

code1 = "for i in range(10): print(i)" c1 = compile(code1, "", mode="exec") exec(c1)

 

code2 = "1+2+3"

 

c2 = compile(code2, "", mode="eval") a = eval(c2)

 

 

print(a)

 

code3 = "name = input ( 'Please enter your name:')" c3 = compile (code3, "", mode = "single") exec (c3)

 

print(name)

 

 

A string returns a value in the form of a string of code with the eval (). The return value is not a string of code in the form of a string exec (). Rarely used compile ()

 

Input and output correlation: input () Gets contents print user input () printout

 

 

Memory-related:

 

hash () of the object acquired hash value (int, str, bool, tuple) id () to obtain the object memory address

 

 File-related operations: open () to open the mouth for a file, create a file handle sentence

 

Related modules:

 

the __import __ () function for dynamically loading classes and

 

help:

 

help () function is used to view the function module or call the relevant purposes detailed description:

 

callable () is used to check whether an object can be called, if it returns True, object likely call fails, but if it returns False calls will never succeed to view the built-in properties..:

 

. Dir () to view the object's built-in properties, methods of access are objects __dir __ () method related to the underlying data type:

 

Digital correlation: bool () given data into a bool value if the value does not return to False..

 

int () given data into the int value. If the value is not given, returns 0 float () given data into float value. decimal i.e.

 

Complex () creates a complex. The first parameter is the real part, the imaginary part of the second parameter. or the first argument string string directly complex described

 

Base conversion: bin () will be converted into binary parameters

Conversion otc () will give arguments to octal hex () parameter will be converted into hexadecimal

 

 

Math: abs () returns the absolute value

divmode () returns the quotient and the remainder round () rounding

 

POW (a, b) of a b-th power demand, if there are three parameters. After the required time after power modulo sum of the third number () sum min () for the minimum max () selecting the maximum value

 

 

And data structure: column lists and tuples:

 

Conversion list () to a list in a column iterable tuple () will be converted into a iterable tuple

 

the reversed () to a column inversion sequence, the sequence returns column inversion iterator slice () column list sections

 

st = "Hello everyone, I am a twist vine" s = slice (1, 5, 2) print (st [s])

 

 

String string Related:

 

 

 

 

str()

The data string is converted into a string

 

 

format()

Associated with the specific data used to calculate the various decimal, actuarial

# String string

 

 

 

 

print(format('test', '<20'))

# Left

print(format('test', '>20'))

# Align Right

print(format('test', '^20'))

# Center

# Values

 

 

 

 

print(format(3, 'b'))

# Binary

 

 

print(format(97, 'c'))

# Converted to unicode characters

print(format(11, 'd'))

(Decimal)

 

 

print (format (11, 'o'))

# Octal

 

 

print(format(11, 'x'))

# Hexadecimal (lowercase)

print(format(11, 'X'))

# Hexadecimal (capital letters)

print(format(11, 'n'))

And d Like #

 

print (format (11)) # and d as

 

 

# Float

 

 

 

 

print (format (123456789, 'e'))

# Scientific notation. The default retention remain six decimal places

print(format(123456789, '0.2e'))

 

# Scientific notation reserved to stay two decimal places (lowercase)

print(format(123456789, '0.2E'))

 

# Scientific notation reserved to stay two decimal places (uppercase)

print(format(1.23456789, 'f'))

# Decimal notation. 6 decimal places remain reserved

print (format (1.23456789, '0.2f))

# Decimal notation. Reserved stay 2 decimals

print(format(1.23456789, '0.10f'))

# Decimal notation. 10 decimal remain reserved

print(format(1.23456789e+10000, 'F'))                              #  小数点计数法.

 

 

bytes() 把字符串串转化成bytes类型

 

s = "你好" bs = s.encode("UTF-8") print(bs)

 

s1 = bs.decode("UTF-8") print(s1)

 

bs = bytes(s, encoding="utf-8")                                 # 把字符串串编码成UTF-8

 

print(bs)

 

bytearray()      返回 一个新字节数组. 这个数字 里里的元素是可变的, 并且每个元素的值得范

 

围是[0,256)

 

ret = bytearray('alex',encoding='utf-8') print(ret[0])

 

print(ret)

 

memoryview() 查看bytes在内存中的情况

 

# 查看bytes字节在内存中的情况 s = memoryview("麻花藤".encode("utf-8")) print(s)

 

ord() 输入字符找带字符编码的位置 chr() 输入位置数字找出对应的字符

ascii() 是ascii码中的返回该值 不是就返回\u...

 

# 找到对应字符的编码位置 print(ord('a')) print(ord('中'))

 

 

#   找到对应编码位置的字符 print(chr(97)) print(chr(20013))

 

#   在ascii中就返回这个值. 如果不不在就返回\u...

 

print(ascii('a'))

 

print(ascii('好'))

 

repr()     返回 一个对象的string形式

 

#   repr 就是原封不不动的输出, 引号和转义字符都不不起作 用print(repr(' 大家好,\n \t我叫周杰伦')) print(' 大家好我叫周杰伦')

 

#   %r  原封不不动的写出来

 

name = 'taibai' print('我叫%r' % name)

 

数据集合: dict() 创建 一个字典

 

 

set()      创建 一个集合

 

frozenset() 创建 一个冻结的集合. 冻结的集合不能进 行行添加和删除操作其他相关:

 

len() 返回 一个对象中的元素的个数 sorted() 对可迭代对象进 行行排序操作(讲完lamda后再讲这个) enumerate() 获取集合的枚举对象

 

lst = ["alex", "wusir", "taibai"] for index, el in enumerate(lst): print(str(index)+"==>"+el)

 

all() 可迭代对象中全部是True, 结果才是True any() 可迭代对象中有 一个是True, 结果就是True

 

print(all([1,2,True,0])) print(any([1,'',0]))

 

zip() 函数 用于将可迭代的对象作为参数, 将对象中对应的元素打包成 一个个元组, 然后返回由这些元组组成的开了了表. 如果各个迭代器的元素个数不 一致, 则返回列列表 长度与最短的对象相同.

 

l1 = [1,2,3,]

 

l2 = ['a','b','c',5]

 

l3 = ('*','**',(1,2,3)) for i in zip(l1,l2,l3):

 

print(i)

 

filter() 过滤(讲完lamda) map() 会根据提供的函数对指定序列列做映射(lamda)

 

 

# def func():
# a = 10
# print(locals()) # 当前作用域中的内容
# print(globals()) # 全局作用域中的内容
# print("今天内容很简单")
# func()


# for i in range(20,15,-3):
# print(i)

# lst = ["大阳哥", "喜欢", "私密的徒步"]
# it = iter(lst) # __iter__()
# print(it.__next__())
# print(next(it)) # __next__()
# print(next(it))
# print(next(it))

# print("李嘉诚", "黄花菜", "马云", sep="*", end="大阳哥") # seperator
# input("提示语")

# hash算法:
# 目的是唯一性
# dict 查找效率非常高, hash表.用空间换的时间 比较耗费内存

# s = "大阳哥"
# print(hash(s))
# a = 12
# print(hash(a))

# lst = [1,2,3,4,5,6,7]
# # print(hash(lst)) # 列表是不可哈希的
# print(hash((1,)))
# print(hash("呵呵"))
# print(hash("哈哈"))

# 让用户输入一个要导入的模块
# import os
# name = input("请输入你要导入的模块:")
# __import__(name) # 可以动态导入模块

# print(help(str))

# print(dir(str))

# a = 11.25
# print(type(a))

# bin() 把一个十进制的数, 转换成二进制
# print(bin(10)) # 二进制
# print(hex(10)) # 十六进制
# print(oct(10)) # 八进制


# a = 10
# print(callable(a))
#
# def func():
# print("马化腾")
# print(callable(func)) # 函数是可以被调用的

# s = input("请输入a+b:")
# print(eval(s)) # 可以动态的执行代码. 代码必须有返回值

# s = "25*4"
# a = eval(s) # 运算
# print(a)

# s = "for i in range(10): print(i)"
# a = exec(s) # exec 执行代码不返回任何内容
# print(a)

# 动态执行代码
# exec("""
# def func():
# print(" 我是周杰伦")
# """ )
# func()

# code1 = "for i in range(10): print(i)"
# com = compile(code1, "", mode="exec") # compile并不会执行你的代码.只是编译
# exec(com) # 执行编译的结果
#
# code2 = "5+6+7"
# com2 = compile(code2, "", mode="eval")
# print(eval(com2))
#
# code3 = "name = input('请输入大阳哥的名字:')"
# com3 = compile(code3, "", mode="single")
# exec(com3)
# print(name)


# print(abs(-2)) # 绝对值
# print(abs(2))

# print(divmod(20,3)) # 求商和余数

# print(round(4.50)) # 五舍六入 => 四舍五入

# print(pow(10,2,3)) # 如果给了第三个参数. 表示最后取余

# print(sum([1,2,3,4,5,6,7,8,9,10])) # 求和

# lst = "你好啊"
# it = reversed(lst) # 不会改变原列表. 返回一个迭代器, 设计上的一个规则
# print(list(it))

# lst = [1, 2, 3, 4, 5, 6, 7]
# print(lst[1:3:1])
#
# s = slice(1, 3, 1) # 切片用的
# print(lst[s])


# name = "你好. \n我叫%s周润发" % "李嘉诚"
# print(name)
# print(repr(name)) # 原样输出,过滤掉转义字符 \n \t \r 不管百分号

# print(ord('a')) # 97, 返回字母a在编码表中的码位
# print(ord('中')) # 20013 中国的中字在编码表中的位置

# print(chr(65)) # 已知码位. 计算字符
# print(chr(20018))
#
# for i in range(65536):
# print(chr(i), end=" ")

# print(ascii("房"))

# s = "李嘉诚的爸爸"
# a = s.encode("UTF-8")
# print(a)
# print(a.decode("GBK"))


# bs = bytes("大阳哥今天很厉害", encoding="utf-8")
# print(bs.decode("utf-8"))

# ret = bytearray("alex" ,encoding ='utf-8')
# print(ret[0])
# ret[0] = 65
# print(str(ret))

# s = memoryview("麻花藤".encode( "utf-8")) # 查看内存
# print(s)


# s = "我叫王尼玛"
# print(format(s, "^20"))
# print(format(s, "<20"))
# print(format(s, ">20"))

# print(format(3, 'b' )) # ⼆进制
# print(format(97, 'c' )) # 转换成unicode字符
# print(format(11, 'd' )) # ⼗进制 %d
# print(format(11, 'o' )) # ⼋进制 8
# print(format(11, 'x' )) # ⼗六进制(⼩写字⺟)
# print(format(11, 'X' )) # ⼗六进制(⼤写字⺟)
# print(format(11, 'n' )) # 和d⼀样
# print(format(11)) # 和d⼀样

# print(format(123456789, 'e' )) # 科学计数法. 默认保留6位小数
# print(format(123456789, '0.2e' )) # 科学计数法. 保留2位小数(小写)
# print(format(123456789, '0.2E' )) # 科学计数法. 保留2位小数(大写)
# print(format(1.23456789, 'f' )) # 小数点计数法. 保留6位小数
# print(format(1.23456789, '0.2f' )) # 小数点计数法. 保留2位小数
# print(format(1.23456789, '0.10f')) # 小数点计数法. 保留10位小数
# print(format(1.23456789e+3, 'F')) # 小数点计数法. 很大的时候输出 INF

# lst = ["蛋1", "蛋2", "蛋3", "蛋4"]
# for i in range(len(lst)):
# print(i)
# print(lst[i])

# for index, el in enumerate(lst, 100): # 把索引和元素一起获取,索引默认从0开始. 可以更改
# print(index)
# print(el)


# print(any([0, "哈哈", "馒头", True]))


lst1 = ["施瓦辛格", "泰达米尔", "阿米尔汗", "威震天"]
lst2 = ["终结者", "英雄联盟", "我的个神啊", "变形金刚"]
lst3 = [10000000, 3, 10, 55,66]
for el in zip(lst1, lst2, lst3):
print(el)

参考资料料:

 

https://www.processon.com/mindmap/5de20a11e4b074c442eefe1d

Guess you like

Origin www.cnblogs.com/Selbst/p/11968379.html