Blogint() and float() — what's actually happening when you convert
2026-05-13 · By Kelvin Amoaba

int() and float() — what's actually happening when you convert

int() and float() are functions, and functions hand things back. Once you see that, the substitution model clicks and lines like int(input(...)) stop feeling like magic.

The first time I wrote money = int("100"), I thought I was casting a small spell. The text became a number and I moved on with my life, no idea what had happened inside. It worked, so I did not ask. Months later, when someone showed me int(input("Years: ")) and I had no idea how to read it, I realised I had been carrying a quiet lie around: that int was a kind of conversion magic, not a function like any other.

It is a function. It is doing one tiny job. And once you see what that job is, half of Python stops feeling like syntax and starts feeling like sentences.

What a function actually is

A function is a small box you hand something to. The box does some work, and hands you something back. That last part, the handing back, is the thing nobody quite spells out on day one, and it is the part that matters most.

Think of dropping an application form at a registrar's window. You hand the clerk the form (in code, that thing is called the argument). The clerk does whatever they do behind the counter (the function's work). Then they hand you back a stamped form, a receipt, or a slip saying "come back Tuesday." Whatever they give back is what you carry forward.

The clerk's work is hidden. You do not see it. You only see what went in and what came out. That is a function.

print(...) is a function. So is input(...). So is int(...), float(...), and len(...). You have been calling functions all week. The new idea today is that most of them hand you something back, and you can hold on to what they hand you.

What "return" means

When a function hands you a value, we say it returns that value. And here is the rule that ties the whole thing together.

When a function returns a value, mentally replace the call with that value, and read the line again.

That is it. That is the substitution model. Everything that looks confusing in your code unravels with that one move.

Take this:

length = len("scholarship")

len("scholarship") is a function call. It returns 11. Substitute it:

length = 11

That is what Python actually does. The call disappears, the value it returned takes its place, and the line assigns 11 to length.

It works the same way for int.

money = int("100")

int("100") is a function call. It takes the text "100" and returns the integer 100. Substitute:

money = 100

Now Python does the assignment. The text "100" was never put in the jar. The number that int handed back was.

This is the bit nobody told me. The function call does not sit in the variable. The function call runs first, returns a value, and the value is what gets stored.

A slow walk through money = int("100")

Let me show what Python actually does, line by line, as if I were narrating it for you.

money = int("100")
  1. Python sees an =. That tells it: "There is an assignment here. Whatever is on the right, evaluate it first, then put the result in the jar on the left."
  2. It looks at the right side: int("100"). That is a function call, because of the parentheses.
  3. It runs the call. It hands "100" to int. int reads the characters, sees they form a valid whole number, and returns 100.
  4. Now Python substitutes. The line becomes money = 100.
  5. It does the assignment. The number 100 goes into the jar labelled money.

The text "100" is gone after step 3. It was an ingredient. The integer 100 is what survives.

Five quiet steps. No magic.

Why this matters for int(input(...))

If you have sat through Session 2, you have seen this line:

years = int(input("Years of experience: "))

Two function calls glued together. Before today, it might have looked like one weird incantation. Apply the substitution model.

The innermost call runs first. input("Years of experience: ") returns whatever the user typed, as text. Say they typed 5. The call returns "5". Substitute:

years = int("5")

Now the next call runs. int("5") returns 5. Substitute again:

years = 5

Now the assignment happens. The integer 5 lands in years.

Three steps, one line. Every nested call in Python works this way. You evaluate from the inside out, replace each call with what it returned, and keep going until the line is just values.

The three converters

You will see three small functions all term whose only job is to hand back a different type of the thing you gave them.

  • int("27") takes text and returns the whole number 27.
  • float("3.7") takes text and returns the decimal number 3.7.
  • str(27) takes a number and returns the text "27".

They do not change the original. They take the value, look at it, and return a new value of a different type. The old thing is left alone.

One honest warning. int("hello") will not work. There is no whole number hiding inside the word "hello," so Python raises a ValueError and stops. The function only returns a number when it can read one out of the text. When it cannot, it complains. That is good. It would be worse if it silently returned zero and you spent an hour hunting the bug.

A useful habit

Every time a line of code confuses you, look for the innermost function call. Read it. Ask yourself: "what does this hand back?" Replace the call in your head with that value. Read the line again. Repeat until the line is just values being assigned, printed, or compared. The whole thing unfolds.

You do not need to memorise this. You need to feel it. After ten or fifteen lines of int(input(...)) and len(something) and print(...), the substitution will happen without you noticing. That is the moment Python stops being a list of incantations and starts being a language you can read.