What is the result of the expression int(4.8) in Python?
a) 5
b) 4.0
c) 4
d) 4.8
Answer:
c) 4
Explanation:
In Python, the int()
function is used to convert a floating-point number to an integer by truncating the decimal part, not rounding. The expression int(4.8)
converts 4.8 to 4.
result = int(4.8) # result is 4
This conversion is useful when you need to perform integer arithmetic or work with indices or counts where decimal values are not allowed. Understanding how type casting works in Python helps prevent errors in calculations, especially in loops or when dealing with user inputs.
It’s important to note that this truncation can lead to a loss of precision, so it should be used carefully, especially in scenarios requiring exact numeric calculations.