





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 9
This page cannot be seen from the preview
Don't miss anything!






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]
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
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()