So today we will be starting our journey into the world of python, not the reptile one, but the python language. It is a high level language which is used to write general purpose programs, it was created by Guido van Rossum and was first released in 1991. I know you are not interested in knowing the history etc, so without beating around the bush let’s start some learning. To run any language you need a platform, hence to run python language you have to download python interpreter, you can download from following link, https://www.python.org/downloads/.

They will be providing you with their latest versions, right now I am using Python 3.6. After installing, now you can start your coding in 2 modes, one is the IDLE shell window and other is python window(looks like command window)

Python Command Mode
IDLE shell window

Usually it is preferred that programs are written in form of function in shell window and are then called after compiling. When program is successfully executed and working, it can be easily executed in python window. So today we will know how to use the python shell and use function in it. Python is a very easy language as there is no need of any definitions like declaring data types, size etc, straight away the instructions are given.

For example, we can use it as a calculator. Simply give two numbers with ‘+’ operator like we do in normal math. We can store string in any variable, no need of declaration, so it is pretty neat and compact.

Here we have declared a and b and then simply added them using ‘+’ operator and also we are storing a string in a variable x, then simply displaying it by writing ‘x’ and pressing enter.

Now we will learn how to build a function and call that function in idle/python window. The syntax for declaring a function is, there is no separate function prototype and function definition:

def function_name():

Now in python one thing we have to take extreme care is about regular spacing called indentation. So all the functions should come in proper line spacing and all the contents inside function should have same spacing. Now for writing a function, we are opening our IDLE window and then opening new file. The function code is shown in figure given below:

Here our function name is ‘square’, since we have to pass our number to calculate the square, one parameter is given inside function, square(n). k is used to hold the result which is displayed using print statement.

Then we have to run our code with run option, if code is correct and free from error, it will restart your shell window and you are ready to use your function. Usually save your filename and function name as same, as it will be easy for working. If any error occurs, correct the errors and again run.

Upon successful execution of code, your shell window will open like this message of restart.

Now since our function is created, we have to call that function. For calling that function, syntax is:

from filename import *

So here we give, from square import* which will import my square function. Now our function is available so we can use it, so we have to call it like normal function in C. Eg. Sqaure(5), will give us square of 5 as follows.

So here we saw how to easily create a function and then call it, our function had an integer parameter here, we can declare and call any kind of functions with the same approach. This was a very simple program, so now you can try a function which takes two parameters and display the sum,diff,div and mul of those numbers. I will be writing the solution in next blog, till then keep coding..

VLSI Engineer with the urge for sharing knowledge.