You are on page 1of 5

LAB TASK 6

PROGRAMMING EXERCISES:
Question 1.
Take a sample list [2, 1, 3, 5, 4, 3, 8]
Apply del(), remove(), sort(), insert(), pop(), extend()…)

Program:
lst=[2, 1, 3, 5, 4, 3, 8]
lst.sort()
print("The sorted list is ",lst)
lst.remove(2)
print("The list after removing 2 is",lst)
lst.insert(3,6)
print("The list with '6' at index 3 is:",lst)
lst.pop(3)
print("The list after removing member of 3rd index is:",lst)
lst.extend([10,76])
print("The extended list is:",lst)

Result:
The sorted list is [1, 2, 3, 3, 4, 5, 8]
The list after removing 2 is [1, 3, 3, 4, 5, 8]
The list with '6' at index 3 is: [1, 3, 3, 6, 4, 5, 8]
The list after removing member of 3rd index is: [1, 3, 3, 4, 5, 8]
The extended list is: [1, 3, 3, 4, 5, 8, 10, 76]

Question 2
A ladder put up right against a wall will fall over unless put up at a certain angle less than
90 degrees. Given variables length and angle storing the length of the ladder and the angle that it forms
with the ground as it leans against the wall, write a Python expression involving length and angle
that computes the height reached by the ladder. Evaluate the expression for these values of length and
angle:
(a)16 feet and 75 degrees (b)20 feet and 0 degrees (c)24 feet and 45 degrees (d)24 feet and 80 degrees
Note: You will need to use the trig formula:
height = length * sin(angle)
The math module sin() function takes its input in radians. You will thus need to convert the angle given
in degrees to the angle given in radians using:
radians = π * degrees / 180
Program:
from math import *
import math
length=[]
angle=[]
for i in range(1,5):
print("Enter values for data no.",i,":")
l=int(input("Enter length in feet "))
a=int(input("Enter angle in degrees "))
ar=a*(math.pi/180)
length.append(l)
angle.append(ar)
height=[]
for x in range(len(length)):
h=length[x]*sin(angle[x])
height.append(round(h,2))
print("The results are: ")
for y in range(1,len(height)+1):
print("The height is for data no",y,"is:",height[y-1],"feets")

Result:
Enter values for data no. 1 :
Enter length in feet 16
Enter angle in degrees 75
Enter values for data no. 2 :
Enter length in feet 20
Enter angle in degrees 0
Enter values for data no. 3 :
Enter length in feet 24
Enter angle in degrees 45
Enter values for data no. 4 :
Enter length in feet 24
Enter angle in degrees 80
The results are:
The height is for data no 1 is: 15.45 feets
The height is for data no 2 is: 0.0 feets
The height is for data no 3 is: 16.97 feets
The height is for data no 4 is: 23.64 feets
Question 3.
Write the relevant Python expression or statement, involving a list of numbers lst and using list
operators and methods for these specifications:
(a)An expression that evaluates to the index of the middle element of lst
(b)An expression that evaluates to the middle element of lst
(c)A statement that sorts the list lst in descending order
(d)A statement that removes the first number of list lst and puts it at the end
Note: If a list has even length, then the middle element is defined to be the rightmost of
the two elements in the middle of the list.

Program:
lst=[10,4,23,16,57]
l=len(lst)
if l%2==0:
m=int(l/2)
else:
m=int((l-1)/2)
print("The index of middle element is",m)
print("The middle element is",lst[m])
lst.sort(reverse=True)
print("The list in descending order is",lst)
a=lst.pop(0)
lst.append(a)
print("The list after removing first element and placing it at last is",lst)

Result:
The index of middle element is 2
The middle element is 23
The list in descending order is [57, 23, 16, 10, 4]
The list after removing first element and placing it at last is [23, 16, 10, 4, 57]

Question 4.
Start by assigning to variables monthsL and monthsT a list and a tuple, respectively, both
containing strings 'Jan', 'Feb', 'Mar', and 'May', in that order. Then attempt the following with both
containers:
(a)Insert string 'Apr' between 'Mar' and 'May'.
(b)Append string 'Jun'.
(c)Pop the container.
(d)Remove the second item in the container.
(e)Reverse the order of items in the container.
(f)Sort the container.
Program:
monthL=["Jan","Feb","Mar","May"]
monthT=("Jan","Feb","Mar","May")

monthL.insert(3,"Apr")
print(monthL)
monthT=monthT[:3]+("Apr",)+monthT[3:4]
print(monthT)

monthL.append("Jun")
print(monthL)
monthT=monthT.__add__(("Jun",))
print(monthT)

monthL.pop()
print(monthL)
monthT=monthT[:5]
print(monthT)

monthL.pop(1)
print(monthL)
monthT=monthT[0:1]+monthT[2:6]
print(monthT)

monthL.reverse()
print(monthL)
monthT=monthT[::-1]
print(monthT)

Result:
['Jan', 'Feb', 'Mar', 'Apr', 'May']
('Jan', 'Feb', 'Mar', 'Apr', 'May')
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun')
['Jan', 'Feb', 'Mar', 'Apr', 'May']
('Jan', 'Feb', 'Mar', 'Apr', 'May')
['Jan', 'Mar', 'Apr', 'May']
('Jan', 'Mar', 'Apr', 'May')
['May', 'Apr', 'Mar', 'Jan']
('May', 'Apr', 'Mar', 'Jan')

Question 5.
Write the corresponding Python assignment statements: (a)Assign 6 to variable a and 7 to
variable b.
(b)Assign to variable c the average of variables a and b.
(c)Assign to variable inventory the list containing strings 'paper', 'staples', and 'pencils'.
(d)Assign to variables first, middle and last the strings 'John', 'Fitzgerald', and 'Kennedy'.
(e)Assign to variable fullname the concatenation of string variables first, middle, and last. Make
sure you incorporate blank spaces appropriately.
Program:
a=6
b=7
c=(a+b)/2
print("Average of 6 & 7 =",c)
inventory=[]
for i in range(3):
inventory.append(input("Enter element: "))
print("Inventory :",inventory)
first="John"
middle="Fitzgerald"
last="Kennedy"
sp=str(" ")
fullname=first+sp+middle+sp+last
print("Full Name :",fullname)

Result:
Average of 6 & 7 = 6.5
Enter element: papers
Enter element: staples
Enter element: pencils
Inventory : ['papers', 'staples', 'pencils']
Full Name : John Fitzgerald Kennedy

You might also like