What comes to your mind when I say ‘python’? The snake, of course! But, no! I am not referring to the venomous snake species here. Instead I am focusing on a renowned programming language used for coding software. All the computer techies must be familiar with this programming buddy. However, this article is intended to enhance the knowledge about python in the minds of people who are not so computer friendly.
Python is the easiest and most flexible coding language when we talk in the terms of software development. It is an open source language, which means it can be easily accessed and used free of cost. You can use this program for a variety of purposes ranging from coding simple programs to designing games and other major development programs.
Getting Started with Python
For beginners to start using python, it is important that you have a computer with an editor, a terminal and python installed on it. The editor which you should begin using can be a notepad++ (windows). It is preferable if you begin working on python 2 rather than jumping onto python 3 version.
Most common pitfalls you should be aware of before beginning to use Python:
These pitfalls are nothing but mistakes which newbies and at times even computer pros can make. So, in order to avoid facing such a situation let us learn more about these pitfalls and focus on avoiding them while programming:
1. Binary mode for files
Sometimes there may be some confusion as to what file mode needs to be utilized for opening up a particular file. For example, you may need to read the file using the following codes in order to distinguish between the text mode and the binary mode.
f1 = open(filename, “r”) # text
f2 = open(filename, “rb”) # binary
The most appropriate thing to do in such a situation is to use ‘r’ for text mode while ‘rb’ for binary mode.
2. Mutable default Arguments
This is one of the most nail-biting problem faced by most of the beginners. Python deals differently with mutable and immutable objects unlike Pascal or C++.
>>> def popo(x=[]):
… x.append(666)
… print x
…
>>> popo([1, 2, 3])
[1, 2, 3, 666]
>>> x = [1, 2]
>>> popo(x)
[1, 2, 666]
>>> x
[1, 2, 666]
From the above example the most obvious output would have been [666] as popo() is called without arguments then obviously[] takes the default argument for x but the default argument for this program would be bound once when the function is created and not when it is called upon. Sometimes this behavior can be very helpful but you need to watch out for any side effects if present.
>>> popo()
[666]
>>> popo()
[666, 666]
>>> popo()
[666, 666, 666]
3. Inconsistent indentation
In python, we use white space indentation instead of the usual curly braces. You can create white space indentation either by the space bar or the enter key in order to delimit the group of codes. So if you face inconsistency in indentation then try using all spaces or all tabs or try to consistently indent the codes. Beware! Don’t mix them up.
4. Catching multiple exceptions
Exceptions are nothing but errors which are detected by the Python program while executing the statement or the code. The codes being syntactically correct still sometimes may show error while its execution is being done.
try:
…something that raises an error…
except IndexError, ValueError:
# expects to catch IndexError and ValueError
# wrong!
>>> try:
… 1/0
… except ZeroDivisionError, e:
… print e
…
Integer division or modulo by zero
The example in the first box is an exception while the second statement is an option that may be used to bind the actual exception code. The most appropriate thing to do in such an instance is to use parentheses in order to create a tuple with exceptions.
5. Floating point rounding errors
This is one of the most surprising errors in terms of the result you will be given. Printing the results will have different outcomes when you are using floating numbers. A simple representation of the str() and repr() will vary the final outcome. Such as:
>>> c = 0.1
>>> c
0.10000000000000001
>>> repr(c)
‘0.10000000000000001’
>>> str(c)
‘0.1’
The actual value is approximated in base 10 as many numbers cannot be represented exactly in the base of 2, most commonly used in the computer hardware. So try to be careful in what you may be using while entering your results.
So after this article on python, I am sure you are much more confident to face this giant python with zeal and ambition to conquer it.
You may also be interested in
6 Pillars of Python: Assessment of Best Python Web Frameworks
Code Wars: PHP vs Python Vs Ruby [Infographic]