DECISION MAKING AND LOOPS IN PYTHON

From few of previous posts we started to talk about Python. So, from this text I’m going to introduce you, decision making and looping in Python. When we want to execute something after checking a some condition, then we can use decision making and when we want to do something repeatedly, then we can use loops in Python.

DECISION MAKING

To do the decision making Python provides some syntaxes such as ‘if‘, ‘if-else‘, ‘if-elif-else‘. Let’s have a close look at each of them with few examples. In looping and if statements, indentation is a must.

  • If statement will check for a condition and if the condition is true then it will execute the code inside if statement. Otherwise it will skip that part. Syntax is,

if (condition):

<action>

1.png

2.png

  • If else statement provides opportunity to do something when the condition is false as well.

if (condition):

<action>

else:

<action>

3.png

4

  • If elif else statement can be used to check multiple conditions.

if (condition):

<action>

elif (condition):

<action>

else:

<action>

5

6

LOOPS

Next will see how to use loops in Python. There are two types of loops used in Python as ‘while‘ and ‘for‘.

  • while loop will do it’s action repeatedly until the condition becomes false.

while (condition):

<action>

<update>

7.png

8.png

  • while can be used with the else as well.

while (condition):

<action>

<update>

else:

<action>

9

10

  • for loop will get it’s number of iterations from a sequence.

for <item> in <sequence>:

<action>

11

12

13

14

  • for statement also can be used with the else statement as well.

15

16

LOOP CONTROL STATEMENTS

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. There are three loop control statements in python,

  1. break
  2. continue
  3. pass

Let’s see how they work with examples,

  • break statement will exit from the loop at a particular instance.

17

18

  • continue statement will skip only the particular instance and then continue the loop as normal.

19

20

  • pass statement actually do nothing. But if we want to execute something at a particular instance of the loop then we can use pass statement.

21

22

From today’s post we talked about how to do decision making, how to use loops and how to control loops in Python. Hope now you have a clear idea about what are they and how do they work. Hope to see you soon with another important topic. Thank You!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s