Introduction to Python – Part 3

This is 3rd post of Introduction to Python. In my previous post, I give brief introduction about numbers and strings. In this post I will show you how to write conditions and loops.

If statements

Conditions play an important role in any programming language. Where you decide if this action happen then do this. Or if this condition is true perform this action. In python we use if statements for decisions.

answer = int(input("Enter an integer: "))
if answer > 0:
   print("You enter a positive number")

The above code sample show you how to write down the if statement. If keyword then condition and in the next indented line print statement if condition is true.  Here you need to take care of two things that after condition statement semicolon is important and the other thing is indentation. If the print statement is not properly indented then the statement will not work.

If we have multiple conditions to check then we will use elif.

answer = int(input("Enter an integer: "))
if answer > 0:
   print("You enter a number greater than zero")
elif answer < 0:
    print("You enter a number less than zero")
elif answer == 0:
    print("You enter zero")

Else will be used to catch any condition which is not listed.  The example below show you how else keyword is used. And also you will observe that I added two statements under else condition. We can write n number of statement under a contition if it is true. But indentation is the thing we need to keep in mind.

answer = int(input("Enter an integer: "))
if answer == 0:
    print("You enter zero")
elif answer > 10:
    print("You enter number greater than 10")
elif answer < 0:
    print("You enter a negative number")
else:
    print("You enter the number which is less than 10")
    print("But greater than zero")

The following are the valid symbols used in conditions.

== 	is equal to
!= 	is not equal to
< 	is less than
> 	is greater than
<= 	is less than or equal to
>= 	is greater than or equal to

Loops

Sometimes we need to repeat an action more than once. In programming languages we use loops to repeat a task. In python, we have for and while loops. Both will use for same purpose but there is a syntax difference.  I will first explain the for loop. Let’s explore the syntax of for loop, In the below example I want to print numbers from 0 to 4.

for i in range(5):
    print(i)

The above syntax shows you how to write for loop in Python. To generate arithmetic progression I use the builtin function range(). Range function accepts multiple parameters. As you are aware of the loop starts from 0. So the above code will written an output of 0 1 2 3 4  Whereas if we want to start the loop count from 1 then the syntax will be like this,

for i in range(1,5):
    print(i)

Now the output will be 1 2 3 4 . In the same way we can tell the loop to skip the values at a specific step as well. For this the syntax will be look like,

for i in range(1,5,2):
    print(i)

And the output will be 1 3. In the above code we set the loop to start from 1 and then skip the value at step 2.  This is quite handy feature of Python.  For loop is very handy in reading the lists. In my blog about lists we will see how we can enumerate through lists using for loop.

Sometimes we want to perform an action until a certain condition not get true. In this situation we can use while loop. But also it depends on the programmer as well. Some programmers feel more comfortable using for loop whereas some loves while loop. To print a range in while loop the syntax will be as follow.

index = 0
while index < 5:
    print(index)
    index += 1
answer = 0
while answer <= 10:
    answer = int(input("Please enter number greater than 10: "))
print("loop end!!!")

In the above code the loop will not terminate until unless the user input is not greater than 10.  When working with iteration the break and continue methods ares very useful. It happen when we want to break the loop even if the condition is not meet. Look at the example below where we stop the execution even if the loop condition is not meet. The loop will terminate if user input is 5.

answer = 0
while answer <= 10:
    answer = int(input("Please enter number greater than 10: "))
    if answer == 5:
        print("Input number is 5")
        break
print("loop end!!!")

Similarly the continue method continues with the next iteration of the loop.

Leave a Reply