Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Programming Fundamentals Lab 6(Lists) (1)

Lab 06 focuses on Python lists, introducing their properties and methods such as extend(), copy(), and clear(). It includes programming exercises that involve manipulating lists and applying mathematical concepts using Python. The lab aims to enhance understanding of list operations and their applications in programming.

Uploaded by

sohair.ateeq6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Programming Fundamentals Lab 6(Lists) (1)

Lab 06 focuses on Python lists, introducing their properties and methods such as extend(), copy(), and clear(). It includes programming exercises that involve manipulating lists and applying mathematical concepts using Python. The lab aims to enhance understanding of list operations and their applications in programming.

Uploaded by

sohair.ateeq6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 06 – Python List

Experiment No. 06
Lab 06 –Python lists

Lab Objectives:

1. Introduction to Python Lists

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.

Program 1: Write a program to copy a list.

Code:
original_list1 = [10, 22, 44, 23, 4]
new_list2 = list(original_list)
print(original_list1)
print(new_list2)

Output:

Student Name: Roll No: Section:


Program 2: Using the following interactive session as an aid, explain in your own words what
the list methods extend(), copy(), and clear() do.
>>> lst = [2, 3, 4]
>>> lst.extend([5, 6])
>>> lst
[2, 3, 4, 5, 6]
>>> lst2 = lst.copy()
>>> lst2
[2, 3, 4, 5, 6]
>>> lst.clear()
>>> lst
[]

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

Program 4: Use of Extend method with list


lis1 = [2, 1, 3, 5]

# initializing list 1
lis2 = [6, 4, 3]

# using extend() to add elements of lis2 in lis1


lis1.extend(lis2)

# displaying list after sorting


print ("List elements after extending are : ", end="")
for i in range(0, len(lis1)):
print(lis1[i], end=" ")

print ("\r")

Programming Fundamentals 2
Lab 06 – Python List

Student Name: Roll No: Section:

Programming Exercise

1. Take a sample list [2, 1, 3, 5, 4, 3, 8]


1. Apply del(), remove(), sort(), insert(), pop(), extend()…)

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

Student Name: Roll No: Section:


(f)Sort the container.
Note: when attempting these on tuple monthsT you should expect errors.

6. 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.

Programming Fundamentals 4

You might also like