menu

Python code examples for Beginners

Python code examples for Beginners - Important Points


6.

What is the output of the following code snippet?

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

A. apple

B. 1

C. banana

D. 2

Discuss Work Space

Answer: option d

Explanation:

Dictionaries in Python are composed of key-value pairs. In this case, we access the value of the "banana" key in the dictionary my_dict, which is the integer 2.


7.

What is the output of the following code snippet?

my_string = "Hello, World!"
print(my_string.upper())

A. hello, world!

B. Hello World!

C. HELLO, WORLD!

D. HELLO, WORLD!

Discuss Work Space

Answer: option c

Explanation:

The upper() method is used to convert all characters in a string to uppercase. In this case, it will convert the string "Hello, World!" to "HELLO, WORLD!".


8.

What is the output of the following code snippet?

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

A. [1, 2, 3, 4]

B. [1, 2, 4, 3]

C. [4, 3, 2, 1]

D. None of the above

Discuss Work Space

Answer: option a

Explanation:

The append() method is used to add an element to the end of a list. In this case, it will add the integer 4 to the end of the list my_list.


9.

What is the output of the following code snippet?

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

A. [1, 2]

B. [2, 3]

C. [1, 3]

D. None of the above

Discuss Work Space

Answer: option a

Explanation:

The pop() method is used to remove and return the last element of a list. In this case, it will remove the integer 3 from the end of the list my_list and return it. Therefore, the list my_list will now contain [1, 2].


10.

What is the output of the following code snippet?

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

A. 1

B. 2

C. 3

D. None of the above

Discuss Work Space

Answer: option c

Explanation:

The len() function is used to return the number of items in an object. In this case, it will return the number of key-value pairs in the dictionary my_dict, which is 3.


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now