Swe-102 Lab 08!
Swe-102 Lab 08!
LAB # 08
LISTING
OBJECTIVE
Exploring list/arrays in python programming.
THEORY
A list can store a collection of data of any size. It is a collection which is ordered and
changeable. The elements in a list are separated by commas and are enclosed by a pair
of brackets [ ].
Creating a lists:
A list is a sequence defined by the list class but also have alternative for creation of list
without built-in function.
Syntax: With Built-in function list( )
list1 = list() # Create an empty list
list2 = list([2, 3, 4]) # Create a list with elements 2, 3, 4
list3 = list(["red", "green", "blue"])#Create a list with strings
list4 = list(range(3, 6)) # Create a list with elements 3, 4, 5
list5 = list("abcd") # Create a list with characters a, b, c, d
Example:
#Create list
list_1 = ["apple", "banana", "cherry"]
#display list
print("Current List:",list_1)
1
Programming Fundamentals (SWE-102) SSUET/QR/114
Example:
myList = ["The", "earth", "revolves", "around", "sun"]
print("Postive index:",myList[4])
print("Negative index:",myList[-2])
Output:
>>> %Run task1.py
Postive index: sun
Negative index: around
Output:
>>> %Run task2.py
Slicing 2 to 5 index: [4, 5, 6]
Slicing before 3rd index value: [1, 2, 4]
Slicing after 3rd index value: [5, 6, 7, 8]
2
Programming Fundamentals (SWE-102) SSUET/QR/114
EXERCISE
A. Point out the errors, if any, and paste the output also in the following Python
programs.
1. Code
Def max_list( list ):
max = list[ 0 ]
for a is in list:
elif a > max:
max = a
return max
print(max_list[1, 2, -8, 0])
Output
2. Code
motorcycles = {'honda', 'yamaha', 'suzuki'}
print(motorcycles)
del motorcycles(0)
print(motorcycles)
3
Programming Fundamentals (SWE-102) SSUET/QR/114
Output:
3. Code
Def dupe_v1(x):
y = []
for i in x:
if i not in y:
y(append(i))
return y
a = [1,2,3,4,3,2,1]
print a
print dupe_v1(a)
Output:
Output
4
Programming Fundamentals (SWE-102) SSUET/QR/114
2. Code
def multiply_list(elements):
t = 1
for x in elements:
t*= x
return t
print(multiply_list([1,2,9]))
Output
3. Code
def add(x,lst=[] ):
if x not in lst:
lst.append(x)
return lst
def main():
list1 = add(2)
print(list1)
list2 = add(3, [11, 12, 13, 14])
print(list2)
main()
Output
1. Write a program that store the names of a few of your friends in a list called ‘names’.
Print each person’s name by accessing each element in the list, one at a time.
2. Write a program that make a list that includes at least four people you’d like to invite
to dinner. Then use your list to print a message to each person, inviting them to dinner.
But one of your guest can’t make the dinner, so you need to send out a new set of
invitations. Delete that person on your list, use del statement and add one more person
at the same specified index, use the insert( ) method. Resend the invitation.
5
Programming Fundamentals (SWE-102) SSUET/QR/114
3. Write a program that take list = [30, 1, 2, 1, 0], what is the list after applying each of
the following statements? Assume that each line of code is independent.
list.append(40)
list.remove(1)
list.pop(1)
list.pop()
list.sort()
list.reverse()