beginner20 min

Python Syntax and Basic Types

Write your first Python statements and learn the four building-block types: int, float, str, and bool.

What you'll learn

  • Run simple Python statements using print()
  • Distinguish int, float, str, and bool values from one another
  • Explain how indentation defines a block of code in Python

Explanation

Python was built around a simple idea: code is read far more often than it is written, so it should look close to plain language. Before building anything real, it helps to know the small set of pieces every Python program is made from.

Statements and print(). A Python program is a list of statements, usually one per line, executed from top to bottom. The first one worth knowing is a call to print(), which sends whatever is inside its parentheses to the screen. It is how you "see" what a program is doing while you're learning.

Variables hold values. Writing age = 29 creates a variable named age and stores the value 29 in it. Python figures out the type of a value automatically from what you assign — you never have to declare it up front the way some languages require.

Four types you'll use constantly:

  • int — a whole number with no decimal point, like 29 or -4.
  • float — a number with a decimal point, like 1.68 or 3.0, used whenever a fractional value is possible.
  • str — text, wrapped in either single quotes ('hi') or double quotes ("hi"); both work the same way. An f-string, written f"Hello, {name}!", lets you drop a variable's value directly inside text.
  • bool — exactly one of two values, True or False, almost always produced by a comparison like age > 18.

You can always check what type a value is with the built-in type() function — very useful while you're still building intuition for which type is which.

Indentation is not decoration — it's syntax. Many languages group statements into blocks with curly braces { }. Python instead uses consistent indentation (a fixed number of spaces, conventionally four) to mark which statements belong together, such as the body of a function or the branch of an if. A line ending in a colon : announces "an indented block follows." Get the indentation wrong — mix tabs and spaces, or indent by an inconsistent amount — and Python raises an IndentationError before your program even starts running. This feels strict at first, but it also means Python code you read always visually matches its actual structure; there's no way for the visible indentation and the real logic to silently drift apart the way they sometimes can in brace-based languages.

Comments start with # and are ignored entirely when the program runs — they exist purely so humans (including future you) can leave notes.

None of this requires memorization through repetition alone; it becomes automatic the moment you start typing real code and let the interpreter's error messages guide you. The example and exercises below are designed to get that feedback loop started immediately.

Example

Four variables covering all four basic types, plus print() and type() used to inspect them.

name = "Ava"
age = 29
height = 1.68
is_learning = True

print("Name:", name)
print("Age in 5 years:", age + 5)
print("Height (m):", height)
print("Still learning?", is_learning)
print(type(age), type(height), type(is_learning))

Try it yourself

Change the name, age, and height values below, then press Run.

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Guided exercise

Guided exercise

The variable price_text holds a price as text. Convert it into a float named price, then create a bool named on_sale that is True when price is less than 30.

Checks: price is a float · price equals 24.99 · on_sale is a bool · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Independent exercise

Independent exercise

Describe a book using Python's basic types: title (str), pages (int), rating (float, 0.0-5.0), is_finished (bool). Then build summary as a single f-string sentence containing the title and the rating.

Checks: title is a non-empty str · pages is a positive int · rating is a float between 0.0 and 5.0 · is_finished is a bool · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Common mistakes

  • Writing a whole number with a decimal point by accident (or vice versa), which silently changes int to float or float to int.
  • Mixing tabs and spaces for indentation, which causes an IndentationError even though the code may look aligned in an editor.
  • Forgetting that comparisons like price < 30 produce a bool value themselves — you don't need an if statement just to get True or False.
  • Confusing the string "24.99" (text) with the number 24.99 (float) — they look similar but only one supports arithmetic.

Knowledge check

Knowledge check

1. What type is the value produced by 3 / 2 == 1?
2. Which of these is a valid way to define a float in Python?
3. What determines which statements belong to the same block in Python?
4. What does type(29) return?

Takeaway

Every Python value has one of a small set of basic types, and indentation — not punctuation — is what defines a block of code.

Summary

Python programs are statements executed top to bottom, built from four basic types (int, float, str, bool) and grouped into blocks purely through consistent indentation after a colon. print() and type() are the two functions you'll lean on constantly while learning.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.