Python @ DjangoSpin

50+ Know-How(s) Every Pythonista Must Know

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 21 minutes

Know-How(s) Every Pythonista Must Know

Know-How(s) Every Pythonista Must Know

Python is an extremely versatile language. Its builtin capability along with its vast community lets you do almost anything. Below is a collection of Know-How(s) that every Pythonista must know.

As of January 2017, there are 86 items in the following list. The collection is split into pages for convenient browsing. You may find the pages menu at the bottom of each page.

Page #1


Running Python scripts

Once you have installed Python, you executed Python scripts (.py files) in the following way:

  • For Windows, press Windows + R for the run dialog, type in cmd and press enter.
  • The command line dialog pops up, now, type python complete_path_to_file.py e.g. python C:\DirOne\file1.py.
  • A handy way to do this entire process is, go into the directory that contains your .py file. Press shift + right click on your mouse > open command window here, then python file.py.
  • You'll see the same output.

The way this works is that the Python keyword invokes the python.exe interpreter which executes your .py file. If you want an interactive Python shell in the command window, type in python and press enter. As you can see, it provides the same window as in IDLE.

  • For Mac users, go into the terminal, navigate into your working directory (that is, the directory you have chosen to keep your learning Python files in)
  • Type in touch file.py, edit it with vi or nano by vi name_of_file.py or nano name_of_file.py
  • Give permissions to your user to execute the script by chmod 777 file.py
  • Then typing python file.py in the terminal will execute it.

The location of Python interpreter in Mac and linux distributions is normally /usr/local/bin/python3. In Windows, it is something like C:\Pythonxx\python.exe if you accepted defaults, this address may vary if you installed it somewhere else and/or if you installed a different version of Python.

If you execute a .py file (containing, say, a single print statement) by double clicking on it, then you will notice that a console window opens for a brief moment, and then it vanishes. In fact, it would have vanished too quickly for you to read anything printed on the screen. There are two ways for making the screen stay till you have read the message in the print statement.

First, after the print statement, in a new line, type input(). Save the file and execute the .py by double-clicking on it. What the input() function does is that it asks for user input.

Second, create a batch file i.e. a .bat file (Windows users), type in python file_name.py, replacing the file_name by your file name and in the second line, type pause. Save this file, and execute it by double-clicking on it. It will produce the same result.


Using the builtin print() function

Every programmer is familiar with the builtin print() function. It's used to producing output onto the console, whether it's the interpreter in IDLE, or in command prompt. It is useful while tracing a piece of code to check which branch of the code is being executed.

	
>>> print("Hello there!")
Hello there!

>>> print(1)
1

>>> print( [1, 2, 3, 4] )
[1, 2, 3, 4]

The print() function has four optional keyword arguments:

  • sep: the separator (a string) between different values, if multiple values provided. Default value - single space.
  • end: end-of-print string appended after the value(s) have been printed. Default value - new line character i.e. '\n'
  • file: stream to which the output is to be dumped to. Can be a file object too. Default value - sys.stdout which refers to console output.
  • flush: when set to True, output of each print() statment is written to the specified stream forcibly. This is useful when we are directing output to a stream. When we perform a write operation to a file, the data is not in file, it is only in the program buffer/internal buffer until the file is closed. Setting flush to True, it writes data from the internal buffer to the operating system buffer after each call to print() function. What this means is that if another process is performing a read operation from the same file, it will be able to read the data you just flushed to the file. Default value - False. To know more about buffers & flush operation, I advise you to visit this link.

Expand the following code snippet for examples illustrating these optional keyword arguments.

' '. You can specify a different separator.

# 'sep'
>>> a = 'Hello'
>>> b = 'there!'
>>> print(a, b)
Hello there!

>>> print(a, b, sep = ' ')
Hello there!

>>> print(a, b, sep = '*')
Hello*there!


# 'end'
>>> print("Hi "); print("there!")
Hi 
there!
>>> print("Hi ", end = '\n'); print("there!")
Hi 
there!
>>> print("Hi ", end = ''); print("there!")
Hi there!
>>> 

# 'file'
>>> fH = open('outputFile.txt', 'w')
>>> print("Hello there!", file = fH)
>>> fH.close()
# contents of outputFile.txt: 
Hello there!





# 'flush'
>>> fH = open('outputFile.txt', 'w')
>>> print("Hello there!", file = fH)
# contents of outputFile.txt: NO CONTENTS
>>> fH.close()
# contents of outputFile.txt: 
Hello there!


>>> fH = open('outputFile.txt', 'w')
>>> print("Hello there!", file = fH, flush = True)
# contents of outputFile.txt: 
Hello there!

Also, if you mean to concatenate two or more strings in a print statement, you need not use the + operator always. You can place them next to each other and print() will render it as a single string.

>>> print( 'Hello'	' '	'there!' )
Hello there!

Slicing a sequence

Slicing means getting a portion of an ordered sequence. An ordered sequence is a sequence whose elements can be accessed using indexes. Examples of ordered sequences are strings, lists, tuples. Here's how you can slice strings in Python.

>>> originalString = "magnanimous"
>>> arrayRepresentationOfOriginalString = '''           # for understanding indexes used later
 
              0   1   2   3   4   5   6   7   8   9  10 
              _   _   _   _   _   _   _   _   _   _   _  
            |   |   |   |   |   |   |   |   |   |   |   |
            | m | a | g | n | a | n | i | m | o | u | s |
            | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ |
            -11 -10  -9  -8  -7  -6  -5  -4  -3  -2  -1 
'''
>>> st = originalString   # for easy reference
>>> st[1:5]               # st[x:y] gives us a string starting with st[x] and ending with st[y-1]
'agna'
>>> st[-5:-1]         # -5 is actually 6 and -1 is actually 10, so this becomes st[6:10]
'imou'
>>> st[1:5:1]         # st[x:y:z] gives us a string starting with st[x], with a step over size of 1, ending with st[y-1]
'agna'
>>> st[1:5:2]         # starting at index 1, ending with index 5-1, with a difference of 2 between the indexes of characters
'an'
>>> st[5:1:-1]        # FOR NEGATIVE STEP SIZE: begin at index 5, move in the opposite direction i.e. left, finish at index 1 + 1.
'nang'
>>> st[-5:-8:-1]
'ina'
>>> st[:5]            # select everything from index 0 to 5-1
'magna'
>>> st[5:]            # select everything from index 5 till the end of the originalString
'nimous'
>>> st[:]             # gives the originalString itself
'magnanimous'

# Slicing takes characters from left to right of a string, unless you specify a negative step size. What this means is that by default, the first number should should be the index of a character on the left of character whose index is denoted by the second number, UNLESS a negative step size is specified, in which case, the first number should be the index of a character on the right of the character whose index is denoted by the second number. Not confusing, right?

Expand for slicing scenarios in lists, tuples, sets & dictionaries.

#######################
# SLICING A LIST: [:] #
#######################
# Slicing in lists works the same way as it does in strings. The indices start from 0. Slicing DOES NOT hamper the original list, unless you assign the sliced list to the original variable.
# [:] gives the original list
print(list1[:])                         # ['one', 2, 'three', 'IV']
# [x:] gives a list starting from element at index 1 till the end of the list
print(list1[1:])                        # [2, 'three', 'IV']
# [:x] gives a list from the beginning of the original list till the element at index(x - 1).
print(list1[:3])                        # ['one', 2, 'three']
# [x:y] gives a list from element at index x till element at index (y - 1).
print(list1[1:3])                       # [2, 'three']
# [x:y:z] gives a list from element at index x till the element at index y, picking every zth element.
list10 = range(0, 10)                   # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list10[::3])                      # [0, 3, 6, 9]
# Negative step size also works. In this case, the element represented by first index you specify has to be on the right of the element represented by the second index, in the original list. Else, it will return an empty list. An exception of this would be [::-z] which will iterate the list from the rear end by default.
list10[10:0:-3]                         # [9, 6, 3]
list10[::-3]                            # [9, 6, 3, 0] # the default value of the second index is -(len + 1) when the step/stride is negative. Note that -11 in the second argument will mean a slice from the end to index -10, which is element 0 in the list. In the above expression, slice is till 0 + 1 i.e. index 1.
# You can of course use negative indexes as well, I'll leave that to you to explore. Comment below if you run into any problem.



########################
# SLICING A TUPLE: [:] #
########################
# Slicing DOES NOT hamper the original tuple, unless you assign the sliced list to the original variable.
tuple1 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
# [:] gives the original tuple
print(tuple1[:])                         # (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
# [x:] gives a tuple starting from element at index 1 till the end of the tuple
print(tuple1[1:])                        # (1, 2, 3, 4, 5, 6, 7, 8, 9)
# [:x] gives a tuple from the beginning of the original tuple till the element at index(x - 1).
print(tuple1[:3])                        # (0, 1, 2)
# [x:y] gives a tuple from element at index x till element at index (y - 1).
print(tuple1[1:3])                       # (1, 2)
# [x:y:z] gives a tuple from element at index x till the element at index y, picking every zth element.
print(tuple1[::3])                      # (0, 3, 6, 9)
# Negative step size also works. In this case, the element represented by first index you specify has to be on the right of the element represented by the second index, in the original tuple. Else, it will return an empty tuple. An exception of this would be [::-z] which will iterate the tuple from the rear end by default.
tuple1[10:0:-3]                         # (9, 6, 3)
tuple1[::-3]                            # (9, 6, 3, 0) # the default value of the second index is -(len + 1) when the step/stride is negative. Note that -11 in the second argument will mean a slice from the end to index -10, which is element 0 in the list. In the above expression, slice is till 0 + 1 i.e. index 1.
# You can of course use negative indexes as well, I'll leave that to you to explore. Comment below if you run into any problem.



#############################
# SLICING A DICTIONARY: n/a #
#############################
# Slicing a python dictionary is not a valid operation, since dictionaries are not index-based data structures like lists. If you try to slice a dictionary, python throws:
TypeError: unhashable type: 'slice'



#########################################################
# SLICING A SET: not supported since a set is unordered #
#########################################################
>>> setOfAnimals = {"cat", "dog", "elephant", "snake"}
>>> setOfAnimals[:]
# Traceback info
TypeError: 'set' object is not subscriptable

Calling functions upon termination of program

The builtin module atexit helps to call functions upon normal termination of Python scripts. This is achieved using its register() function.

# Contents of testAtExit.py
import atexit

def bye():
    print("Goodbye...")

atexit.register(bye)

# Command prompt / Terminal
$ python path_to_testAtExit.py
Goodbye...

Python allows to register more than one functions, and to pass arguments to these functions.

# Contents of testAtExit.py
import atexit

def bye(name):
    print("Goodbye {}...".format(name))

atexit.register(bye, 'Ethan')

# Command prompt / Terminal
$ python path_to_testAtExit.py
Goodbye Ethan...

Note that registered functions are not called in the following events:

  1. When program is exited using os._exit(). The registered functions are called when the program is exited using sys.exit().
  2. When the program is killed with a signal. This can be done using the subprocess module and kill() function of the os module.

Using the pass keyword

Code-blocks in Python are separated by indents and not by curly braces ( {} ). In cases where you don't want to populate the body of a function or a class, you cannot leave it completely empty. In fact, Python won't allow you to leave it fully vacant. For this purpose, Python has the pass keyword. This keyword is convenient to draft a high-level design of a complicated solution. The user can code on the functionality of the function of the later, he can emphasize solely on the structure of the program.

def functionalityOne():
    pass
 
def functionalityTwo():
    pass

Installing external modules using pip

Python comes with many useful modules out-of-the-box. In addition to these modules, there is a large library of third-party modules which Python developers have distributed for fellow developers to use. These modules are listed in Python Package Index (PyPI), also known as the Cheese Shop, with a reference to Monty Python's Flying Circus, the TV show after which the language was named. These external modules come in wheel files (.whl) or executables (.exe). Wheel files can be installed using pip.

pip is Python's package management system, and is shipped with the installation in versions 2.7.9+ & 3.4+. This decision of including a package manager with the standard installation was welcome graciously by the Python community, as it spared them the hassles of setting up a package manager themselves. This puts Python in a bracket of programming languages which ship their package manager with standard installations, such as Haskell, Ruby, Node.js. The word pip is a recursive acronym that is interpreted to mean either "Pip installs Python" or "Pip installs Packages".

For Python versions 2 - 2.7.8 & 3 - 3.3, you will have to save this page as get-pip.py in your system, and then run it using command-prompt (in Administrator mode) i.e. $ python get-pip.py, ensuring you are in the current directory.

Before pip, a builtin Python module called easy_install was used to install third-party modules. In fact, in versions mentioned above, it is still used to install third-party modules.

# Installing a .whl using pip
# Command Prompt/Terminal
$ pip install file_name.whl

# Installing an external module directly using pip. The mentioned module must be present in the Cheese Shop.
$ pip install cx_Oracle


# In case you get a .exe file from PyPI instead of a .whl file, you can execute the .exe files directly without using Python.
If pip is not working, it might be due to the fact that it is not in your path environment variable. One workaround for this issue is to run pip as a script by using the -m flag of python command line.

$ python -m pip install cx_Oracle

There are chances that you might receive an error while installing these modules. This could either be due to the 32/64 bit operating system issue, or the language version incompatibility or some other reason. In these events, Google the error, since it is very likely that the same error could have been encountered by fellow Python developers.


Calculating the sum of elements of an iterable: sum()

The builtin sum() function returns the summation of an iterable of numbers. It has an optional second positional argument, which denotes the value to start the summation with. Its default value is 0. In the event that the iterable is empty, it returns the value of 'start'.

>>> sum( [1, 2, 3, 4] )
10
>>> sum( range(1, 11) )
55

Using the with keyword

The with keyword automatically shuts down resources as soon as the code block following it is executed. It is considered good practice while dealing with connections, file objects etc. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally clauses.

>>> with open('fileToBeReadFrom.txt', 'r') as fH:
       contents = fH.read()
>>> fH.closed
True

If you wish to know more about how the with statement works, click here.


Getting help from Python: Using the builtin function help()

Python's builtin help() function the description of the object passed to it, in addition to its basic usage. For example, if you were to know how to use the replace() method of strings, you would execute help(str.replace).

>>> help(str.replace)
Help on method_descriptor:
 
replace(...)
    S.replace(old, new[, count]) -> str
     
    Return a copy of S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.

The help() function without any argument gives you the help console, where you type the object you seek help on.

>>> help()
 
Welcome to Python 3.4's help utility!
 
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.4/tutorial/.
 
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".
 
To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".
 
help> str.replace
Help on method_descriptor in str:
 
str.replace = replace(...)
    S.replace(old, new[, count]) -> str
     
    Return a copy of S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.

Exploring an object: Using the builtin function dir()

The dir(object) function returns a list of all the methods and associated with the mentioned object. Everything in Python is an object, be it variables, classes, functions etc. The parent data types are also objects, so if you pass 'str' to this function, it will list every method(function) and attribute associated with it. And to gain what all do these related objects do, you can use the builtin help() function.

>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

>>> help(str.replace)
Help on method_descriptor:
 
replace(...)
    S.replace(old, new[, count]) -> str
     
    Return a copy of S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.

The dir() function without any argument gives you all the objects in the current scope i.e. the variables you have declared after you opened the interpreter.

>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b', 'c', 'coordinates', 'd', 'dimension', 'n', 'newSetOfAnimals', 'primes', 'set2', 'set3', 'setOfAnimals', 't']

See also: 50+ Tips & Tricks for Python Developers


Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply