Remember
int() converts supported values to integers. Converting a floating-point number truncates toward zero.
int() converts supported values to integers. Converting a floating-point number truncates toward zero.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
number = 3.8
print(int(number))Top-level scope
3.8int() converts supported values to integers. Converting a floating-point number truncates toward zero.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
number = 3.8
print(int(number))
|
v
DISPLAYED RESULT
3
FINAL BINDINGS
number --> object 1 (float)
object 1: 3.8Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
number = 3.8
print(int(number))Top-level scope
3.8The example has finished. Compare the final output with the values and references above.
3
What do you expect this program to print?
number = 3.8
print(int(number))int() FunctionThe int() function in Python converts a number or a string into an integer. It's one of the most commonly used functions in Python programming.
int(x=0, base=10)
x – A number (like float or string) to convert to an integer. Default is 0.base – (Optional) The base of the number (used only if x is a string). Default is 10.print(int(5.9))
5
Note: It truncates the decimal part (does not round).
print(int("42"))
42
print(int("101", 2))
5
print(int("1A", 16))
26
int("abc") → ValueErrorNone or an empty string: causes TypeError or ValueErrorThe int() function is frequently used in string parsing, input handling, and base conversions — all common in coding interviews.
int() converts numbers and strings to integers.Write a program that asks the user to enter a number and prints the number as an integer.
user_input = input("Enter a number: ")
try:
print("As integer:", int(user_input))
except ValueError:
print("That's not a valid number!")
Connect what you noticed in the example with the idea below.
int() converts supported values to integers. Converting a floating-point number truncates toward zero.
int() always rounds to the nearest integer.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.