menu

Python code examples for Beginners


1.

What is the output of the following code snippet?

print("My name is", "John", "and I am", 25, "years old.")

My name is John and I am 25 years old.

My name is and I am years old.

John 25

None of the above


2.

What is the output of the following code snippet?

my_list = [1, 2, 3, 4]
print(my_list[2])

1

2

3

4


3.

What is the output of the following code snippet?

my_tuple = (1, 2, 3, 4)
print(my_tuple.index(3))

0

1

2

3


4.

What is the output of the following code snippet?

my_dict = {"apple": 1, "banana": 2, "orange": 3}
print(my_dict["banana"])

apple

1

banana

2


5.

What is the output of the following code snippet?

my_list = [1, 2, 3, 4]
new_list = [x**2 for x in my_list]
print(new_list)

[1, 2, 3, 4]

[1, 4, 9, 16]

[2, 4, 6, 8]

None of the above


6.

What is the output of the following code snippet?

my_list = [1, 2, 3]
my_list.pop()
print(my_list)

[1, 2]

[2, 3]

[1, 3]

None of the above


7.

What is the output of the following code snippet?

my_list = [1, 2, 3]
my_list.append(4)
print(my_list)

[1, 2, 3, 4]

[1, 2, 4, 3]

[4, 3, 2, 1]

None of the above


8.

What is the output of the following code snippet?

my_dict = {"apple": 1, "banana": 2, "orange": 3}
print(len(my_dict))

1

2

3

None of the above


9.

What is the output of the following code snippet?

my_list = [1, 2, 3, 4]
print(sum(my_list))

1

10

2.5

5


10.

What is the output of the following code snippet?

my_tuple = (1, 2, 3, 4)
print(my_tuple[1:3])

(1, 2)

(2, 3)

(2, 3, 4)

(1, 2, 3)