0% found this document useful (0 votes)
7 views6 pages

Python Control Flow and Loops Examples

The document provides a series of Python programs demonstrating control flow and conditions using if-else constructs and for loops. It includes examples for performing arithmetic operations, finding the greater of two numbers, determining if a number is odd or even, summing a list of numbers, printing list elements, and checking if a number is negative, positive, or zero. Each program includes code snippets and explanations for user input and output results.

Uploaded by

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

Python Control Flow and Loops Examples

The document provides a series of Python programs demonstrating control flow and conditions using if-else constructs and for loops. It includes examples for performing arithmetic operations, finding the greater of two numbers, determining if a number is odd or even, summing a list of numbers, printing list elements, and checking if a number is negative, positive, or zero. Each program includes code snippets and explanations for user input and output results.

Uploaded by

p1865.bilha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 5 – Flow of Control and Conditions

PYTHON PROGRAMS – IF-Else constructs and for loop constructs

1. Write a Python program which performs arithmetic operations on two


numbers. The two numbers and the arithmetic operation will be input by the
user.
Program Code:

#program to perform arithmetic operations of your choice

# enter the numbers of your choice

varA = int(input("Enter the first number : "))

varB = int(input("Enter the second number : "))

varOperator = input("Enter the operation you wish to perform


(Add/Sub/Mul/Div)")

varResult = 0

# calculations

if (varOperator == 'Add'):

varResult = varA+varB

elif (varOperator == 'Sub'):

varResult = varA-varB

elif (varOperator == 'Mul'):

varResult = varA * varB

elif (varOperator == 'Div'):

varResult = varA/varB

else:

Page 1 of 6
print("Please enter a valid operation")

print("The result of the operation is ", varResult)

2. Write a Python program which finds out greater of 2 numbers and prints out
the number. The two numbers will be input by the user.
Program Code:

# to find the greater of 2 numbers :

# get the numbers from the user:

varA = int(input("Enter the first number : "))

varB = int(input("Enter the second number :"))

# if -else condition to check

if varA > varB:

print( varA," is greater than ",varB)

elif varB > varA:

print( varB," is greater than ",varA)

else:

print( varA," is equal to ", varB)

3. Write a Python program which finds out if a given number is ODD or EVEN. The
number will be input by the user.
Program Code:

# program to find if a number is odd or even

Page 2 of 6
# enter a number of your choice

varA = int(input("Enter a number of your choice : "))

if varA%2!= 0:

print(varA, "is ODD")

else:

print(varA, "is EVEN")

4. Write a Python program which finds out the sum of the numbers stored in a
list using a for loop and prints out the sum.
Program Code:

# program to add numbers given in a list

# intialise the list

aList = [3, 67, 9,0.08,33, 7, 12]

# initialise the variable to hold the sum

varSum = 0

# add numbers using a for loop

for i in aList:

varSum = varSum+i

print(aList)

print("The sum of the values in the list is : ", varSum)

Page 3 of 6
5. Write a Python program which finds prints out each element stored in a list
using a for loop.
Program Code:

# program to print the items given in a list

# intialise the list

aList = ['apples','cabbage','beans','potatoes', 'onions', 'brinjal']

# print the values in the for loop

for i in aList:

print (i)

6. Write a Python program which performs arithmetic operations on two


numbers. The two numbers and the arithmetic operation will be input by the
user. Use a nested IF- else construct for additional check.
Program Code:

# calculator

# input numbers

varA = float(input("Enter a 2 - digit number of your choice : "))

varB = float(input("Enter a 2 - digit number of your choice : "))

# var to get the operator of your choice

varOperator = input("Enter the operation of your choice (+ , - , *, / , e)")

varResult = 0 # intialise the varResult

if varOperator == '+':

varResult = varA+varB

Page 4 of 6
print("Result of ", varA, varOperator , varB, " = ", varResult)

elif varOperator == '-':

if varA > varB:

varResult = varA - varB

print("Result of ", varA, varOperator , varB, " = ", varResult)

else:

varResult = varB - varA

print("Result of ", varB, varOperator , varA, " = ", varResult)

elif varOperator == '*':

varResult = varA * varB

print("Result of ", varA, varOperator , varB, " = ", varResult)

elif varOperator == '/':

if varA > varB:

varResult = varA / varB

print("Result of ", varA, varOperator , varB, " = ", varResult)

else:

varResult = varB / varA

7. Write a Python program to find if a number if negative, positive or zero using


nested if-else.

Program Code:

varNum = float(input("Enter a number :"))

Page 5 of 6
if varNum >= 0:

if varNum == 0:

print("Zero")

else:

print("Postive Number")

else:

print("Negative Number")

Page 6 of 6

You might also like