Python- Control Statements

#Control Statements

if  :,else :, elif :
Switch: Not available in python

while  :
foreach =>for name in names: names Heterogeneous
For C-Style:  for item in range(0,10,2):# 2 is step here



#For Linux users

vim if.py. chmod a+x

#!/usr/bin/python


#Conditional Example

#!/usr/bin/Python
i=1

if i==1:
print "1.Line\n"

elif i==2:
    print "2.Line\n"

elif i==3:
    print "3.Line\n"
elif i==4:
    print "4.Line\n"
elif i==5:
    print "5.Line\n"

#if-elif-else

i=10

if i==1:
    print "1.Line\n"
elif i==2:
    print "2.Line\n"

elif i==3:
    print "3.Line\n"
elif i==4:
    print "4.Line\n"
elif i==5:
    print "5.Line\n"
else:
    print "No.Line\n"


Name=raw_input(“what is your name”)
#i=int(raw_input("Enter value of i")) # To convert in integer
Print 'your name is ‘+name
If name ==”aashish”: # remember indentation required in place of blocks can use 8 space or tab
       Print “hello”+ name
       Print” you are administration”
elIf name ==”amit”:
       Print “hello”+ name
       Print” you are user”
else:
       Print”unknown user”

# Nested if
username=(raw_input("Enter username"));
password=(raw_input("Enter Password"));
if username=="aashishmittal":
if password=="1234":
print"Welcome"
else:
print"Password not corrcet"
else:
print"Username not correct"


#While To print 1 to 10
i=1
while i<=10:
    print i,".Line\n"
    i=i+1


# Foreach loop

values=['a',1,3.5,"aashish"]

for value in values:
    print value



#C style for loop have to use range

for item in range(0,10,2):# 2 is step here
      Print item


Reactions: