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
GE3151_PSPP_UNIT_3_Notes
PPTX
1. control structures in the python.pptx
PDF
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
PDF
2 Python Basics II meeting 2 tunghai university pdf
PPTX
Python if_else_loop_Control_Flow_Statement
PPTX
Python for Beginners(v2)
DOCX
iterations.docx
Looping Statements and Control Statements in Python
GE3151_PSPP_UNIT_3_Notes
1. control structures in the python.pptx
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
2 Python Basics II meeting 2 tunghai university pdf
Python if_else_loop_Control_Flow_Statement
Python for Beginners(v2)
iterations.docx

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

PPTX
Mastering Python lesson 3a
PPTX
Chapter 9 Conditional and Iterative Statements.pptx
PPTX
Python programming –part 3
PDF
Slide 6_Control Structures.pdf
PPTX
While loop
PDF
Python_Module_2.pdf
PDF
ppt python notes list tuple data types ope
PPTX
Loops in Python
PPTX
python ppt.pptx
PDF
Python - Control Structures
PPT
Control structures pyhton
PPT
Py4inf 05-iterations (1)
PPT
Py4inf 05-iterations (1)
PPT
Py4inf 05-iterations
PPTX
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
PPTX
While_for_loop presententationin first year students
PDF
While-For-loop in python used in college
PPTX
PPT
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
PPT
Python Control structures
Mastering Python lesson 3a
Chapter 9 Conditional and Iterative Statements.pptx
Python programming –part 3
Slide 6_Control Structures.pdf
While loop
Python_Module_2.pdf
ppt python notes list tuple data types ope
Loops in Python
python ppt.pptx
Python - Control Structures
Control structures pyhton
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
Py4inf 05-iterations
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
While_for_loop presententationin first year students
While-For-loop in python used in college
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
Python Control structures
Ad

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
master seminar digital applications in india
PDF
Classroom Observation Tools for Teachers
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Types and Its function , kingdom of life
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
Sports Quiz easy sports quiz sports quiz
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
GDM (1) (1).pptx small presentation for students
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pharma ospi slides which help in ospi learning
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
2.FourierTransform-ShortQuestionswithAnswers.pdf
Insiders guide to clinical Medicine.pdf
Microbial diseases, their pathogenesis and prophylaxis
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
master seminar digital applications in india
Classroom Observation Tools for Teachers
Supply Chain Operations Speaking Notes -ICLT Program
Complications of Minimal Access Surgery at WLH
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Renaissance Architecture: A Journey from Faith to Humanism
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Types and Its function , kingdom of life
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Sports Quiz easy sports quiz sports quiz
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Cell Structure & Organelles in detailed.
GDM (1) (1).pptx small presentation for students
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
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.