Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Neha Py 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Name: Ankush bhakare

Roll No.: 14
Assignment No.: 01
Title: Develop programs to understand the control structures of python

Code:

1.1 Continue statement:

# Program to find out even and odd number in between given range using for loop:

for num in range(10):


if num % 2 == 0:
print(num, "is even number")
continue
print(num, "is odd number")

Output:
0 is even number
2 is even number
4 is even number
6 is even number
8 is even number

# program to print odd numbers from 1 to 10 using while loop:

num = 0
n = int(input("Enter a number in between 1 to 10: "))
if n > 10:
print("please enter a number in between 1 to 10")
else:
while num < n:
num += 1
if (num % 2) == 0:
continue
print(num)

Output:
Enter a number in between 1 to 10:3
1
3

1.2 Break Statement:

# program to find first 5 multiples of 6

i=1
n = int(input("Enter a number in between 1 to 10: "))
if n > 10:
print("please enter a number in between 1 to 10")
else:
while i <= 10:
print('6 * ', (i), '=',6 * i)
if i >= n:
break
i=i+1

Output:
Enter a number in between 1 to 10:5
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30

1.3 Pass Statement:

#Program to find out odd number in given list

num = [1, 3, 6, 33, 76, 29, 17, 60, 47, 53, 88, 10, 2, 3, 75,55]

print('Odd numbers are: ')


for i in num:
# check if the number is even
if i % 2 == 0:
# if even, then pass
pass
# print the odd numbers
else:
print (i)

Output:
Odd numbers are:
1
3
33
29
17
47
53
3
75
55

1.4 Conditional Statement (Chained if):

#program to find out Grade of student:

marks = int(input("Enter the marks: "))


if marks>100:
print("Please enter proper marks!")
elif marks > 85 and marks <= 100:
print("Congrats ! you scored grade A ...")
elif marks > 60 and marks <= 85:
print("You scored grade B + ...")
elif marks > 40 and marks <= 60:
print("You scored grade B ...")
elif (marks > 30 and marks <= 40):
print("You scored grade C ...")
else:
print("Sorry you are fail")

Output:
Enter the marks: 100
Congrats ! you scored grade A ...

1.5 Nested Loop:

#program to print Multiplication table up to given number:

n = int(input("Enter any number up to 100:"))

# Iterating over numbers in the range 1 to n


for row in range(1,n+1):
# Iterating over numbers in the range 1 to n
for col in range(1,n+1):
# Printing the product of row and col
print(row*col, end="\t")
print()

Output:
Enter any number up to 100:5
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

1.6 Nested Condition:

a = int(input("Enter 1st number: "))


b = int(input("Enter 2nd number: "))
c = int(input("Enter 3rd number: "))
if(a>b):
if(a>c):
print("a is greater")

if(b>a):
if(b>c):
print("b is greatest")

if(c>a):
if(c>b):
print("c is greatest")

if(a == b and b == c):


print("all are equal")

Output:
Enter 1st number: 50
Enter 2nd number: 90
Enter 3rd number: 70
b is greatest

You might also like