wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python - String and Lists

Total questions: 20

Worksheet time: 30mins

Name
Class
Date
1.

str1 = "Hello World"

result = ""

for char in str1:

if char.isalpha():

result += char.lower()

else:

result += char

print(result)

What will be the output of the above code?

a)

hello world

b)

HELLO WORLD

c)

Hello World

d)

hELLO wORLD

2.

str2 = "Python is awesome"

result = ""

for i in range(len(str2)):

if i % 2 == 0:

result += str2[i].upper()

else:

result += str2[i].lower()

print(result)

What will be the output of the above code?

a)

PyThOn iS AwEsOmE

b)

pYtHoN iS AwEsOmE

c)

PYTHON IS AWESOME

d)

PytHon Is aWesoMe

3.

str3 = "12345"

result = ""

for char in str3:

result += str(int(char) * 2)

print(result)

What will be the output of the above code?

a)

246810

b)

13579

c)

Typecasting Error

d)

12345246810

4.

str4 = "Good Morning"

result = ""

for char in str4:

if char.isalpha():

result += char

print(result)

What will be the output of the above code?

a)

Good Morning

b)

GdMrng

c)

Morning

d)

GoodMorning

5.

str5 = "abcdefg"

result = ""

for i in range(len(str5)):

if i % 2 != 0:

result += str5[i]

print(result)

What will be the output of this code?

a)

bdfg

b)

aceg

c)

abcdefg

d)

bdf

6.

my_list = [1, 2, 3, 4, 5]

if len(my_list) > 3:

result = my_list[:3]

else:

result = my_list[::-1]

print(result)

What will be the output of the above code?

a)

[1, 2, 3]

b)

[5, 4, 3, 2, 1]

c)

[1, 2, 3, 4, 5]

d)

[1, 2]

7.

my_list = [1, 2, 3, 4, 5]

if sum(my_list) > 20:

result = my_list[-1] * 2

else:

result = my_list[2]**2 + my_list[-2]**2

print(result)

What will be the output of this code?

a)

7

b)

10

c)

25

d)

2

8.

my_list = [10, 20, 30, 40, 50]

if my_list[0] < my_list[-1]:

result = my_list[-1] - my_list[0]

else:

result = len(my_list)

print(result)

What will be the output of this code?

a)

5

b)

40

c)

10

d)

50

9.

my_list = [1, 2, 3, 4, 5]

if len(my_list) > 3:

result = [x**2 for x in my_list]

else:

result = [x for x in my_list if x % 2 == 0]

print(result)

What will be the output of this code?

a)

[1, 4]

b)

[1, 4, 9, 16, 25]

c)

[2, 4]

d)

[1, 4, 9, 16]

10.

my_list = [10, 20, 30, 40, 50]

if sum(my_list) > 150:

result = [x * 2 for x in my_list]

else:

result = [x for x in my_list if x > 30]

print(result)

What will be the output of this code?

a)

[40, 50]

b)

[20, 30, 40, 50]

c)

[20, 30]

d)

[10, 20, 30, 40, 50]

11.

my_list = [1, 2, 3, 4, 5]

if my_list[-1] % 2 == 0 and my_list[-4] % 2 != 0:

result = [x**2 for x in my_list]

else:

result = [x**3 for x in my_list]

print(result)

What will be the output of this code?

a)

[1, 4, 9, 16, 25]

b)

[1, 8, 27, 64, 125]

c)

[1, 27, 125]

d)

[1, 8, 27, 64, 5]

12.

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

if len(my_list) % 2 == 0:

result = [x for x in my_list if x % 2 == 0]

else:

result = [x for x in my_list if x % 2 != 0]

print(result)

What will be the output of this code?

a)

[2, 4, 6, 8]

b)

[1, 2, 3, 4, 5, 6, 7, 8, 9]

c)

[1, 3, 5, 7, 9]

d)

[2, 4, 6, 7, 8]

13.

my_list = [10, 20, 30, 40, 50]

result = []

for i in range(len(my_list)):

result.append(my_list[i] - i)

print(result)

What will be the output of this code?

a)

[10, 18, 28, 38, 48]

b)

[10, 19, 28, 37, 46]

c)

[0, 19, 28, 37, 46]

d)

[10, 20, 30, 40, 50]

14.

my_list = [10, 20, 30, 40, 50]

result = []

for i in my_list:

if i % 3 == 0:

result.append(i)

else:

result.append(i // 2)

print(result)

What will be the output of this code?

a)

[5, 10, 15, 20, 25]

b)

[5, 10, 15, 20, 25, 30]

c)

[5, 10, 15, 20, 25, 30, 40, 50]

d)

[5, 10, 30, 20, 25]

15.

my_list = [2, 3, 4, 5, 6]

result = [x**2 if x % 2 == 0 else x**3 for x in my_list]

print(result)

What will be the output of this code?

a)

[2, 27, 4, 125, 6]

b)

[4, 9, 16, 25, 36]

c)

[4, 27, 16, 125, 36]

d)

[4, 9, 16, 5, 36]

16.

my_list = [2, 4, 6, 8, 10]

result = []

for i in range(len(my_list)):

result.append([my_list[i], my_list[i] % 3 == 0])

print(result)

What will be the output of this code?

a)

[[2, True], [4, True], [6, False], [8, True], [10, False]]

b)

[[2, False], [4, False], [6, True], [8, False], [10, False]]

c)

[2, 4, 6, 8, 10]

d)

[[2, 4, 6, 8, 10], [False, False, True, False, False]]

17.

str2 = "Python is powerful"

result = ""

for word in str2.split():

if len(word) % 2 == 0:

result += word.upper() + " "

else:

result += word.lower() + " "

print(result.strip())

What will be the output of this code?

a)

python IS POWERFUL

b)

PYTHON IS POWERFUL

c)

Python IS powerful

d)

python is POWERFUL

18.

str4 = "1234567890"

result = ""

for char in str4:

if int(char) % 2 == 0:

result += char

else:

result += "-"

print(result)

What will be the output of this code?

a)

24680

b)

1-3-5-7-9-

c)

1234567890

d)

-2-4-6-8-0

19.

list4 = [2, 4, 6, 8, 10]

result = [x**2 if x % 2 == 0 else x for x in list4 if x > 5]

print(result)

What will be the output of this code?

a)

[36, 64]

b)

[16, 36, 64, 100]

c)

[36, 64, 100]

d)

[4, 16, 36, 64, 100]

20.

list3 = [10, 15, 20, 25, 30]

result = [x if x % 2 == 0 else x * 2 for x in list3]

print(result)

What will be the output of this code?

a)

[10, 15, 20, 25, 30]

b)

[20, 30, 40, 50, 60]

c)

[20, 15, 40, 25, 60]

d)

[10, 30, 20, 50, 30]