menu

Python Basics

Python Basics - Important Points


26. Which of the following is the correct way to read a line from a file in Python?

A. file.readline()

B. file.read(line)

C. read(file, line)

D. file.read()

Discuss Work Space

Answer: option a

Explanation:

The readline() method is used to read a line from a file in Python.

27. Which of the following is the correct way to check if a key is in a dictionary in Python?

A. dict.contains(key)

B. dict.has_key(key)

C. key in dict

D. dict.get(key)

Discuss Work Space

Answer: option c

Explanation:

The in keyword is used to check if a key is in a dictionary in Python.

28.

What is the output of the following code?

x = "Hello, world!"
print(x[1:8])

A. Hello,

B. ello, w

C. ello, wo

D. Hello, w

Discuss Work Space

Answer: option b

Explanation:

Slicing is used to extract a portion of a string. In this case, the portion from index 1 to index 7 (excluding index 8) is extracted.


29. Which of the following is the correct way to convert a string to an integer in Python?

A. int(string)

B. str(int)

C. string.int()

D. All of the above

Discuss Work Space

Answer: option a

Explanation:

The int() function is used to convert a string to an integer in Python.

30. Which of the following is the correct way to iterate over a list in Python?

A. for i in range(len(list)):

B. for i in list:

C. for i in list.items():

D. All of the above

Discuss Work Space

Answer: option b

Explanation:

The for loop is used to iterate over a list in Python.


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