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

Control Structures & Loops

Loops in Python

Uploaded by

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

Control Structures & Loops

Loops in Python

Uploaded by

chintal.gala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Exercise 4: Control Structures

a. Conditional Statements:
P6-1:

# indententation is used to identify the scope of control blocks


# If the number is positive, we print an appropriate message
num = int(input("Enter the first number: "))
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")
num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")

num = 2
if num > 0:
print(num, "is a positive number.")
print("yes it is indeed a positive number")
print("This too is always printed.")

num = 6
if num > 0:
print(num, "is a positive number.")
# nested ifs
if (num%2 ==0) :
print(num, "is also an even number ")
print("Yes, This too is always printed.")
print("job Done .. \n")

#==============================================================
P6-2:
# Program checks if the number is positive or negative
# And displays an appropriate message

#num=3
num = float(input("Enter a value "))

if num >= 0:
print(num ,"is Positive or Zero")
else:
print(num, "is a Negative number")
print("job Done .. \n")
#=======================================================
P6-3:
# any value which is non zero is treated as True.
# any value equal to zero is treated False
var1 = 10
# uncomment this statement below and try again
# var1 = -10
if var1:
print ("var1 - is a true expression value")
print (var1)
else:
print ("var1 - is a false expression value")
print (var1)

var2 = 0
if var2:
print ("var2 - is a true expression value")
print (var2)
else:
print ("var2 - is a false expression value")
print (var2)

print ("Job Done...!")

#============================================================
P6-4:
# determine if a value is less than 50,
# or equal to any of the values (50, 100, 150 )or not
# uses if ..elif

var = int(input("Enter value "))

if var < 200:


print ("Expression value is less than 200")
if var == 150:
print ("And it is 150")
elif var == 100:
print ("And it is 100")
elif var == 50:
print ("And it is 50")
elif var < 50:
print ("Expression value is less than 50")
else:
print ("Value is equal to or more than 200 ")
print ("Job Done .. !")
b. While loop
P6-5:
# Program to add natural
# numbers upto n
# sum = 1+2+3+...+n
# Take input from the user,
n = int(input("Enter n: "))
# initialize sum and counter
sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1 # update counter
# print the sum
print("The sum is", sum)
print("Job Done .. !")
#============================================================
P6-6:
# Program to find sum and average of input values
# initialize sum and counter
sum = 0
i = 0
n = int(input("Enter number: "))
while n >= 0 :
sum = sum + n
n = int(input("Enter number: "))
i = i+1 # update counter

# print the sum


print("The sum of {} values is {}".format(i, sum))
print("the average of the values is : ", sum/i)
#=========================================================
P6-7:
# Program to find sum and average of input values
# while .. else in use
# initialize sum and counter
sum = 0
i = 0
n = int(input("Enter number: "))
while n >= 0 :
sum = sum + n
n = int(input("Enter number: "))
i = i+1 # update counter
else:
print("User entered a negative value, quitting loop ")

# print the sum


print("The sum of {} values is {}".format(i, sum))
print("the average of the values is : ", sum/i)
print("Job Done .. !")
c. For loop
P6-8:
# An iterator for a for loop
samples=[8,7,12,15,9,4,11,20,2,8,14,0,6,17,13]
low=medium=high=excellent=0;

print("even values from samples : ")


# iterate over all sample values and check if even
for s in samples:
if (s%2==0):
print(s)
print("Frequency Distribution is : ")
# histogram
for s in samples:
if s >=0 and s<5 :
low += 1;
if s >=5 and s<10 :
medium += 1;
if s>=10 and s<15 :
high += 1;
if s>=15 :
excellent +=1;
# print the count values
print ( "Low count = " , low)
print ( "Medium count = " , medium)
print ( "High count = " , high)
print ( "Excellent count = " , excellent)
print("Job Done .. !")

#=====================================================================
P6-9:

# An iterator for a for loop


# iterate over a range of values from a list
samples=[8,7,12,15,9,4,11,20,2,8,14,0,6,17,13]

# range() gives a list of values from 0 to the specified number


for index in range(5):
print (index)

# select values from the list in a given range from .. upto


sum=0
for index in range(4,11):
print("Sample is ", samples[index])
sum += samples[index]
print ("sum of samples in range : " , sum)

print("Job Done .. !")

You might also like