Lists Output#

Section Title: Lists Output

  • Find the output of the following code.

  • Please don’t run the code before giving your answer.     

Question-1#

for i in ['Utah', 'Texas', 'Florida']:
  print(i[1])

Solution

t
e
l

Question-2#

for i in ['Mike', 'Jack', 'Liz', 'Ted', 'John', 'Ashley']:
  print(f'Hello i')

Solution

Hello i
Hello i
Hello i
Hello i
Hello i
Hello i

Question-3#

for i in ['Mike', 'Jack', 'Liz', 'Ted', 'John', 'Ashley']:
  print(f'Hello {i}')

Solution

Hello Mike
Hello Jack
Hello Liz
Hello Ted
Hello John
Hello Ashley

Question-4#

for i in ['Mike', 'Jack', 'Liz', 'Ted', 'John', 'Ashley', 'Bob']:
  if i[1] == 'o':
    print(i)

Solution

John
Bob

Question-5#

for i in ['Mike', 'Jack', 'Liz', 'Ted', 'John', 'Ashley', 'Bob', ['A', 'B']]:
  if len(i)<4:
    print(i)

Solution

Liz
Ted
Bob
[‘A’, ‘B’]

Question-6#

for i in ['Mike', 'Jack', 'Liz']:
  print(f'{i.lower()} ---- {i.upper()}')

Solution

mike —- MIKE
jack —- JACK
liz —- LIZ

Question-7#

n = 0
my_list = [3,1,6,-3,-5,7,8,9]
for i in my_list:
  if i >5:
    n += i
print(n)

Solution

30

Question-8#


p = 1
my_numbers = [2,-4,-3,5]
for i in my_numbers:
  if i <4:
    p *= i
print(p)

Solution

24

Question-9#

n = 0
my_list = [3,1,6,-3,-5,7,8,9]
for i in range(len(my_list)):
  if i >5:
    n += my_list[-i]
print(n)

Solution

7

Question-10#

my_list = [1,2,3,4,5]
for i in range(3):
  my_list[i] = 10*i
print(my_list)

Solution

[0, 10, 20, 4, 5]

Question-11#

x = []
while True:
  x.append(2)
  if len(x) == 4:
    break
print(x)

Solution

[2, 2, 2, 2]

Question-12#

mylist = [['NY', 'CA', 'FL'], ('Amy', 'John', 'Ashley', 'Michael')]
print(mylist[0][-2])
print(mylist[1][1])
print(mylist[1][:2])

Solution

CA
John
(‘Amy’, ‘John’)