Pravar Agrawal Technology & Travel

Python Learning Part-1

I will be posting a series of posts as part of my learning on Python in coming few days. I’m undergoing training under Dgplug summer training and currently learning pym module. The pym book is quite handy when learning python from scratch. Although I’ve been working in Python for quite a few years, this has come as a good refresher to me.

Python, which has become one of the most popular and widely used language in the community - is an interpreted language. And being that, we can either write our code directly into the interpreter or by writing it in a file and then run it. Most widely used method is the latter one i.e. putting the code in a source file and then running it. Python requires whitespace to be specified as indentation in it’s code. Whitespace in the begining of the line is known as indentation. And if we give wrong indentation, it throws error. For example,

>>> a = 12

Above shown is the correct way to write Python code with indent specified. Always, we need to use 4 spaces for indentation and also never to mix tabs and spaces together. Apart from this, there has to be one blank like between functions and two blank lines between classes.

We can put comments in our Python code by using # like,

# This piece write here won't show up

Comments are a great way to put help inside our code so that whoever is reading it can get an idea about that part of code. It even helps ourselves later, when we are referring to our own writing again.

Modules in Python are collections of files that contains different functions or variables which can be reused. A module file should always end with .py extension. To use a module we give like,

>>> import os
>>> print(os.getcwd())

Let’s understand the code construct in Python. For any programming language including Python, Keywords and Identifiers are an integral part. Just like grammer is to the languages we speak. In Python, we do not specify the type of data for a variable. A variable declaration is given like,

a = 12

Here, we haven’t specified any data type for a although it is an integer type variable with value 12. While writing a programme which requires user’s involvment like getting input values either be a number or a string we can use Python’s input() method. For example,

value = int(input("Enter the value: "))
print(value)

Above code example will return the value taken by the user input and stored under value variable.

We’ll continue with further interesting features of Python in the upcoming posts.