menu

Python code examples for Beginners

Python code examples for Beginners - Important Points


1.

What is the output of the following code snippet?

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

A. My name is John and I am 25 years old.

B. My name is and I am years old.

C. John 25

D. None of the above

Discuss Work Space

Answer: option a

Explanation:

The print() function can take multiple arguments separated by commas. In this case, it will output the string "My name is John and I am 25 years old."

2.

What is the output of the following code snippet?

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

A. 10

B. 5 + 5 = 10

C. 5 + 5

D. 5 + 5 =10

Discuss Work Space

Answer: option b

Explanation:

The print() function can perform arithmetic operations within its arguments. In this case, it will output the string "5 + 5 =" followed by the result of the addition operation, which is 10.

3.

What is the output of the following code snippet?

x = 5y = 7print("The sum of", x, "and", y, "is", x + y)

A. The sum of 5 and 7 is 12

B. The sum of x and y is x + y

C. The sum of and is

D. The sum of x and y "is" x + y

Discuss Work Space

Answer: option a

Explanation:

The print() function can perform arithmetic operations using variables within its arguments. In this case, it will output the string "The sum of 5 and 7 is" followed by the result of the addition operation between x and y, which is 12.

4.

What is the output of the following code snippet?

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

A. 1

B. 2

C. 3

D. 4

Discuss Work Space

Answer: option c

Explanation:

Lists are indexed starting from 0 in Python. In this case, my_list[2] will access the element at index 2 (the third element), which is the integer 3.


5.

What is the output of the following code snippet?

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

A. (1, 2)

B. (2, 3)

C. (2, 3, 4)

D. (1, 2, 3)

Discuss Work Space

Answer: option b

Explanation:

Tuples are also indexed starting from 0 in Python. In this case, my_tuple[1:3] will access the elements at index 1 and index 2 (the second and third elements), which are the integers 2 and 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