NEW
Font size
WorksheetsPython - String and Lists
Total questions: 20
Worksheet time: 30mins
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?
hello world
HELLO WORLD
Hello World
hELLO wORLD
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?
PyThOn iS AwEsOmE
pYtHoN iS AwEsOmE
PYTHON IS AWESOME
PytHon Is aWesoMe
str3 = "12345"
result = ""
for char in str3:
result += str(int(char) * 2)
print(result)
What will be the output of the above code?
246810
13579
Typecasting Error
12345246810
str4 = "Good Morning"
result = ""
for char in str4:
if char.isalpha():
result += char
print(result)
What will be the output of the above code?
Good Morning
GdMrng
Morning
GoodMorning
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?
bdfg
aceg
abcdefg
bdf
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?
[1, 2, 3]
[5, 4, 3, 2, 1]
[1, 2, 3, 4, 5]
[1, 2]
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?
7
10
25
2
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?
5
40
10
50
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?
[1, 4]
[1, 4, 9, 16, 25]
[2, 4]
[1, 4, 9, 16]
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?
[40, 50]
[20, 30, 40, 50]
[20, 30]
[10, 20, 30, 40, 50]
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?
[1, 4, 9, 16, 25]
[1, 8, 27, 64, 125]
[1, 27, 125]
[1, 8, 27, 64, 5]
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?
[2, 4, 6, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 5, 7, 9]
[2, 4, 6, 7, 8]
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?
[10, 18, 28, 38, 48]
[10, 19, 28, 37, 46]
[0, 19, 28, 37, 46]
[10, 20, 30, 40, 50]
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?
[5, 10, 15, 20, 25]
[5, 10, 15, 20, 25, 30]
[5, 10, 15, 20, 25, 30, 40, 50]
[5, 10, 30, 20, 25]
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?
[2, 27, 4, 125, 6]
[4, 9, 16, 25, 36]
[4, 27, 16, 125, 36]
[4, 9, 16, 5, 36]
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?
[[2, True], [4, True], [6, False], [8, True], [10, False]]
[[2, False], [4, False], [6, True], [8, False], [10, False]]
[2, 4, 6, 8, 10]
[[2, 4, 6, 8, 10], [False, False, True, False, False]]
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?
python IS POWERFUL
PYTHON IS POWERFUL
Python IS powerful
python is POWERFUL
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?
24680
1-3-5-7-9-
1234567890
-2-4-6-8-0
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?
[36, 64]
[16, 36, 64, 100]
[36, 64, 100]
[4, 16, 36, 64, 100]
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?
[10, 15, 20, 25, 30]
[20, 30, 40, 50, 60]
[20, 15, 40, 25, 60]
[10, 30, 20, 50, 30]
