PythonBeginner 14 min Lesson 2 of 30

Day 2 — Variables and Data Types

What a variable really is, the core Python types, why 0.1 + 0.2 is not 0.3, and how to convert between types without breaking things.

Python · Lesson 2 of 30
0/30 done(0%)

What is it? #

A variable is a name that points at a value. When you write age = 30, Python creates the number 30 somewhere in memory and makes the name age point at it. Reassigning the name just points it somewhere else — the old value is thrown away once nothing points at it.

Every value in Python has a type, and the type decides what you can do with the value. You can subtract two numbers. You cannot subtract two pieces of text. Python will not guess what you meant; it raises an error instead.

The types you will use constantly are int (whole numbers), float (numbers with a decimal point), str (text), bool (True or False), and None (a deliberate "nothing here").

You never declare the type up front. Python works it out from the value you assign. That is convenient, and it is also why reading a variable name like data three screens later can be confusing — good names matter more in Python than in languages that write the type next to every variable.

Think of it like this #

Think of a variable as a luggage tag, not a box. The tag does not hold the suitcase; it just names it. Move the tag to a different suitcase and the first one is still there until the airport clears it away.

That is why two names can point at the same value, and why changing what one name points at does not affect the other.

Simple example #

An order in a shop needs a few different kinds of value: a quantity (whole number), a price (decimal), a customer name (text), and a flag for whether it is paid (true or false). Each one is a different type, and each type behaves differently when you combine it with others.

Code #

PYTHON
quantity = 3                 # int
unit_price = 249.50          # float
customer = "Arjun Mehta"     # str
is_paid = False              # bool
coupon = None                # nothing chosen yet

total = quantity * unit_price
print(type(quantity), type(unit_price), type(customer), type(is_paid))
print("Total:", total)

# Text + number needs a conversion
print("Order for " + customer + " costs " + str(total))

# f-strings do the conversion for you
print(f"Order for {customer} costs {total:.2f}")

# Floats are approximations
print(0.1 + 0.2)             # 0.30000000000000004

from decimal import Decimal
print(Decimal("0.1") + Decimal("0.2"))   # 0.2

How it works #

type(quantity) asks Python what kind of value a name currently points at. It is a quick way to check your assumptions when something behaves oddly.

quantity * unit_price mixes an int and a float. Python widens the int to a float and gives you a float back — 748.5.

The concatenation line shows the rule that trips up every beginner: + between two strings joins them, but + between a string and a number is an error. str(total) converts the number into text first.

The f-string on the next line does the same job with far less noise. Put an f before the quote and anything inside {} is evaluated and inserted. {total:.2f} also formats it to two decimal places, which is what you want for money on a screen.

0.1 + 0.2 printing 0.30000000000000004 is not a Python bug. Floats store numbers in binary, and 0.1 has no exact binary form — the same way 1/3 has no exact decimal form. The tiny error is inherent to the format. For money, use Decimal (or store paise/cents as integers) so the arithmetic is exact.

None is its own type. It means "no value on purpose", which is different from 0 or "". Checking it uses is: if coupon is None:.

Real-world use #

Type confusion is one of the top sources of real bugs. Data arriving from an HTML form, a CSV file or a JSON API is text, even when it looks like a number. "5" + 1 fails, and worse, "5" * 3 quietly gives you "555" instead of 15.

The float issue shows up in invoices and reports, where three lines of 0.1 somehow add up to 0.30000000000000004 and a total ends a paisa off. Teams that handle money settle this early: integers for the smallest unit, or Decimal throughout.

And None is everywhere — a database column with no value, a lookup that found nothing, a function that returned early. Handling it explicitly is most of what defensive Python looks like.

Common mistakes #

  • Assuming user input is a number. input() always returns text; convert it with int() or float() and handle the case where the conversion fails.
  • Using == to compare with None. Use is None — it is the check for identity, which is what "is this literally nothing" means.
  • Comparing floats for exact equality. if total == 0.3 can be false even when the value looks right. Compare with a small tolerance, or use Decimal.
  • Naming variables list, str, sum or type. That hides the built-in with the same name and breaks code later in the file.
  • Writing int("12.5"), which raises an error. Go through float() first if the text may have a decimal point.

Practice #

Write a script that stores a price as a float, a quantity as an int, and a customer name as text. Print a formatted line like Arjun Mehta — 3 x 249.50 = 748.50 using an f-string. Then add 0.1 + 0.2 == 0.3 as a print statement and explain the result in a comment.

Quick quiz

  1. 1. What does `type(3.0)` return?

  2. 2. Why does `"5" + 1` raise an error?

  3. 3. Which is the correct way to check for no value?

  4. 4. Why does `0.1 + 0.2` not equal `0.3` exactly?

  5. 5. What does an f-string do?

Summary

  • A variable is a name pointing at a value; the value carries the type, not the name.
  • The everyday types are int, float, str, bool and None.
  • Convert explicitly with `int()`, `float()` and `str()` — Python will not guess.
  • f-strings are the clean way to build text out of values.
  • Floats are approximate. Use Decimal or integer cents when money is involved.