Python Interview Questions
Python - Data Types
|1. What is the type of the following: 1
Explanation: As there is no decimal, the number is of type int
|2. What is the type of the following "7.1"
Explanation: The type is string
|3. What is the result of the following code segment: int(12.3)
Explanation: In Python, if you cast a float to an integer, the conversion truncates towards zero.
|4. What is the result of the following code segment: int(True)
Explanation: When you cast a boolean True to an integer you get a 1
|5. What do you call a value that doesn’t have decimal values?
A) A number
B) An integer
C) A string
Answer: An integer
|6. What do you call a value that does have decimal values?
A) A float
B) A number
C) An integer
Answer: A float
|7. What data type can only have either a value of True or False?
A) A string
B) A boolean
C) An integer
Answer: A boolean
|8. What code would turn the string “1” into an integer?
|9. What character begins a single line comment?
|10. What do we call it when we convert from one data type to another?
|11. What is the datatype of np.nan?
|12. Which of the following numbers is NOT a float?
Explanation:0 on its own is an int. 0.0 however, is a float.
|13. What values can the Boolean data type hold?
A) Integers, fractions, complex numbers
B) Unicode characters
C) True or False values
D) Any other data type
Answer: True or False values
|14. WWhat does it mean that Python is a dynamically-typed language?
A) Variables in python can implicitly change to other types when comparing. For examples you can compare a string "2" and the number 2 using ==.
B) Python variables can be assigned to different types and changes types at will.
C) Python is a more efficient language than C++
D) All of the above
Answer: Python variables can be assigned to different types and changes types at will.
Explanation: Dynamic-typing just refers to the ability for variables to flexibly learn their types during assignment.