0% found this document useful (0 votes)
22 views4 pages

Laboratory No

The document discusses Python lists, including creating and manipulating list elements, slicing lists, nesting lists, and sorting lists. It provides examples of list operations like indexing, concatenation, replacing and removing elements, appending to lists, and using len() to determine a list's length.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Laboratory No

The document discusses Python lists, including creating and manipulating list elements, slicing lists, nesting lists, and sorting lists. It provides examples of list operations like indexing, concatenation, replacing and removing elements, appending to lists, and using len() to determine a list's length.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

BULACAN STATE UNIVERSITY

COLLEGE OF ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
City of Malolos Bulacan

COMPUTER FUNDAMENTALS AND PROGRAMMING


LABORATORY NO. 2
LISTS
MARQUEZ, DENISE ANN B.
NAME: ___________________________________ SCORE: _______________
BSCE 1B
C/Y/S: __________________________________ 28/04/2024
DATE: ________________
1. Objective: To know what is Python programming language, its structure, its operation and its
difference & significance on other programming languages and familiarized with numbers and
strings manipulation.
2. Software: Python 2.7.14 or newer verisions
3. Procedures
1. Go to command prompt and type python.
LISTS
Python knows a number of compound data types, used to group together other values.
The most versatile is the list, which can be written as a list of comma-separated values (items)
between square brackets. Lists might contain items of different types, but usually the items all
have the same type.
Enter the given commands and write the result or your answers if you encountered a
blank space like _____________.
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 8, 16, 25]
______________________
Like strings (and all other built-in sequence type), lists can be indexed and sliced:
>>> squares[0] # indexing returns the item
1
______________________
>>> squares[-1]
25
______________________
>>> squares[-3:] # slicing returns a new list
8
______________________
All slice operations return a new list containing the requested elements. This means that the
following slice returns a new (shallow) copy of the list:
>>> squares[:]
[1, 4, 8, 16, 25]
______________________
Lists also supports operations like concatenation:
>>> squares + [36, 49, 64, 81, 100]
______________________
[1, 4, 8, 16, 25, 36, 49, 64, 81, 100]

1
Unlike strings, which are immutable, lists are a mutable type, i.e. it is possible to change their
content:
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here
>>> 4 ** 3 # the cube of 4 is 64, not 65!
64
______________________
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
______________________
You can also add new items at the end of the list, by using the append() method.
>>> cubes.append(216) # add the cube of 6
>>> cubes.append(7 ** 3) # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
______________________

Assignment to slices is also possible, and this can even change the size of the list or clear it
entirely:
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
______________________
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
______________________
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
______________________
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]
______________________
The built-in function len() also applies to lists:
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
______________________
It is possible to nest lists (create lists containing other lists),
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
______________________
>>> x[0]
['a', 'b', 'c']
______________________
>>> x[0][1]
'b'
____ __________________

2. EXERCISES
What does the following code print?
lst = []
nums = [15, 6]
2
lst.append(nums)
nums = [10, 30, 20]
lst.append(nums)
lst.sort()
print lst

A. [6, 10, 15, 20, 30]


B. [[6, 15], [10, 20, 30]]
C. [[15, 6], [10, 30, 20]]
D. [[10, 30, 20], [15, 6]]
E. [[10, 20, 30], [6, 15]]

What does the following code print?


mhuggz = [1, 1, 2, 3, 5, 8, 13, 21]
mhuggz[1:3]
A. [1, 1, 2, 3]
B. [1, 2, 3]
C. [1, 2]
D. [1, 1, 2]
E. [1, 2, 3, 5, 8, 13, 21]

3.

You might also like