~/TechPurAI
~/tutorials/python-from-scratch/variables-types-and-input
beginner·part 2 of 6·3 min read

Variables, types, and reading input

Updated Aug 11, 2026Python

A tracker that only prints fixed text isn't tracking anything. In this part the script starts asking questions and doing arithmetic with the answers — which is where Python's types stop being trivia and start mattering.

Variables hold values

A variable is a name pointing at a value. No declaration keyword, no type annotation required:

python
amount = 12.5
category = "food"
note = "lunch with the team"

Python infers the type from the value: 12.5 is a float, and the quoted values are str. You can confirm it any time with the built-in type():

python
print(type(amount))
print(type(category))

That prints <class 'float'> and <class 'str'>. Reaching for type() when something behaves unexpectedly saves a lot of guessing, because most confusing errors in Python come from a value being a different type than you pictured.

Reading what the user types

input() prints a prompt and waits for a line of text:

python
category = input("Category: ")
amount = input("Amount: ")

print("Recorded", amount, "for", category)

Run it and it works. Now try doing math with what you got back:

python
amount = input("Amount: ")
print(amount * 2)

Type 12.5 and you get 12.512.5, not 25.0. input() always returns a string, and multiplying a string by an integer repeats it. This is the single most common surprise for people starting out in Python.

Converting types

Convert the string into a number before doing arithmetic:

python
amount = float(input("Amount: "))
print(amount * 2)

float() parses the text into a number, so 12.5 becomes 12.5 and the multiplication does what you expect. Use int() when you want whole numbers only — but be aware int("12.5") raises an error rather than rounding, because it refuses to guess whether you meant 12 or 13.

Common mistake

float(input(...)) crashes with ValueError if someone types twelve or just presses Enter. That's fine for now — you'll catch it properly in part 6. Being aware the crash exists is enough at this stage; hiding it too early makes it harder to see what's happening.

Formatting output

String concatenation with + fails the moment a number is involved, because Python won't silently mix types:

python
print("Spent " + amount + " on " + category)

That raises TypeError: can only concatenate str (not "float") to str. An f-string handles it — prefix the string with f and put expressions in braces:

python
print(f"Spent {amount} on {category}")

Anything inside the braces is evaluated and converted to text, so the type mismatch disappears. You can format numbers inline too:

python
print(f"Spent ${amount:.2f} on {category}")

The :.2f part means "as a fixed-point number with two decimals", turning 12.5 into 12.50. For money that's not cosmetic — it stops 9.1 and 9.10 from looking like different amounts in a list.

Where the script stands

python
category = input("Category: ")
amount = float(input("Amount: "))
note = input("Note: ")

print(f"Recorded ${amount:.2f} for {category} ({note})")

One expense, held in three separate variables, forgotten the moment the script exits. In the next part you'll group those three values into a single object and keep a list of them.

VK

Vijay Kumar

Founder of TechPurAI — writing hands-on tutorials and honest tool breakdowns.

LinkedIn ↗
← previous1. Installing Python and your first scriptnext →3. Lists and dictionaries