In this section, we’ll start with the basics of Python, one of the most widely used programming languages today. Python is also the foundation for tools like Qiskit, which allow us to program quantum computers. For now, we’ll keep it simple and focus on the key concepts and basic syntax, without going too deep into the details.
Control flow decides which statements run and how many times they run. The main tools are loops (while, for) and conditionals (if/elif/else).
A loop that repeats code for each item in a sequence (like a list or string) or a range of numbers. Use when you know the number of repetitions or want to process items one by one.
A loop that repeats code for each item in a sequence (like a list or string) or a range of numbers. Use when you know the number of repetitions or want to process items one by one.
Checks conditions and chooses which instructions to run. Use when the program needs to make decisions.
Stops a loop immediately, even if the condition is still true or items remain. Use when you want to exit early from a loop.
A named set of instructions you can run whenever needed. Use to reuse code, give a clear name to a task, and avoid repetitions.
Scope describes where a variable can be accessed.
Local variable: created inside a function and usable only inside that function.
Global variable: created outside any function and accessible throughout the file (unless shadowed by a local variable).
“Containers” that store and organise multiple values. They differ by:
Order: whether items keep a fixed sequence.
Mutability: whether items can be changed after creation.
Duplicates: whether repeated values are allowed.
Ordered: items keep the sequence they were added.
Mutable: items can be added, removed, or changed.
Indexed: each item is accessed by a numeric position (0, 1, ...).
Duplicates allowed: the same value can appear multiple times.
Use when you need to store multiple values in order and update them.
Ordered: items keep the sequence they were added.
Immutable: items cannot be added, removed, or changed.
Indexed: each item is accessed by a numeric position (0, 1, ...).
Duplicates allowed: the same value can appear multiple times.
Use when you need a fixed group of values.
Unordered: items do not keep a fixed sequence.
Mutable: items can be added or removed, but not changed in place.
Unindexed: items cannot be accessed by position.
No duplicates: each value appears only once.
Use when you need no duplicates or to check if something exists quickly.
Dictionaries store information in key–value pairs. Think of them like a real-world dictionary: the word is the key, and the definition is the value. Use when you need to look up values by a name (key).
Keys
Unique: every key must be different; duplicate keys are not allowed. Assign a value to an existing key replaces the previous value.
Immutable type: keys must be of an immutable (unchangeable) type, such as strings, numbers, or tuples containing only immutable elements.
Not changeable in place: once a key exists, you cannot change it directly. To “change” a key, you must remove the old key–value pair and add a new one with the desired key.
Values
Duplicates allowed: multiple keys can point to the same value.
Any data type: values can be of any type (numbers, strings, lists, other dictionaries, etc.).
Mutable: values can be changed at any time. Assigning a new value to an existing key replaces the previous value.
Dictionary as a whole is mutable: you can add new key–value pairs, change values, or remove pairs.
Access by key: each key acts as a unique label that lets you quickly look up its associated value. Values are not accessed by numeric position.
Errors stop a program from running. Exceptions let us catch those errors so the program doesn’t crash.
Common Python errors:
ValueError → occurs if a function receives a value of wrong type.
Example: int("hello") → can’t turn text into a number.
TypeError → occurs if an operation tries to perform an action with an unexpected data type.
Example: 5 + "hello" → can’t add number and string.
IndexError → occurs when you use an index on a sequence, like a list or a tuple, and the index is out of range.
Example: [1, 2, 3][5] → list has no item at index 5.
Handles errors so the program does not crash. Use when something might go wrong (like dividing by zero or invalid input).
A template for creating objects with data (attributes) and actions (methods). Use to model real-world things in code.
Instead of writing everything from scratch, you can import ready-made code.
Module: A single Python file (.py) that contains functions, classes, or variables.
Library: A collection of modules grouped together.
Use modules and libraries to reuse code written by yourself or others, and to avoid rewriting common tasks.
Python uses the import statement to bring in modules and libraries. You can import:
Standard libraries (built into Python), e.g.
math → mathematical functions
random → random number generation
Third-party libraries (installed separately), e.g.
qiskit → IBM’s quantum computing tools
scikit-learn → machine learning algorithms
Your own modules — just save your code in a .py file and import it in another script.
With these tools, you can start building simple projects, and you are ready to explore more advanced areas like data science or quantum computing with Qiskit.