SlideShare a Scribd company logo
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.

More Related Content

PPTX
Looping Statements and Control Statements in Python
PDF
2 Python Basics II meeting 2 tunghai university pdf
PDF
GE3151_PSPP_UNIT_3_Notes
PPTX
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
PDF
loopingstatementinpython-210628184047 (1).pdf
PPTX
Looping statement in python
PPT
Python Programming Introduction - Loops & Boolean
PDF
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Looping Statements and Control Statements in Python
2 Python Basics II meeting 2 tunghai university pdf
GE3151_PSPP_UNIT_3_Notes
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
loopingstatementinpython-210628184047 (1).pdf
Looping statement in python
Python Programming Introduction - Loops & Boolean
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...

Similar to Python notes for students to learn and develop (20)

PPTX
Loops in Python
PDF
Programming fundamental 02
DOC
Slide07 repetitions
PDF
Python_Module_2.pdf
PPTX
Loops in c language
PPTX
Loops in c language
PPTX
1. control structures in the python.pptx
PPT
Python Control structures
PPTX
Control structure of c
PDF
loops.pdf
PPTX
loopin gstatement in python using .pptx
PPTX
Chapter 2-Python and control flow statement.pptx
PPTX
Control Structures in C
PDF
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
PPTX
FLOW OF CONTROL-NESTED IFS IN PYTHON
PPTX
Conditional and control statement
PDF
algorithm
PPTX
Chapter08.pptx
PDF
basic of desicion control statement in python
Loops in Python
Programming fundamental 02
Slide07 repetitions
Python_Module_2.pdf
Loops in c language
Loops in c language
1. control structures in the python.pptx
Python Control structures
Control structure of c
loops.pdf
loopin gstatement in python using .pptx
Chapter 2-Python and control flow statement.pptx
Control Structures in C
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
FLOW OF CONTROL-NESTED IFS IN PYTHON
Conditional and control statement
algorithm
Chapter08.pptx
basic of desicion control statement in python
Ad

Recently uploaded (20)

PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Classroom Observation Tools for Teachers
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Basic Mud Logging Guide for educational purpose
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Structure & Organelles in detailed.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
VCE English Exam - Section C Student Revision Booklet
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Final Presentation General Medicine 03-08-2024.pptx
Classroom Observation Tools for Teachers
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
RMMM.pdf make it easy to upload and study
Microbial disease of the cardiovascular and lymphatic systems
Module 4: Burden of Disease Tutorial Slides S2 2025
O7-L3 Supply Chain Operations - ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPH.pptx obstetrics and gynecology in nursing
Complications of Minimal Access Surgery at WLH
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Basic Mud Logging Guide for educational purpose
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Structure & Organelles in detailed.
Ad

Python notes for students to learn and develop

  • 2. 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.
  • 3. Syntax: If (condition) : if (condition 1) : statement 1 else: statement 2 Else: statement 3
  • 4. Flowchart: Example: 1. greatest of three numbers 2. positive negative or zero
  • 5. 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
  • 6. 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
  • 7. ITERATION/CONTROL STATEMENTS:  State  while  for  break  continue  pass
  • 8. State: • Transition from one process to another process under specified condition with in a time is called state.
  • 9. 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.
  • 10.  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.
  • 11. Syntax: initial value while (condition) : body of while loop increment
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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.
  • 20. Syntax: for i in range(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 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
  • 23. 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
  • 24. 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
  • 25. 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.