Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

PY
Python
0/14 completed
Beginner
Hello PythonVariables and typesWorking with stringsConditions and loopsFunctions
Intermediate
Lists, tuples, dicts and setsFiles and exceptionsModules and the standard library
Advanced
Classes and OOPIterators and generatorsDecorators and context managers
Pro
Async PythonTesting and type hintsVirtual environments, packages and real projects
Tracks / Python / Beginner

Variables and types

A variable is a name that refers to a value. You create one with =:

Python · editable

There's no special keyword and no type declaration: Python works out the type from the value. You can also reassign a variable at any time:

Python · editable

Naming rules

  • Letters, digits and _, but not starting with a digit: total_2, ✅ 2total ❌
  • Case-sensitive: name and Name are different variables
  • Convention: snake_case for variables and functions (first_name, max_speed)
  • UPPER_CASE signals a constant you don't intend to change: MAX_USERS = 100
  • Choose meaningful names: price_with_tax, not p2

The basic types

Type Example Meaning
int 42, -7, 1_000_000 Whole numbers (any size!)
float 3.14, 2.0, 1e6 Decimal numbers
str "hello", 'Python' Text (a "string" of characters)
bool True, False Yes/no values
NoneType None "No value here"

type() tells you what something is:

Python · editable

Floats are approximate

Computers store decimals in binary, so some can't be represented exactly. This surprises everyone once:

Python · editable

For money, use whole cents (int) or the decimal module.

Converting between types

Python · editable

Mixing text and numbers without converting is an error, because Python won't guess what you meant:

Python · editable

Fix it with str(age), or better, an f-string: f"I am {age} years old".

Truthy and falsy

Every value can be treated as True or False. These are falsy: False, None, 0, 0.0, "" (empty string), [], {}, (). Everything else is truthy:

Python · editable

Comparison and logic

Python · editable

Note that = assigns and == compares.

Multiple assignment

Python · editable

None: "nothing yet"

Python · editable

Practice

  1. Create variables for a product's name, price (float) and quantity (int).
  2. Calculate the total, and print it with an f-string, rounded to 2 decimals: f"{total:.2f}".
  3. Convert the string "250" to a number, add 50, and print the result.
Python · editable
✎ Check your understanding

1.What is the type of 7 / 7?

2.Which of these is falsy?

3.How do you swap the values of a and b in Python?

← Hello Python