Python Tutorials 2 – Conditions and Loops

Hello Guys !! Hope our previous article regarding the Introduction to Python helped you all in some aspect. Futher,we would like all of you to provide us your useful feedbacks regarding the tutorials so that we can improve on our skills and methods.

Python-Tutorials-Conditions-and-Loops

Python Tutorials 2 – Conditions and Loops

Now here is the article regarding the CONDITIONALS $ LOOPS in Python.

CONDITIONALS

As most of us are aware of C/C++/JAVA ,so we all have some prior knowledge of Conditionals in programming.The most used ones are if and switch statements.

The if,if-else and the switch statements in C/C++/JAVA require the use of {} for the line of codes where as it is quite different in the case of Python.

Example:

#!/usr/bin/python

 

var1 = 100

if var1:

print “1 – Got a true expression value”

print var1

else:

print “1 – Got a false expression value”

print var1

 

var2 = 0

if var2:

print “2 – Got a true expression value”

print var2

else:

print “2 – Got a false expression value”

print var2

 

print “Good bye!”

They are actually  block of codes called suites in Python Documentation and they simply consist of code that is indented under the level of whatever it is that’s controlling it.Four space of indenting is considered standard in Python.

Also,we can use the if conditional as:

Print (“foo” if a<b else “bar”)

SWITCH CASE

The switch statement in Python is same as C/C++/JAVA.The example is given below.

switch(n) {

case 0:

printf(“You typed zero.\n”);

break;

case 1:

case 9:

printf(“n is a perfect square\n”);

break;

case 2:

printf(“n is an even number\n”);

case 3:

case 5:

case 7:

printf(“n is a prime number\n”);

break;

case 4:

printf(“n is a perfect square\n”);

case 6:

case 8:

printf(“n is an even number\n”);

break;

default:

printf(“Only single-digit numbers are allowed\n”);

break;

}

 

LOOPS

A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:

Python programming language provides following types of loops to handle looping requirements.

Loop Type Description
while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
nested loops You can use one or more loop inside any another while, for or do..while loop.

 

Loop Control Statements:

 

Python supports the following control statements.the best of your health.

Control Statement Description
break statement Terminates the loop statement and transfers execution to the statement immediately following the loop.
continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
pass statement The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

 

The details regarding the coding part of the looping statements would be given in our next tutorial.

Feedbacks are awaiting.Till the:

“Stay Happy : Stay Foolish : Stay Blessed “