On This Page
Prerequisites
This chapter builds on Chapter 2: Python, Git & Package Management. You should already be able to create a .py file, run it from the terminal using the python3 command, and recognize basic syntax elements like comments and the print() function. You also need a working Python 3.10+ installation and a code editor—VS Code is recommended. No prior experience with variables or data types is required; this chapter introduces them from scratch.
Learning Goals
-
Create variables with meaningful names
- Create variables with meaningful namesthat communicate intent and follow Python's naming conventions so that your code remains readable as projects grow beyond single-file scripts
- Apply snake_case naming for variables and constants, distinguishing between user_age and USER_AGE by understanding when each convention is appropriate
- Recognize which names are illegal in Python—names starting with digits, names containing hyphens, and names that collide with reserved keywords like class, return, and import
- Use assignment statements to bind names to values, and understand that Python variables are references to objects in memory rather than fixed storage containers
-
Work with strings, integers, floats, booleans
- Work with strings, integers, floats, and booleansto model real-world data accurately, selecting the correct type for each piece of information your program handles
- Construct strings using single quotes, double quotes, and triple quotes, knowing when each style prevents unnecessary escape characters
- Distinguish between int and float arithmetic, recognizing that dividing two integers with / always produces a float while // performs floor division and returns an integer
- Use boolean values True and False in logical expressions, and understand Python's truthiness rules where 0, 0.0, "", and None all evaluate to False
-
- Perform type conversionsbetween strings, numbers, and booleans so that your program can safely process user input, environment variables, and API responses that arrive as raw text
- Convert strings to integers and floats using int() and float(), and handle the ValueError that Python raises when the string does not represent a valid number
- Cast numeric values back to strings with str() for concatenation and logging, avoiding the TypeError that occurs when you try to concatenate a string with an integer directly
- Apply bool() to understand how Python coerces other types into boolean values, learning that non-zero numbers and non-empty strings evaluate to True
-
- Use print and f-strings for debuggingto inspect variable state at runtime, replacing guesswork with concrete evidence about what your code is actually doing at each step
- Write f-string expressions that embed variables and arithmetic directly inside curly braces, such as f"Total: {price * quantity}", eliminating the need for manual string concatenation
- Use format specifiers inside f-strings to control decimal precision (:.2f), padding (:<10), and thousands separators (:,) when displaying numeric output
- Combine print() with type() to display both a variable's value and its runtime type in a single line, a technique that quickly reveals type-mismatch bugs during development
Key Terminology
Variable
A name that references an object stored in memory, created through an assignment statement like **x = 10**.
Assignment operator
The **=** symbol that binds a variable name on the left to a value or expression on the right.
String
An immutable sequence of Unicode characters, declared with single quotes, double quotes, or triple quotes.
Integer
A whole number with no fractional component, represented by the **int** type and supporting arbitrary precision in Python.
Float
A floating-point number that stores decimal values using IEEE 754 double-precision, represented by the **float** type.
Boolean
A data type with exactly two possible values, **True** and **False**, used to represent logical conditions.
f-string
A formatted string literal prefixed with **f** that evaluates expressions inside curly braces **{}** at runtime and embeds their string representations directly into the output.
Type conversion
The explicit process of transforming a value from one data type to another using built-in functions like **int()**, **float()**, **str()**, or **bool()**.
Type inference
Python's ability to determine a variable's type automatically from the assigned value without requiring an explicit type declaration.
Snake case
The Python naming convention where words are lowercase and separated by underscores, such as **user_name** or **max_retry_count**.
Reserved keyword
A word that Python reserves for its own syntax and cannot be used as a variable name, such as **if**, **class**, **return**, and **import**.
String concatenation
The operation of joining two or more strings end-to-end using the **+** operator, which requires all operands to be strings.
Truthiness
The rule Python applies when evaluating a non-boolean value in a boolean context, where **0**, **0.0**, **""**, empty collections, and **None** are falsy and everything else is truthy.
Format specifier
A mini-language expression placed after a colon inside an f-string's curly braces to control output formatting, such as **:.2f** for two decimal places.
Dynamic typing
Python's type system in which a variable's type is determined at runtime and can change when you reassign the variable to a value of a different type.
TypeError
An exception Python raises when an operation receives an argument of the wrong type, such as attempting to add a string to an integer.
ValueError
An exception Python raises when a function receives an argument of the correct type but an inappropriate value, such as passing **"hello"** to **int()**.