menu

Python code examples for Beginners


1.

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


2.

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)


3.

What is the output of the following code snippet?

print("5 + 5 =", 5 + 5)

10

5 + 5 = 10

5 + 5

5 + 5 =10


4.

What is the output of the following code snippet?

my_list = [1, 2, 3, 4]
new_list = map(lambda x: x**2, my_list)
print(list(new_list))

[1, 2, 3, 4]

[1, 4, 9, 16]

[2, 4, 6, 8]

None of the above


5.

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


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?

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


8.

What is the output of the following code snippet?

my_string = "Hello, World!"
print(my_string.replace("World", "Python"))

Hello, World!

Hello, Python!

Python, World!

None of the above


9.

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


10.

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