Introduction to Python

Python is a new generation, powerful programming language. It is called as “language of the future”. The specialty about python is that it can perform highly complex tasks with lesser lines of code. The python code is highly readable, easy to understand and debug. Python can be used in variety of domains, from simple functional programming problems to complex problems like those involving machine learning, Artificial Intelligence etc.
In this article, you will be taught basics of python programming. We will start with a simple hello world program, and gradually move to variables, if-else statements and loops.

“Hello World!” in Python

Below is a code to print “hello world” in python. Open python IDLE, type in the following code and press enter:

print("hello world")

output: hello world
As we saw above, print function prints whatever value is given to it on screen. We can extent this function to variables as well. For example, consider a variable x with current value 1. Then to print value of x, we simply type the code below in IDLE:


output: 2

another example:

y="welcome to New York"

output: welcome to New York

As we saw above, to print a string, we need to enclose it in quotes, but to print value of a variable, we directly write the variable in print function.

Math in Python

Python can be used for mathematical calculations. For example, type the following in IDLE:


output: 4


output: 3

Variables in Python

Variables can be used in python to store any information. The most interesting thing about python variables is that they do not need their data type to be predefined. Unlike languages like C where we have to declare a variable with its data type before using it, python variables can be used straight for storing a given data, without telling its data type. For example, create a new python file, type in the following code, and run it:


output: 5

as we saw in the above code, we did not need to predefine the data type of x, y and z. We straightaway started using them to store numbers.

If- Else statement in Python

Like most other programming languages, python also has if-else statement. They are used to do different tasks with different conditions. The basic skeleton goes like this: “If this, then do something, else do something else”. Below is a code which explains their working:

if x=2:

output: happy
another example:

if y==1:

output: 2
Apart from if-else, we also have if-elif-else statement in python. Elif if basically an equivalent of “else if” in C programming language. Here is a tutorial on how to use it:

if x==”hi”:
print(“hi!, how are you?”)
elif x==”hello”:
print(“hello, nice to meet you!”)

output: hello, nice to meet you!
Thus we saw how we can put conditions for a piece of code to run using if-else statements.

Loops in Python

To run a particular piece of code many times, loops are used. A piece of code if wrapped up inside a loop, and in the loop syntax, it is specified how many times to run that loop. Python, like most programming languages, have two types of loop, for loop and while loop. Let us understand working of both using examples given below:

for x in range (0, 4):

output: hello
Let us understand what happened above. We set the initial value of x=0. now we write for x in range (0, 4). This means that till the value of x remains in the range (0, 4) i.e 0, 1, 2, 3, run the piece of code wrapped in for loop, and also increment the value of x by one each time we run the code. Thus in the output, we received “hello” four times, i.e one time for each value of x in the given range.
Now let us also look at an example of while loop:

while x>0:

output: hello
what basically happened this time is, we first set the value of x=4, then we put a condition using while loop. It states that till value of x is greater than 0, run the code wrapped in loop. But in while loop, we have to adjust the value of x by ourselves, which we did using statement: x=x-1. It is not like for loop where x got incremented automatically. So the code checked each time if x>0, if true then it printed “hello”, then it decremented x by one. After printing “hello” four times, the value of x became 0, thus x was no longer greater than 0, thus “hello” could not be printed further.
Thus we end with basic introduction to python, This much knowledge is good to go for initial stage of learning python. keep practicing and stay tuned to The Coding Bot!

Leave a Reply

Your email address will not be published. Required fields are marked *