Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

❖ The Islamia University Of

Bahawalpur
❖ Department of Information Security
❖ Name : Muhammad Talha Munir
❖ Assignment # 5
❖ Class: BS(IS) – Semester 1(MOR)
❖ Course Instructor: Dr. Shahzada Khurram
❖Roll no: F20BINFS1M02046

Q:1
Write a Python program to calculate the length of a string.
Ans:1
x = "talha"
print(len(x))
output:
5

Q:2
Write a program to add 'ing' at the end of a given string (length
should be at least 3). If the given string already ends with 'ing'
then add 'ly' instead. If the string length of the given string is less
than 3, leave it unchanged.
Ans:2
def add_string(str1):
length = len(str1)

if length > 2:
if str1[-3:] == 'ing':
str1 += 'ly'
else:
str1 += 'ing'

return str1
print(add_string('ab'))
print(add_string('abc'))
print(add_string('string'))

Q:3
Consider the following list:
a = [1, 2, 3, 4, 5, 4, 3, 2, 1, 0]
What is the value of total after each of the following loops
complete?
Ans:3
a)
total = 0
for i in range (1, 10, 2):
total = total + a[i]
print(total)
OUTPUT
2
6
10
12
12

b)
total = 0
for i in range (2,11):
total = total + a[i]
print(total)
OUTPUT
3
7
12
16
19
21
22
22
list index out of range

c)
total = 0
for i in range (9,-1,-2):
total = total + a[i]
print(total)
OUTPUT
0
2
6
10
12

d)
total = 0
for i in range (0,10):
total = total + a[i]
print(total)
OUTPUT
1
3
6
10
15
19
22
24
25
25

Q:4
What is wrong with each of the following code segments?
Ans:4
a)
values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(1, 11) :
values[i] = i * i
WRONG: values and range are same we only used one thing at once

b)
values = []
for i in range(len(values)) :
values[i] = i * i
WRONG: Values are not given
Q:5
Write a loop that reads ten numbers and a second loop that
displays them in the opposite order from which they were
entered.
ANS:5
Loop That Reads TEN Number
j=0
while j<10:
print(j)
j+=1
OUTPUT
0
1
2
3
4
5
6
7
8
9

Loop That Display Opposite Number


i=9
while i>-1:
print(i)
i-=1
OUTPUT
9
8
7
6
5
4
3
2
1
0

Q:6
How do you perform the following tasks with lists in
Python?
Ans:6
a)
Test that two lists contain the same elements in the same order.
l1=[1,"2","3","4"]
l2=[1,"2","3","4"]
print(l1,l2)
OUTPUT
[1, '2', '3', '4'] [1, '2', '3', '4']

b)
Copy one list to another.
l1=[1,2,3,4,5]
l2=l1
print(l2)
OUTPUT
[1, 2, 3, 4, 5]

c)
Fill a list with zeroes, overwriting all elements in it
l1=[1,2,3,4,5]
l1[0]=0
l1[1]=0
l1[2]=0
l1[3]=0
l1[4]=0
print(l1)
OUTPUT
[0, 0, 0, 0, 0]

d)
Remove all elements from a list.
l1=[1,2,3,4,5]
l1.clear()
print(l1)
OUTPUT
[]

Q:7
Consider the following list:
a = [1, 2, 3, 4, 5, 4, 3, 2, 1, 0]
What are the contents of the list a after each of the
following loops complete? (For each part, assume the list a
contains the original list of values.)
a)
for i in range (1, 10) :
a[i] = a[i - 1]
print(a)
OUTPUT
[1, 1, 3, 4, 5, 4, 3, 2, 1, 0]
[1, 1, 1, 4, 5, 4, 3, 2, 1, 0]
[1, 1, 1, 1, 5, 4, 3, 2, 1, 0]
[1, 1, 1, 1, 1, 4, 3, 2, 1, 0]
[1, 1, 1, 1, 1, 1, 3, 2, 1, 0]
[1, 1, 1, 1, 1, 1, 1, 2, 1, 0]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

b)
for i in range (9, 0, -1) :
a[i] = a[i - 1]
print(a)
OUTPUT
[1, 2, 3, 4, 5, 4, 3, 2, 1, 1]
[1, 2, 3, 4, 5, 4, 3, 2, 2, 1]
[1, 2, 3, 4, 5, 4, 3, 3, 2, 1]
[1, 2, 3, 4, 5, 4, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 4, 5, 4, 3, 2, 1]
[1, 2, 3, 3, 4, 5, 4, 3, 2, 1]
[1, 2, 2, 3, 4, 5, 4, 3, 2, 1]
[1, 1, 2, 3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