Programming Fundamentals Lab 6(Lists) (1)
Programming Fundamentals Lab 6(Lists) (1)
Experiment No. 06
Lab 06 –Python lists
Lab Objectives:
Python List
A list is a container which holds comma separated values (items or elements) between square
brackets where items or elements need not all have the same type. In general, we can define a list
as an object that contains multiple data items (elements). The contents of a list can be changed
during program execution. The size of a list can also change during execution, as elements are
added or removed from it.
Code:
original_list1 = [10, 22, 44, 23, 4]
new_list2 = list(original_list)
print(original_list1)
print(new_list2)
Output:
Programming Fundamentals 1
Lab 06 – Python List
>>> lst2
[2, 3, 4, 5, 6]
Output:
Program 3: Write a Python function which takes no argument and generate and print a list of first and last
6 elements where the values are cube of numbers between 1 and 30 (both included).
Code:
def CubeValues():
lst = list()
for i in range(1,31):
lst.append(i**3)
print(l[:6])
print(l[-6:])
CubeValues()
# initializing list 1
lis2 = [6, 4, 3]
print ("\r")
Programming Fundamentals 2
Lab 06 – Python List
Programming Exercise
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
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.
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.
Programming Fundamentals 3
Lab 06 – Python List
Programming Fundamentals 4