menu

Python code examples for Beginners

Python code examples for Beginners - Important Points


11.

What is the output of the following code snippet?

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

A. 0

B. 1

C. 2

D. 3

Discuss Work Space

Answer: option c

Explanation:

The index() method is used to return the index of the first occurrence of a specified value in a tuple. In this case, it will return the index of the integer 3 in the tuple my_tuple, which is 2.


12.

What is the output of the following code snippet?

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

A. 1

B. 10

C. 2.5

D. 5

Discuss Work Space

Answer: option b

Explanation:

The sum() function is used to return the sum of all elements in a list. In this case, it will return the sum of the integers in the list my_list, which is 1 + 2 + 3 + 4 = 10.


13.

What is the output of the following code snippet?

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

A. [4, 3, 2, 1]

B. [1, 2, 3, 4]

C. [1, 3, 2, 4]

D. None of the above

Discuss Work Space

Answer: option a

Explanation:

The reverse() method is used to reverse the order of elements in a list. In this case, it will reverse the order of the elements in the list my_list.


14.

What is the output of the following code snippet?

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

A. Hello, World!

B. Hello, Python!

C. Python, World!

D. None of the above

Discuss Work Space

Answer: option b

Explanation:

The replace() method is used to replace a specified substring with another substring in a string. In this case, it will replace the substring "World" with "Python" in the string "Hello, World!", resulting in the string "Hello, Python!".


15.

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)

A. [1, 2, 3, 4]

B. [1, 4, 9, 16]

C. [2, 4, 6, 8]

D. None of the above

Discuss Work Space

Answer: option b

Explanation:

This code snippet uses a list comprehension to create a new list, where each element is the square of the corresponding element in the list my_list. Therefore, the new list will be [1, 4, 9, 16].



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