Working with Python – Control Statement and Loops

Introduction

In this blog post, we are going to discuss about the control statement and loops in Python. We are trying to make all the blog straight to the point without any extra discussion as we are all developers and we have enough idea in programming concept. What we have to learn is the Syntax and usages only.

Hope it will be interesting.

Control Statement

Here in Python only one control statement exists and it is IF condition.

 

IF Condition

Syntax:

if < Cpntition > :

    Steatement

else:

    Statement

 

Remember the concept of Line alignment as there is no Begin or End statement there.

Example:

x=input("Enter Your Name:")

if x=="Joydeep":

        print("Hi Joydeep")

else:

        print("Who are you")

 

Enter Your Name:Joydeep

Hi Joydeep

 

We can use nested IF condition.

In Python there is no CASE like statement. By pro-grammatically we can achieve that by using Dictionary mapping.

Alternate of CASE

def getval(ind):

    dic={

         1:"Joydeep",

         2:"Mayuree",

         3:"Sauryya"

        }

    return dic.get(ind, "Not Found")

 

print(getval(2))

Mayuree

 

Looping in Python

In python, we have While and For loop only. There is no Do…While Loop like other Programming language.

 

For Loop

Syntax:

for <variable> in xrange(<an integer expression >):

      <statement-1 >

      <statement-n >

 

Example:

lst=[1,2,3,4,5]

for n in lst:

    x=n*5

    print(x)

 

5

10

15

20

25

 

While Loop

Syntax:

while <condition>:

           <statements>

 

Example:

i=1

while i<=5:

    print(i)

    i=i+1

 

1

2

3

4

5

 

 

Hope you like it.


Comments

  1. Scope of variable, like global, local is another concept that we all knows in other programing language. In python we have understand that. Mostly in my next post it is related to that.

    ReplyDelete

Post a Comment

Popular Posts

Working with Python – Map, Filter and Reduce Function

Working with Python – Module

Copying Multiple File from Blob Storage to Single SQL Table – Part-2