0% found this document useful (0 votes)
13 views

Python Programming For Polytechnic-28!7!24 To 3-8-24

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python Programming For Polytechnic-28!7!24 To 3-8-24

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Python Programming

Control Flow Statements


The statements inside your Python program are generally executed sequentially from top to bottom,
in the order that they appear.

Apart from sequential control flow statements you can employ decision making and looping control
flow statements to break up the flow of execution thus enabling your program to conditionally
execute particular blocks of code. The term control flow details the direction the program takes.

The control flow statements in Python Programming Language are

Sequential Control Flow Statements: This refers to the line by line execution, in which the
statements are executed sequentially, in the same order in which they appear in the program.

Decision Control Flow Statements: Depending on whether a condition is True or False, the decision
structure may skip the execution of an entire block of statements or even execute one block of
statements instead of other (if, if…else and if…elif…else).

Loop Control Flow Statements: This is a control structure that allows the execution of a block of
statements multiple times until a loop termination condition is met ( for loop and while loop). Loop
Control Flow Statements are also called Repetition statements or Iteration statements.
Program 3.1: Program Reads a Number and Prints a Message If It Is Positive
number = int(input("Enter a number"))
if number >= 0:
print(f"The number entered by the user is a positive number")
Program 3.2: Program to Read Luggage Weight and Charge the Tax Accordingly
weight = int(input("How many pounds does your suitcase weigh?"))
if weight > 50:
print(f"There is a $25 surcharge for heavy luggage")
print(f"Thank you")
The if…else Decision Control Flow Statement
An if statement can also be followed by an else statement which is optional. An else statement does
not have any condition. Statements in the if block are executed if the Boolean_Expression is True.
Use the optional else block to execute statements if the Boolean_Expression is False. The if…else
statement allows for a two-way decision.
1
Program 3.4: Program to Find the Greater of Two Numbers
number_1 = int(input("Enter the first number"))
number_2 = int(input("Enter the second number"))
if number_1 > number_2:
print(f"{number_1} is greater than {number_2}")
else:
print(f"{number_2} is greater than {number_1}")
This if…elif…else decision control statement is executed as follows:

 In the case of multiple Boolean expressions, only the first logical Boolean expression
which evaluates to True will be executed.

 If Boolean_Expression_1 is True, then statement_1 is executed.

 If Boolean_Expression_1 is False and Boolean_Expression_2 is True, then state-


ment_2 is executed.

 If Boolean_Expression_1 and Boolean_Expression_2 are False and Boolean_


Expression_3 is True, then statement_3 is executed and so on.

 If none of the Boolean_Expression is True, then statement_last is executed.


Python Program to Display First 10 Numbers Using while Loop Starting from 0
i=0
while i < 10:
print(f"Current value of i is {i}")
i=i+1
Program to Find the Average of n Natural Numbers Where n Is the Input from the
User
number = int(input("Enter a number up to which you want to find the average"))
i=0
sum = 0
count = 0
while i < number:
i=i+1
sum = sum + i
count = count + 1
average = sum/count
print(f"The average of {number} natural numbers is {average}")
Program to Find the GCD of Two Positive Numbers
m = int(input("Enter first positive number"))
n = int(input("Enter second positive number"))
if m == 0 and n == 0:
print("Invalid Input")
if m == 0:
print(f"GCD is {n}")
if n == 0:
print(f"GCD is {m}")
while m != n:
if m > n:
m=m–n
if n > m:
n=n–m
print(f"GCD of two numbers is {m}")
Python Program to Find the Sum of Digits in a Number
number = int(input('Enter a number'))
result = 0
remainder = 0
while number != 0:
remainder = number % 10
result = result + remainder
number = int(number / 10)
print(f"The sum of all digits of given number {number} is {result}")
Write a Program to Display the Fibonacci Sequences up to nth Term Where n is Provided by the
User
nterms = int(input('How many terms?'))
current = 0
previous = 1
count = 0
next_term = 0
if nterms <= 0:
print('Please enter a positive number')
elif nterms = = 1:
print('Fibonacci sequence')
print('0')
else:
print("Fibonacci sequence")
while count < nterms:
print(next_term)
current = next_term
next_term = previous + current
previous = current
count += 1
Demonstrate for Loop Using range() Function
print("Only ''stop'' argument value specified in range function")
for i in range(3):
print(f"{i}")
---------------------
print("Both ''start'' and ''stop'' argument values specified in range function")

for i in range(2, 5):


print(f"{i}")
---------------------
print("All three arguments ''start'', ''stop'' and ''step'' specified in range function")
for i in range(1, 6, 3):
print(f"{i}")
------------------
Program to Iterate through Each Character in the String Using for Loop
for each_character in "Blue":
print(f"Iterate through character {each_character} in the string 'Blue'")
Write a Program to Find the Sum of All Odd and Even Numbers up to a Number
Specified by the User.
number = int(input("Enter a number"))
even = 0
odd = 0
for i in range(number+1):
if i % 2 == 0:
even = even + i
else:
odd = odd + i
print(f"Sum of Even numbers are {even} and Odd numbers are {odd}")
Write a Program to Find the Factorial of a Number
number = int(input('Enter a number'))
factorial = 1
if number < 0:
print("Factorial doesn't exist for negative numbers")
elif number == 0:
print('The factorial of 0 is 1')
else:
for i in range(1, number + 1):
factorial = factorial * i
print(f"The factorial of {number} is {factorial}")
The continue and break Statements
The break and continue statements provide greater control over the execution of code
in a loop. Whenever the break statement is encountered, the execution control
immediately jumps to the first instruction following the loop. To pass control to the
next iteration without exiting the loop, use the continue statement. Both continue and
break statements can be used in while and for loops.

Program to Demonstrate Infinite while Loop and break


n=0
while True:
print(f"The latest value of n is {n}")
n=n+1
One way of ending this infinite loop is to use break statement along with if condition as
shown in the following code.

n=0
while True:
print(f"The latest value of n is {n}")
n=n+1
if n > 5:
print(f"The value of n is greater than 5")
break
Write a Program to Check Whether a Number Is Prime or Not
number = int(input('Enter a number > 1: '))
prime = True
for i in range(2, number):
if number % i == 0:
prime = False
break
if prime:
print(f"{number} is a prime number")
else:
print(f"{number} is not a prime number")
Program to Demonstrate continue Statement
n = 10
while n > 0:
print(f"The current value of number is {n}")
if n == 5:
print(f"Breaking at {n}")
n = 10
continue
n=n–1

You might also like