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.

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.

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.

The following are the valid symbols used in conditions.

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.

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,

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,

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.

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.

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

Posted in CPython, Python, Visual Studio, Visual Studio 2013 Tagged with: , , , ,

Leave a Reply

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

*