Python-04 (Control Flow Tool)
Python-04 (Control Flow Tool)
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]
#.........................................Example-01.........................
for w in words:
print(w, '=>word length=',len(w))
#.........................................Example-02........................
# function range() generates arithmetic progressions
#.........................................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
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 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.