Python Lab Assignment on Control Structures, Assignments of Computer Science

A Python lab assignment on control structures. It includes programs to print even and odd numbers, display product of digits, check if a number is even or odd, calculate age, identify triangle type, print numbers greater than 50, calculate salary, and perform mathematical operations. The programs use control structures like loops, conditionals, and list comprehension. sample inputs and outputs for each program.

Typology: Assignments

2021/2022

Available from 11/30/2022

adarsh-kashyap-1
adarsh-kashyap-1 🇮🇳

6 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab Assignment
(Control Structure)
1. Write a program to print first 10 even numbers
print([2*i for i in range(1,11)])
#output:- [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
2. Write a program to print first 10 odd numbers
print([2*i+1 for i in range(10)])
#output:- [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
3. Write a program to print first 10 even numbers in reverse order
print([2*i for i in range(10,0,-1)])
#output:- [20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
4. Write a program to display product of the digits of a number accepted from
the user.
n=int(input("enter the number:-"))
prod=1
while(n):
rem=n%10
prod=prod*rem
n=n//10
print(f'product is {prod}')
#output:- enter the number:-3313
product is 27
5. Check the input number is even or odd.
n=int(input("enter the number:--"))
print("even") if (n%2==0) else print("odd")
#output:- enter the number:--33
odd
6. Write a program to calculate a person age by taking all the parameters as
user input like current date, month, year and birth date, month and year.
Date:- 04/07/22
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Python Lab Assignment on Control Structures and more Assignments Computer Science in PDF only on Docsity!

Lab Assignment (Control Structure)

1. Write a program to print first 10 even numbers print([2i for i in range(1,11)]) #output:- [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] 2. Write a program to print first 10 odd numbers print([2i+1 for i in range(10)]) #output:- [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] 3. Write a program to print first 10 even numbers in reverse order print([2*i for i in range(10,0,-1)]) #output:- **[20, 18, 16, 14, 12, 10, 8, 6, 4, 2]

  1. Write a program to display product of the digits of a number accepted from** the user. n=int(input("enter the number:-")) prod= while(n): rem=n% prod=prod*rem n=n// print(f'product is {prod}') #output:- enter the number:- 3313 product is 27 5. Check the input number is even or odd. n=int(input("enter the number:--")) print("even") if (n%2==0) else print("odd") #output:- enter the number:-- 33 odd 6. Write a program to calculate a person age by taking all the parameters as user input like current date, month, year and birth date, month and year.

bd,bm,by=[int(x) for x in input("enter date of birth-in dd/mm/yyyy format").split("/")] cd,cm,cy=[int(x) for x in input("enter current date-in dd/mm/yyyy format").split("/")] if(cd>=bd): days=cd-bd else: if cm in [1,3,5,7,8,10,12]: cd=cd+ days= cd-bd elif cm==2: if (cy%4==0): if(cy%100==0): if(cy%400==0): cd=cd+ else: cd=cd+ else: cd=cd+ else: cd=cd+ cm-= if(cm>=bm): months = cm-bm else: cy=cy- 1 cm=cm+ months= cm-bm if(cy>=by): years=cy-by print(f"you are {years} years {months} months and {days} days old") else: print("invalid date of birth") #output enter date of birth-in dd/mm/yyyy format:-15/08/ enter current date-in dd/mm/yyyy format:-17/08/ you are 0 years 0 months and 2 days old

33X9=

33X10=

10. Write a program to whether a number (accepted from user) is divisible by 2 and 3 both. n=int(input("enter the number:-")) print('not div') if(n%6) else print("div") output:- enter the number:- 33 not div 11. Write a program to accept a number from 1 to 7 and display the name of the day like 1 for Sunday, 2 for Monday and so on. n=int(input("enter the number between 1 and7 both included:- ")) if(n==1): print("Sunday") elif(n==2): print("Monday") elif(n==3): print("Tuesday") elif(n==4): print("Wednesday") elif(n==5): print("Thursday") elif(n==6): print("Friday") elif(n==7): print("Saturday") else: print("please enter between 1 to 7") output:- enter the number between 1 and7 both included:- 7 Saturday 12. Accept three sides of a triangle and check whether it is an equilateral,

isosceles or scalene triangle. s1=float(input("enter 1st side")) s2=float(input("enter 2nd side")) s3=float(input("enter 3rd side")) if(s1==s2==s3): print("equilateral") elif(s1!=s2 and s2!=s3 and s3!=s1): print("scalen") else: print("isoscalen") output:- enter 1st side:-33. enter 2nd side:-33. enter 3rd side:-33. equilateral

13. Print the number from 0 to 100 which are greater than 50 in both traditional method and list comprehension methods. print("printing by traditional method") for i in range(101): if i>50: print(i,end=",") print("\n printing by list comprehension method") l2=[i for i in range(101) if i>50 ] print(l2) output:- printing by traditional method 51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70, 71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90, 91,92,93,94,95,96,97,98,99,100, printing by list comprehension method [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

16. Write a program to accept two numbers and mathematical operators and perform operation accordingly. Like: Enter First Number: 7 Enter Second Number : 9 Enter operator : + Your Answer is : 16 num_1=int(input("Enter First Number: ")) num_2=int(input("Enter Second Number :")) op=str(input("Enter operator ----[ + - * / % ] : ")) if op=="+": result=num_1+num_ print("Your Answer is :",result) elif op=="-": result=num_1-num_ print("Your Answer is :",result) elif op=="": result=num_1num_ print("Your Answer is :",result) elif op=="/": result=num_1/num_ print("Your Answer is :",result) elif op=="%": result=num_1%num_ print("Your Answer is :",result) else: print("=====Dude, Enter a Valid Operator! =====") #output:- Enter First Number: 34 Enter Second Number : Enter operator ----[ + - * / % ] : + Your Answer is : 123 17. Write a program to print a diamond pattern.

n= for i in range(1,n+1): for j in range(1,2n): if(j>=n-i+1 and j<=n+i-1): print(" ",end=" ") else: print(" ",end=" ") print() for i in range(1,n): for j in range(1,2n): if(j>=i+1 and j<=2n-i-1): print("* ",end=" ") else: print(" ",end=" ") print() output:-








18. Write a program to print a hill pattern. n= for i in range(1,n+1): for j in range(1,2n): if(j>=n-i+1 and j<=n+i-1): print(" ",end=" ") else: print(" ",end=" ") print()