Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

PYTHON Notes - Control flow,loop statements

The document provides an overview of control flow statements in Python, including if, if-else, and nested if statements, along with examples for each. It also discusses loop statements such as while and for loops, detailing their syntax and providing sample programs. Additionally, it covers the use of short-hand if statements and the importance of loops in simplifying code execution.

Uploaded by

rahulvenkat.465
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PYTHON Notes - Control flow,loop statements

The document provides an overview of control flow statements in Python, including if, if-else, and nested if statements, along with examples for each. It also discusses loop statements such as while and for loops, detailing their syntax and providing sample programs. Additionally, it covers the use of short-hand if statements and the importance of loops in simplifying code execution.

Uploaded by

rahulvenkat.465
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Python

Control Flow Statements

Condition Description
if An if statement consists of a boolean expression followed by one or
more statements.
if-else / else-if An if statement can be followed by an optional else statement, which
executes when the boolean expression is FALSE.
nested – if You can use one if or else if statement inside another if or else
if statement(s).

If - flow chart

Example:
a = 10
b = 15
if a<b:
print("A is small number")

Note : When the condition is True program statements prints other wise no output.

If – else flow chart

Examples

check whether it is a square or rectangle


Program

length=int(input("enter a length "))


breadth=int(input("enter a breadth "))
if length==breadth:
print("It is a Sqaure")
else:
print("It is a Rectangle")

Find the eligibility of an employee for Gratuity, if employee has the eligibility provide the 10% of his
salary?
Program
yos=int(input("enter a years of service"))
if yos>5:
salary = int(input("enter a salary"))
print("Gratutity is :",salary*0.10)
else:
print("No Gratutity")

Else – If

Example
Find the student grades based on their marks.
Marks 0 to 35 → Fail
Marks 35 to 60 → Grade C
Marks 60 to 75 → Grade B
Marks 75 to 100 → Grade A

Example
Program
marks=int(input("Enter Marks "))
if marks >= 0 and marks <35:
print("Fail")
elif marks >= 35 and marks <60:
print("Grade C")
elif marks >= 60 and marks < 75:
print("Grade B")
elif marks >=75 and marks <=100:
print("Grade A")
else:
print("Invalid")
Example
Generate the Electricity bill for different slabs ?
Slab 1 → 0 – 100 units → Minimum charge is 200
Slab 2 → 100 – 200 units → Per unit is 5/-
Slab 3 → 200 – 300 units → per unit is 10/-
Slab 4 → 300 and above → per unit is 15/-

Example
Electricity Bill Generation
Program
num=int(input("Enter number of electric unit"))
if num<=100:
amt=200
if num>100 and num<=200:
amt=200+(num-100)*5
if num>=200 and num<=300:
amt=700+(num-200)*10
if num>300:
amt = 1700 + (num - 300) * 15
print("Amount to pay :",amt)

Nested – If flow chart


Short Hand if and if-else
Short Hand If
If you have only one statement to execute, you can put it on the same line as the if statement.
Example
if a > b: print("a is greater than b")
Short Hand If ... Else
If you have only one statement to execute, one for if, and one for else, you can put it all on the
same line.
a=2
b=4
print(“A") if a > b else print(“B")
Loops Statements

Loop
The flow of the programs written in any programming language is sequential by default.
Sometimes we may need to alter the flow of the program. The execution of a specific code
may need to be repeated several numbers of times.
Why we use loops in python?
The looping simplifies the complex problems into the easy ones.
It enables us to alter the flow of the program so that instead of writing the same code again
and again, we can repeat the same code for a finite number of times

While loop
❑ While loop is used to iterate over a block of code repeatedly until a given condition
returns false.
❑ The main difference is that we use while loop when we are not certain of the number
of times the loop requires execution.
❑ On the other hand when we exactly know how many times we need to run the loop,
we use for loop.
Syntax
while condition:
#body_of_while
Flow of while loop
❑ First the given condition is checked, if the condition returns false, the loop is terminated
and the control jumps to the next statement in the program after the loop.
❑ If the condition returns true, the set of statements inside loop are executed and then
the control jumps to the beginning of the loop for next iteration.
These two steps happen repeatedly as long as the condition specified in while loop remains
true.
While loop

Program
Program 1:
n=int(input("Enter a Number : "))
while n<15:
print(n)
n+=1

Program 2: Using Break and If


n=int(input("Enter a Number : "))
while n<15:
print(n)
if n==10:
break
n+=1

Program 3: Using Continue and If-Else


n=int(input("Enter a Number : "))
while n<15:
n+=1
if n==10:
continue
print(n)
else:
print("Number Should be less than 15")
Write a Program to display Fibonacci sequence
n = int(input("How many terms? "))
n1, n2 = 0, 1
count = 0
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence upto",n,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < n:
print(n1)
sum = n1 + n2
n1 = n2
n2 = sum
count += 1

For loop flow chart


For loop

❑ The for loop is used in the case where we need to execute some part of the code until
the given condition is satisfied.
❑ The for loop is also called as a per-tested loop. It is better to use for loop if the number
of iteration is known in advance.
❑ The for loop in Python is used to iterate the statements or a part of the program several
times. It is frequently used to traverse the data structures like list, tuple, or dictionary.

Syntax :
for iterating variable in iterating sequence:
statement(s)

Examples
Program to find sum all numbers in the list?
List is [1,2,3,4,5]
Program:
numbers = [1,2,3,4,5]
sum = 0
for val in numbers:
sum = sum + val
print("The sum is", sum)

Write a program to check given number is a “Prime Number” or not ?


Program
num = int(input("Enter a Number : "))
for i in range(2,num):
if num % i == 0:
print("Number",num,"is not a prime number")
break
else:
print("Number",num,"is a prime number")
Write a program for Multiplication Table?
Program
n=int(input("Enter the number"))
for i in range(1,11):
c=n*i
print(n,"*",i,"=",c)

Write a program to display students marks ?


Program
student_name = input("Enter student Name ")
marks = {'Raju': 70, 'Krishna': 80, 'Arjun': 90, 'Vamsi': 60}
for student in marks:
if student == student_name:
print(marks[student])
break
else:
print('No entry with that name found.')

You might also like