Jul 2, 2018

Learning control flow statements in Python

#!/usr/local/bin/python2.7

'''
    #Learning control flow statements in Python
    File name: python_control_flow_statements.py
    Author: Prabhath Kota
    Date: June 29, 2018
    Python Version: 2.7
'''

'''
Sequential
Selection (Python Conditional Statements) 
if
if...else
if..elif..else statements
nested if statements
not operator in if statement
and operator in if statement
in operator in if statement
Repetetion
for loop
while loop
'''

print('-------------------Python if statements----------------')
x=20
y=10
if x > y :
  print("X is bigger")
#X is bigger

print('-------------------Python if...else statements----------------')
x=10
y=20
if x > y :
  print("X is bigger")
else :
  print("Y is bigger")
#Y is bigger

print('-------------------Python if...else...else statements----------------') 
x=500
if x > 500 :
  print("X is greater than 500")
elif x < 500 :
  print("X is less than 500")
elif x == 500 :
  print("X is 500")
else :
  print("X is not a number")
#X is 500

print('-------------------Python Nested if statements----------------') 
mark = 72
if mark > 50:
  if mark >= 80:
    print ("You got A Grade !!")
  elif mark >= 60 and mark < 80 :
    print ("You got B Grade !!")
  else:
    print ("You got C Grade !!")
else:
  print("You failed!!")
#You got B Grade !!

print('-------------------not operator in if statement----------------') 
mark = 100
if mark != 100: #mark != 100
  print("mark is not 100")
else:
  print("mark is 100")
#mark is 100

print('-------------------and operator in if statement----------------') 
mark = 72
if mark > 80:
  print ("You got A Grade !!")
elif mark >= 60 and mark < 80 :
  print ("You got B Grade !!")
elif mark >= 50 and mark < 60 :
  print ("You got C Grade !!")
else:
  print("You failed!!")
#You got B Grade !!

print('-------------------in operator in if statement----------------') 
color = ['Red','Blue','Green']
selColor = "Red"
if selColor in color:
  print("Red is in the list")
else:
  print("Not in the list")
#Red is in the list4

print '----------------For loop---------------------'
freedom_fighters = ["Lala Lajipati Rai", "Dadabhai Naoroji", "Rajendra Prasad", "Sarojini Naidu", "Dadabhai Naoroji", "Lal Bahadur Shastri"]
print freedom_fighters
for each in freedom_fighters:
print each

#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
#Lala Lajipati Rai
#Dadabhai Naoroji
#Rajendra Prasad
#Sarojini Naidu
#Dadabhai Naoroji
#Lal Bahadur Shastri

print '----------------While loop---------------'
#Try to avoild While loops, use only unless otherwise reequired
#If you sometimes don't handle it, it may end up in infinite loop
i = 0
squares = []
while(i < 10):
squares.append(i**2)
i += 1
print squares
#[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

No comments:

Post a Comment