UNIT III
CONTROL FLOW, FUNCTIONS
Nested conditionals
• One conditional can also be nested within another. Any
number of condition can be nested inside one another.
• In this, if the condition is true it checks another if
condition1.
• If both the conditions are true statement1 get executed
otherwise statement2 get execute. if the condition is
false statement3 gets executed.
Syntax:
If (condition) :
if (condition 1) :
statement 1
else:
statement 2
Else:
statement 3
Flowchart:
Example:
1. greatest of three numbers
2. positive negative or zero
greatest of three numbers Output
a=eval(input(“enter the value of a”))
b=eval(input(“enter the value of b”))
c=eval(input(“enter the value of c”))
if(a>b):
if(a>c):
print(“the greatest no is”,a)
else:
print(“the greatest no is”,c)
else:
if(b>c):
print(“the greatest no is”,b)
else:
print(“the greatest no is”,c)
enter the value of a 9
enter the value of a 1
enter the value of a 8
the greatest no is 9
positive negative or zero Output
n=eval(input("enter the value of n:"))
if(n==0):
print("the number is zero")
else:
if(n>0):
print("the number is positive")
else:
print("the number is negative")
enter the value of n:-9
the number is negative
ITERATION/CONTROL STATEMENTS:
 State
 while
 for
 break
 continue
 pass
State:
• Transition from one process
to another process under
specified condition with in a
time is called state.
While loop:
 While loop statement in Python is used to
repeatedly executes set of statement as long
as a given condition is true.
 In while loop, test expression is checked
first. The body of the loop is entered only if
the test_expression is True.
 After one iteration, the test expression is checked
again. This process continues until the
test_expression evaluates to False.
 In Python, the body of the while loop is
determined through indentation.
 The statements inside the while starts with
indentation and the first unindented line marks
the end.
Syntax:
initial value
while (condition) :
body of while loop
increment
Flowchart:
Example:
1. program to find sum of n numbers:
2. program to find factorial of a number
3. program to find sum of digits of a number:
4. Program to Reverse the given number:
5. Program to find number is Armstrong number or not
6. Program to check the number is palindrome or not
Sum of n numbers: output
n=eval(input("enter n"))
i=1
sum=0 while(i<=n): sum=sum+i
i=i+1
print(sum)
enter n
10
55
Factorial of a numbers: output
n=eval(input("enter n"))
i=1 fact=1 while(i<=n):
fact=fact*i
i=i+1
print(fact)
enter n
5
120
Sum of digits of a number: output
n=eval(input("enter a number"))
sum=0
while(n>0):
a=n%10
sum=sum+a
n=n//10
print(sum)
enter a number
123
6
Reverse the given number: output
n=eval(input("enter a number"))
sum=0
while(n>0):
a=n%10
sum=sum*10+a
n=n//10
print(sum)
enter a number
123
321
Armstrong number or not output
n=eval(input("enter a number"))
org=n
sum=0
while(n>0):
a=n%10
sum=sum+a*a*a
n=n//10
if(sum==org):
print("The given number is Armstrong
number")
else:
print("The given number is not Armstrong
number")
enter a number153
The given number is
Armstrong number
Palindrome or not output
n=eval(input("enter a number"))
org=n
sum=0
while(n>0):
a=n%10
sum=sum*10+a
n=n//10
if(sum==org):
print("The given no is palindrome")
else:
print("The given no is not palindrome")
enter a number121
The given no is
palindrome
For loop:
For in range:
 We can generate a sequence of numbers using
range() function. range(10) will generate
numbers from 0 to 9 (10 numbers).
 In range function have to define the start, stop
and step size as range(start,stop,step size). step
size defaults to 1 if not provided.
Syntax:
for i in range(start, stop, steps):
body of for loop
Flowchart:
For in sequence
 The for loop in Python is used to iterate over a sequence
(list, tuple, string). Iterating over a sequence is called
traversal. Loop continues until we reach the last element in
the sequence.
 The body of for loop is separated from the rest of the code
using indentation.
for i in sequence:
print(i)
Sequence can be a list, strings or tuples
S.No Sequences Example Output
1. For loop in string for i in "Ramu":
print(i)
R
A
M
U
2. For loop in list for i in [2,3,5,6,9]:
print(i)
2
3
5
6
9
3. For loop in tuple
for i in (2,3,1):
print(i)
2
3
1
Example:
1. Print nos divisible by 5 not by 10:
2. Program to print fibonacci series.
3. Program to find factors of a given number
4. Check the given number is perfect number or not
5. Check the no is prime or not
6. Print first n prime numbers
7. Program to print prime numbers in range
print nos divisible by 5 not by 10 Output
n=eval(input("enter a")) for i
in range(1,n,1): if(i%5==0
and i%10!=0):
print(i)
enter a:30
5
15
25
Fibonacci series Output
a=0 b=1
n=eval(input("Enter the number of
terms: "))
print("Fibonacci Series: ")
print(a,b) for i in
range(1,n,1):
c=a+b print(c)
a=b b=c
Enter the number of terms: 6
Fibonacci Series:
0 1
1
2
3
5
8
Find factors of a number Output
n=eval(input("enter a number:"))
for i in range(1,n+1,1):
if(n%i==0):
print(i)
enter a
number:10
1
2
5
10
Check the no is prime or not Output
n=eval(input("enter a number"))
for i in range(2,n):
if(n%i==0):
print("The num is not a prime")
break
else:
print("The num is a prime number.")
enter a no:7
The num is a
prime
number.

Python notes for students to learn and develop

  • 1.
  • 2.
    Nested conditionals • Oneconditional can also be nested within another. Any number of condition can be nested inside one another. • In this, if the condition is true it checks another if condition1. • If both the conditions are true statement1 get executed otherwise statement2 get execute. if the condition is false statement3 gets executed.
  • 3.
    Syntax: If (condition) : if(condition 1) : statement 1 else: statement 2 Else: statement 3
  • 4.
    Flowchart: Example: 1. greatest ofthree numbers 2. positive negative or zero
  • 5.
    greatest of threenumbers Output a=eval(input(“enter the value of a”)) b=eval(input(“enter the value of b”)) c=eval(input(“enter the value of c”)) if(a>b): if(a>c): print(“the greatest no is”,a) else: print(“the greatest no is”,c) else: if(b>c): print(“the greatest no is”,b) else: print(“the greatest no is”,c) enter the value of a 9 enter the value of a 1 enter the value of a 8 the greatest no is 9
  • 6.
    positive negative orzero Output n=eval(input("enter the value of n:")) if(n==0): print("the number is zero") else: if(n>0): print("the number is positive") else: print("the number is negative") enter the value of n:-9 the number is negative
  • 7.
    ITERATION/CONTROL STATEMENTS:  State while  for  break  continue  pass
  • 8.
    State: • Transition fromone process to another process under specified condition with in a time is called state.
  • 9.
    While loop:  Whileloop statement in Python is used to repeatedly executes set of statement as long as a given condition is true.  In while loop, test expression is checked first. The body of the loop is entered only if the test_expression is True.
  • 10.
     After oneiteration, the test expression is checked again. This process continues until the test_expression evaluates to False.  In Python, the body of the while loop is determined through indentation.  The statements inside the while starts with indentation and the first unindented line marks the end.
  • 11.
    Syntax: initial value while (condition): body of while loop increment
  • 12.
  • 13.
    Example: 1. program tofind sum of n numbers: 2. program to find factorial of a number 3. program to find sum of digits of a number: 4. Program to Reverse the given number: 5. Program to find number is Armstrong number or not 6. Program to check the number is palindrome or not
  • 14.
    Sum of nnumbers: output n=eval(input("enter n")) i=1 sum=0 while(i<=n): sum=sum+i i=i+1 print(sum) enter n 10 55 Factorial of a numbers: output n=eval(input("enter n")) i=1 fact=1 while(i<=n): fact=fact*i i=i+1 print(fact) enter n 5 120
  • 15.
    Sum of digitsof a number: output n=eval(input("enter a number")) sum=0 while(n>0): a=n%10 sum=sum+a n=n//10 print(sum) enter a number 123 6
  • 16.
    Reverse the givennumber: output n=eval(input("enter a number")) sum=0 while(n>0): a=n%10 sum=sum*10+a n=n//10 print(sum) enter a number 123 321
  • 17.
    Armstrong number ornot output n=eval(input("enter a number")) org=n sum=0 while(n>0): a=n%10 sum=sum+a*a*a n=n//10 if(sum==org): print("The given number is Armstrong number") else: print("The given number is not Armstrong number") enter a number153 The given number is Armstrong number
  • 18.
    Palindrome or notoutput n=eval(input("enter a number")) org=n sum=0 while(n>0): a=n%10 sum=sum*10+a n=n//10 if(sum==org): print("The given no is palindrome") else: print("The given no is not palindrome") enter a number121 The given no is palindrome
  • 19.
    For loop: For inrange:  We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9 (10 numbers).  In range function have to define the start, stop and step size as range(start,stop,step size). step size defaults to 1 if not provided.
  • 20.
    Syntax: for i inrange(start, stop, steps): body of for loop Flowchart:
  • 21.
    For in sequence The for loop in Python is used to iterate over a sequence (list, tuple, string). Iterating over a sequence is called traversal. Loop continues until we reach the last element in the sequence.  The body of for loop is separated from the rest of the code using indentation. for i in sequence: print(i)
  • 22.
    Sequence can bea list, strings or tuples S.No Sequences Example Output 1. For loop in string for i in "Ramu": print(i) R A M U 2. For loop in list for i in [2,3,5,6,9]: print(i) 2 3 5 6 9 3. For loop in tuple for i in (2,3,1): print(i) 2 3 1
  • 23.
    Example: 1. Print nosdivisible by 5 not by 10: 2. Program to print fibonacci series. 3. Program to find factors of a given number 4. Check the given number is perfect number or not 5. Check the no is prime or not 6. Print first n prime numbers 7. Program to print prime numbers in range
  • 24.
    print nos divisibleby 5 not by 10 Output n=eval(input("enter a")) for i in range(1,n,1): if(i%5==0 and i%10!=0): print(i) enter a:30 5 15 25 Fibonacci series Output a=0 b=1 n=eval(input("Enter the number of terms: ")) print("Fibonacci Series: ") print(a,b) for i in range(1,n,1): c=a+b print(c) a=b b=c Enter the number of terms: 6 Fibonacci Series: 0 1 1 2 3 5 8
  • 25.
    Find factors ofa number Output n=eval(input("enter a number:")) for i in range(1,n+1,1): if(n%i==0): print(i) enter a number:10 1 2 5 10 Check the no is prime or not Output n=eval(input("enter a number")) for i in range(2,n): if(n%i==0): print("The num is not a prime") break else: print("The num is a prime number.") enter a no:7 The num is a prime number.