The future module
With in-built future module, we can import python 3.x functions to python 2.x.
Division operator
Integer division ‘/’ gives an integer in python 2 but it gives a float in python 3. Python 3 has a floor division ‘//’ to get integer value.
Print function
Python 2’s print statement has been replaced by the print() function in python 3
Unicode
Python 2 has ASCII str() types, separate unicode(), but no byte type.
In Python 3, we have Unicode (utf-8) strings, and 2 byte classes: byte and bytearrays
Xrange
In Python 3, the range() is like the xrange() function in python 2. xrange() function does not exist in python 3.
Keywords are the reserved words in Python. In side python these words have its own meaning and usability. You can always get the list of keywords in your current version by typing the following.
import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def',
'del', 'elif','else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Remember these key words are specific to a particular version of python. Some keywords may be different in different versions.
Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching.
At the lowest level, a raw memory allocator ensures that there is enough room in the private heap for storing all Python-related data by interacting with the memory manager of the operating system. On top of the raw memory allocator, several object-specific allocators operate on the same heap and implement distinct memory management policies adapted to the peculiarities of every object type. For example, integer objects are managed differently within the heap than strings, tuples or dictionaries because integers imply different storage requirements and speed/space tradeoffs. The Python memory manager thus delegates some of the work to the object-specific allocators, but ensures that the latter operate within the bounds of the private heap.
Python supports following operators
• Arithmetic Operators (+, -, , /….)
• Comparison Operators (>, <, !=, ==, >=, <= …)
• Assignment Operators (=, +=, -=, =, /=…)
• Boolean Operators (or, and, not)
• Bitwise Operators (&, |, ^, ~, )
• Membership Operators (in, not in)
• Identity Operators (is, is not)
• Shifting Operators (<<, >>)
help() and dir() both are built-in functions of python.
help() function:
If no argument is given “help()”, the interactive help system starts on the interpreter console.
If argument is given then it shows its documentation string along with modules, keywords and attributes.
>>> def myfunc():
... '''This is myfunc'''
... x = 2
... return x
...
>>> help(myfunc)
Help on function myfunc in module __main__:
myfunc()
This is myfunc
dir() function:
Without arguments, it returns the list of names in the current local scope.
With an argument, attempt to return a list of valid attributes for that object.
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'myfunc']
>>> dir(myfunc)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
To make a Python Script executable on Unix
• Script file's mode must be executable and
• the first line must begin with # ( #!/usr/local/bin/python)