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()
View Answer Discuss Work SpaceAnswer: option a
Explanation:
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)
View Answer Discuss Work SpaceAnswer: option c
Explanation:
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
View Answer Discuss Work SpaceAnswer: 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
View Answer Discuss Work SpaceAnswer: option a
Explanation:
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
View Answer Discuss Work SpaceAnswer: option b
Explanation: