python basis of built-in functions Analysis on the Second

Book connected to the text:
Here Insert Picture DescriptionJulia Roberts Town House! ! ! !

divmod (a, b)
it two (non-complex) number as an argument and returns one pair quotient and the remainder in performing integer division. Mixed operand types,
applicable rules of arithmetic operators eyes. For the same integer, and the result (a // b, a% b ). For floating-point number, the result is
(q, a% b), q is typically math.floor (a / b) but may be smaller than 1. In any case,% Q * b + a
b and a are substantially equal; if a% b is nonzero, and its sign as b, and 0 <= abs (a% b ) <abs (b).

divmod(10,2)==>(5, 0)#返回为元组整除和余数
divmod(11.1,2)==>(5.0, 1.0999999999999996)
#浮点运算就不怎么靠谱了,建议还是用math模块吧

enumerate (iterable, start = 0)
Returns an enumerator object. iterable must be a sequence, or iterator, or other objects to support iteration. the enumerate ()
Returns the __next __ iterator () method returns a tuple, which contains a counted value (from the start, the default is 0)
and the value obtained by iteration iterable.

li=[
	'天下风云出我辈',
	'一入江湖岁月催',
	'皇图霸业谈笑中',
	'不胜人生一场醉',
	'提剑跨骑挥鬼雨',
	'白骨如山鸟惊飞']
for i in enumerate(li,1):
    print(i)
 ==>
(1, '天下风云出我辈')
(2, '一入江湖岁月催')
(3, '皇图霸业谈笑中')
(4, '不胜人生一场醉')
(5, '提剑跨骑挥鬼雨')
(6, '白骨如山鸟惊飞')

Let me back up a few words:
dust matter waves like water, only a few people back to rivers and lakes sigh.
Note divided ########## #########################
ID (Object)
"identification value" returned object. This value is an integer, to ensure that the life cycle of this object is unique and constant. Life of the two overlapping objects may not have the same id () value.
Like you and me our id is different! ! !

a='小楼一夜听春雨'
b=['梦里花落知多少']
id(a)==>2841055982232
id(b)==>2841064088904
在你自己的电脑上打出也是不同的,因为我们不一样,不一样。。

INPUT ([prompt])
if prompt argument exists, it is written to standard output, do not end with a newline. Next, the function reads the input from a
line, converting it to a string (except the newline) and returns.

这个函数不过多解释大家都懂的对吧!!

map (function, iterable, ...)
returns a function will be applied to each and outputs the result of the iterator iterable. If the incoming iterable additional parameters, function arguments must receive the same number of items in parallel and applied to all iterations acquired from the object. When there are multiple iterables shortest iterable depleted the entire iteration will end.

!!! Note that returns an iterator, iterator want to get results either cycle or expand with a list, or the tuple and other functions.

a=[1,2,3,4,5,6]
b=['银烛秋光冷画屏','轻罗小扇扑流萤','天阶夜色凉如水','卧看牵牛织女星',]
list(map(str,a))==>['1', '2', '3', '4', '5', '6']

list(map(lambda x,y: str(x)+y,a,b))
==>['1银烛秋光冷画屏', '2轻罗小扇扑流萤', '3天阶夜色凉如水', '4卧看牵牛织女星']
此处只可意会,省略100字!!

filter (function, Iterable)
function returns true those elements with the function iterable, to build a new iterator. iterable may be a sequence, a container support iteration, or an iterator. If the function is None, it will assume it is an identity function, that is, all false elements in the iterable will be removed. Please note, filter (function, iterable) is equivalent to a generator expression when the function is not the time to None (item for item in iterable if function (item)); function is None, when is (item for item in iterable if item).

a=[0,0,0,'',1,1,2,3,4,[]]
list(filter(None,a))==>[1, 1, 2, 3, 4]
#如果第一个参数为None,则返回每个元素bool值为真的元素

list(filter(lambda i :not i,a))==>[0, 0, 0, '', []]
#真真假假假假真真!!

The following is a function of two people love to hate:
love this hate is coming at the side of the soft side cruel! !

eval (expression [, globals [, locals]])
argument is a string, and optional globals and locals. globals argument must be a dictionary. locals can be any mapping
shot object. expression parameter as a Python expression (technically is a list of conditions) is parsed and evaluated, and the use of globals and locals dictionaries as global and local namespace. If the dictionary is present and does not include globals in __builtins__ value is a bond, this will be inserted for the built-in module builtins key references before parsing expression. This means that
expression normally has full access to the standard builtins module and restricted environments are propagated. If omitted, it defaults locals dictionary globals dictionary. If both dictionaries are omitted at the same time, the expression will use the eval () is called an environment of globals and locals execution. Please Italy, eval () and does not have access to (non-local) nested under the scope of the external environment. The return value is the result of evaluating the expression.

dic={
	'a':'天上白玉京',
	'b':'九楼十三层',
	'c':'仙人抚我顶',
	'd':'结发爱长生'}
#注意我传进去得是个字符串
eval('dic')==>#他还给我一个字典
{'a': '天上白玉京', 'b': '九楼十三层', 'c': '仙人抚我顶', 'd': '结发爱长生'}

eval('print(a)',{'a':123})===>123
虽然eval()很好用,但是提醒大家还是要慎重,特别是在接收用户输入得时候,因为eval的权限是有点大了。
eval()是由返回值的,返回值是表达式求值的结果

exec (object [, globals [,
locals]]) This function supports dynamic execution of Python code. It must be a string or object code objects. If a string, the string will be interpreted as a series of statements and execute Python (unless a syntax error occurs). If a code object, it will be directly executed. In any case, the code is executed and requires the same input file is valid (see Reference Manual on the input file
section). Note that even though passed to exec () function in the context code, return and yield statement can not be used outside the function definition. The function returns the value None. In either case, if you omit the option, the code will be executed in the current scope. If only Globals, it must
be a dictionary (dictionary is not a subclass), the dictionary will also be used for global and local variables. If both globals and locals, they are used for global and local variables. If a locals, it can be any mapping object. Keep in mind that on the module level, globals and locals are the same dictionary. If two separate objects obtained as exec globals and locals, embedding the code to be executed as in the case of the same class definition. If the globals dictionary does not contain builtins key, that key will be inserted on the built-in module builtins dictionary reference. Thus, prior to the Exec (), can be obtained by their own code to be executed in the transmitting builtins inserted into the dictionary
globals in which the built-in control codes to be used.

exec(exec('for i in range(10): print(i)'))
==>
0
1
2
3
4
5
6
7
8
同样的语句eval是执行不了的,而exec可以,但是exec只注重过程,不要结果,所以没有返回值,而eval是有返回值的。

Do things to stay the course, ending beauty! !
Here Insert Picture Description

Published 23 original articles · won praise 5 · Views 370

Guess you like

Origin blog.csdn.net/weixin_43287121/article/details/105155587