In Python Programming Language – A Deep Primer Part 1, approximately all the basics were covered that you need to know. That would help you start your journey pretty quick.

Readable Code in Python

Although you can code python in any transliteration from any type of human or robotic language. You can write your variable, function, class etc names in any gibberish. But remember when you come back to see your own code, you won’t be able to understand it. So think of your code as a story told to a toddler. Write code that is readable, understandable, and deliverable.

Alway document what your classes and functions do. Make use of comments where the comments are due. You can make use of a concept called ‘documentation string’ or ‘docstring‘ for short. In the example I will be adding docstrings, so keep an eye on that as well. Try to write a terse, and to the point, docstring for each function that you write. Once you have the habit of it, your code will go a long way. Here is an example docstring, anyhow:

Use of Exceptions

Exceptions are a special way of programming your code. In this way you can use a specific code giving it a special surrounding syntax, which will make sure that if the warning or error condition occurs, your code will handle it. Many programmers thing of exceptions as best practices. But remember it is more than a best practice. Make sure you do it often as required. The exceptions give you indications that something went wrong. It is highly desirable in Agile paradigm as well. If you can catch errors at an early stage, it can save you time in the long run.

Almost all the modules in the python library make use of them extensively. Remember to use the terminology of ‘handle the exception’. So always handle the exceptions in your code. This also means there there are certain exceptions which are un-handled exceptions. Always make sure they are handled, if possible.

Possible examples of exceptions are:

If you to open a database in order to write in to it, you might get an exception that database is in-accessible. If you want to convert a string to something else, but you find out that what you are given is actually not a string. If you want to reference some module, and you come to know that the API (Application Programming Interface) or interface of that module has changed. And the list of possibilities goes on.

How to handle an exception

You can handle an exception using try…except block. Your test code goes into this block, and your handling code is also situated side-by-side. The terminology to check for is that a certain piece of code can raise an exception. So when it is raised, handle it using try…except block. You can raise an exception using raise statement. Always remember that if you have nested functions in nested classes, your exceptions travel bottom up. So if function at one level raised an exception, you can handle that exception somewhere along the path upwards e.g. the function that called the function that raised an exception. It is complex but once you get to do some examples that would become quite easy and intuitive. For now move on. Let us see an example.

Here is an assignment, enlist or find different types of exceptions. As an example, check out the NameError, TypeError, and KeyError exceptions.

Comprehensions

This is one of the most important concepts. This has huge implications and usage. It is a complex one. But python has simplified it dramatically. You will love the concept, and how easy it is to use, and get benefit from. It is a powerful technique using which you can use elements from one type e.g. sets, lists, or dictionaries, to map to another similar type, and run functions against it. In other words, you can build sequences from other sequences. You can manipulate the lists, and you can also filter the lists based on some criteria or function. You can also use functions within the comprehensions as well. So the possibilities are limitless. There are three kinds of comprehensions viz. list comprehensions, set comprehensions, and dictionary comprehensions.

List Comprehensions

You can map a list to another list by applying a function to each element of the second list.

In the above snippet, you should notice few things. It has got an input sequence. Which in our case is variable named words. We have a variable called w which represents elements or members in our input sequence. An expression, although optional, ‘for w in words’, our predicate, which works using input sequence and control variable for elements. An output sequence, title_stats in our case, which satisfies the predicate.

You can use any predicate. For example, you can use an operator, an if statement, a for-in loop etc.

Map, Filter and Anonymous Lamda

You can create new list sequences, using existing sequences by other means as well. For map, filter, and lamda functions are also used to achieve the same goals as above. I will leave them as a small assignment to you. There is another way in which you can use all these functions together in a single statement, do explore that as well. Also if you have any questions, write a comment as well.

Set Comprehensions

The thought behind the set comprehension is the same as it is for list comprehension, but you get a set as an output instead of a list. Remember that the set contains unique elements.

Dictionary Comprehensions

It is similar to list, and set comprehensions. But it gives a dictionary in the output or after the construction. Let’s see an example:

Unicode

If you want to include any other human-readable language with any sign, you can make use of Unicode. Unicode is a system. This system gives you power to handle any character or symbol in your application. Unicode handling has matured pretty well in python. And this is the area you should know. World is becoming a global village, and sometimes in future you might require a multi-lingual capability to your applications.

Unicode system represents each ideograph as a 4-byte number. An ideograph can be any character or symbol.  4-bytes consume a lot of storage, since english needs only less than 8-bits, and even Chinese requires less than 2-bytes. So we have more encoding that support Unicode. E.g. UTF-32, UTF-16, UTF-8 etc. As we go towards UTF-8, we get space-efficiency, but increases the complexity. But designers are smart, and we need not bother right now for learning sake. When it comes to algorithm implementations in python, it needs to be rethought.

For now, just remember that every character in Python 3 is a Unicode character. Every string is represented in Python 3 as a Unicode character, so hurray & hi5 on that.  The encoding behind the scenes becomes irrelevant for now. The expectation is that all of your Python source code should be written in UTF-8. This also means that any programmer can also type any language character in the strings. If you want different encoding to use in your code, then you can specify that using # -*- coding: UTF-32 -*- at the top of your source file.

I hope you must have enjoyed the part 2 of the primer. This is the first edition of the Part 2 of the primer. If you would like me to cover some other topics, feel free to message in the comments, and let the discussion rolling.


The author can be reached at khawar@infoginx.com.

If you would like to contribute on Infoginx, feel free to reach out at info@infoginx.com.