Control Flow Tool
By: Dr. Vipin Kumar
if-elif-else Statements
x=int(input("Please enter an integer= "))
if x<0:
print("Entered value is Negative")
if x>100:
print("and more than -100")
else:
print("and less than or equal to -100")
elif x>0:
print("Entered value is Positive")
if x>100:
print("and more than 100")
else:
print("and less than or equal to 100")
else:
print("Entered value is Zero")
print(" Program ends here...thank you...!!!")
for Statements
• for statement iterates over the items of any sequence (a list or a string) in the order
that they appear in the sequence.
• range() behaves as if it is a list, but in fact it isn’t.
•It is an object which returns the successive items, but it doesn’t
really make the list thus saving the space
•The function list() creates lists from the range() function
>>> list(range(6))
[0, 1, 2, 3, 4, 5]
words = ['Vipin', 'Sushant', 'Jayant', "Abhi"]
print(words)
#.........................................Example-01.........................
for w in words:
print(w, '=>word length=',len(w))
#.........................................Example-02........................
# function range() generates arithmetic progressions
print("\nThis is another example..!")
for i in range(2,10):
print(i)
#.........................................Example-03.......................
# to specify a different increment (even negative)
print("\nThis is another example..!")
for i in range(0, 10, 3):
print(i)
break and continue Statements
#Find the first integer from the list
which is odd
X=[2,4,6,10,6,2,3,5,7]
for n in X:
if n%2==1:
print("Odd value=",n)
break
else:
print("Got even
value=",n,"\n")
print("Program terminate here...!!!")
e n t
t at em
ak S
Bre
break and continue Statements
#Find the first integer from the
list which is odd
X=[2,4,6,10,6,2,3,5,7]
for n in X:
if n%2==0:
print("Even value=",n)
continue
print("your odd value =",n)
print("Program terminate
here...!!!")
e n t
S t atem
e
tinu
con
“pass” Statements
• “pass” statement
>>> while True:
does nothing.
pass
• it is only use when
the statement #Busy-wait for keyboard
required interrupt (Ctrl+C)
syntactically, but
there is no action
needed.
>>>Class MyEmptyClass:
pass
Defining Functions
• Fibonacci series function
defining on command
window…
>>> def fib(n):
a,b=0,1
while a<n:
print(a,end=' ')
a,b=b, a+b
print()
return() in Functions
#Returning value in function
def fibonacciReturn(n):
Result=[]
a, b=0, 1
while a<n:
Result.append(a)
a,b=b, a+b
return results
•The return statement returns with a value from a function. return without an
expression argument returns None.
•Falling off the end of a function also returns None.
•The statement result.append(a) calls a method of the list object result.
Default Argument Values in Functions
def abc(a=12, b="vipin", c=[1,2,3,"a"]):
print(a)
print(b)
print(c)
Thank You…