Objective
|
Page Index
Lesson
The Python interpreter has a number of functions and types built into it that are always available.
This means that you don't have to import anything so that you can access them.
Some of the built-in functions are so powerful, simple, common or useful, such as |
class range(start,stop[,step])
In the python documentation the Of the three arguments >>> list (range(4))
[0, 1, 2, 3] # 'stop' is not included in the range.
>>>
>>> list (range(4,9))
[4, 5, 6, 7, 8]
>>>
>>> list (range(4,37,7))
[4, 11, 18, 25, 32]
>>>
>>> tuple(range(3,-7,-2))
(3, 1, -1, -3, -5)
>>>
>>> tuple(range(3,-7,0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: range() arg 3 must not be zero
>>>
>>> tuple(range(3,-7,4))
() # Empty range.
>>>
>>> range(-2,29,5)[2:5]
range(8, 23, 5)
>>>
>>> range(-2,29,5)[2:5][:2]
range(8, 18, 5)
>>>
>>> range(-2,29,5)[17]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: range object index out of range
>>> range(-2,29,5)[2]
8
>>> range(-2,29,5)[-1]
28
>>>
An operation on >>> 6 in range(-17,193 , 7)
False
>>> 4 in range(-17, 193 , 7)
True
>>>
Try it with big numbers. >>> 100_000_000_000_000 in range ( -5_123_456_789, 1_000_000_000_000_000, 7 )
False
>>> 100_000_000_000_001 in range ( -5_123_456_789, 1_000_000_000_000_000, 7 )
False
>>> 100_000_000_000_002 in range ( -5_123_456_789, 1_000_000_000_000_000, 7 )
True
>>>
>>> len(range ( -5_123_456_789, 1_000_000_000_000_000_456_789_123, 789_012_456 ))
1267407114292763
>>>
>>> len(range ( 5_123_456_789, -1_000_000_000_000_000_456_789_123, -789_012_456 ))
1267407114292763
>>>
>>> a,b,c = 5_123_456_789, -1_000_000_000_000_000_456_789_123, -789_012_456
>>> d,e,f = 407114292763 , 267407114292763 , 674071142
>>> range(a,b,c)[d:e:f]
range(-321218248000514199139, -210987544000000514199139, -531850527268144752)
>>> len(range(a,b,c)[d:e:f])
396_101
>>> range(a,b,c)[d:e:f][763::2345][-3]
-207760474949976816255955
>>>
>>> # and it's very fast.
|
globals()
This expression has the appearance of a function but it returns and behaves like a dictionary representing the current global symbol table. Invoke python on Unix and display information about $ python3.6
Python 3.6.3 (v3.6.3:2c5fed86e0, Oct 3 2017, 00:32:08)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> type(globals())
<class 'dict'>
>>>
>>> globals()
{
'__name__' : '__main__',
'__doc__' : None,
'__package__' : None,
'__loader__' : <class '_frozen_importlib.BuiltinImporter'>,
'__spec__' : None,
'__annotations__': {},
'__builtins__' : <module 'builtins' (built-in)>
}
# Braces '{}' enclose a dictionary.
# Each key '__name__', '__doc__', '__package__' .... is a string.
# Each value '__main__', None, None, .... may or may not be a string. Most values above are not strings.
>>> quit()
$
When you invoke python interactively, the above global variables are predefined. Unless you really know what you are doing, it is recommended that you do not attempt to change any of the predefined global variables. Python source file $ head -3 tmp1.py
# tmp1.py
print ('\n'.join([ ((v + (' '*30))[:20] + str(v1)) for v in globals() for v1 in (globals()[v],) ]))
exit (0)
$
When python is invoked by executing python source file $ python3.6 tmp1.py
__name__ __main__
__doc__ None
__package__ None
__loader__ <_frozen_importlib_external.SourceFileLoader object at 0x10189e2b0>
__spec__ None
__annotations__ {}
__builtins__ <module 'builtins' (built-in)>
__file__ tmp1.py
__cached__ None
$
The values Within the listcomp above:
Accessing dictionary globals()
Creating global variables
Importing modules
Global communication
|
locals()
This expression has the appearance of a function but it returns and behaves like a dictionary representing the current local symbol table.
At module level, globals and locals are the same dictionary. This means that,
outside the body of a function, Outside a function
Inside a function
Variable name duplicated
Inside a listcomp
|
Assignments
|
Further Reading or Review
|
References
1. Python's documentation: 2. Python's methods:
"class range(start, stop(, step))", "Ranges", "globals()", "locals()" |