Python Basics — Programs, Variables, Data Types, and Operators
Summary
This chapter introduces the fundamental building blocks of every Python program: what a computer program is, how to store information in variables, and the core data types Python uses to represent numbers, text, and true/false values. You will learn Python's syntax rules, how indentation works, how to write helpful comments, and how to display output with the print statement. By the end of this chapter you will be able to write and run simple Python programs that store and display integers, floats, strings, and booleans.
Concepts Covered
This chapter covers the following 15 concepts from the learning graph:
- Computer Program
- Source Code
- Variable
- Data Type
- Integer
- Float
- String
- Boolean
- Python Syntax
- Indentation in Python
- Comment in Code
- Print Statement
- Arithmetic Operators
- Comparison Operators
- Assignment Operators
Prerequisites
This chapter assumes only the prerequisites listed in the course description.
Hi! I'm Monty.
Welcome to Learning MicroPython and Physical Computing! I'm Monty, a cheerful Python snake who loves building things with code. I wear little round glasses and carry a tiny Raspberry Pi Pico badge on my chest — because that little computer is what this whole course is about. I'll be popping into every chapter to help you along, but I don't show up randomly. I have exactly six jobs, and you'll learn to recognize me by which one I'm doing:
- Welcome you at the start of every chapter — just like I'm doing right now.
- Help you think when a new idea needs a moment to sink in.
- Give you tips — the handy tricks that experienced coders use all the time.
- Warn you gently about the spots where beginners often get tripped up.
- Encourage you when something feels hard — because hard things are worth doing.
- Celebrate with you at the end of each chapter when you've earned it.
That's it. If I'm not doing one of those six things, I'm not in the chapter. Let's build something amazing!
What Is a Computer Program?
A computer program is a list of instructions for a computer to follow. Think of it like a recipe. A recipe tells a chef exactly what to do, step by step. A computer program tells a computer exactly what to do, one instruction at a time.
Every program you write starts as source code — the text you type in a code editor. The computer reads your source code and carries out each instruction. In this course, you will write source code in a language called MicroPython. MicroPython is a version of Python designed to run on tiny computers called microcontrollers.
The microcontroller we use is the Raspberry Pi Pico. It costs about four dollars and fits in the palm of your hand. When you load your source code onto the Pico, it follows your instructions instantly.
Here is the simplest MicroPython program you can write:
1 2 | |
When you run this program, it shows:
1 | |
Here is what each part does:
# This program says hello to the world— a comment. Python ignores this line. It is a note for you to read.print("Hello, world!")— tells Python to display the text inside the parentheses on the screen.# print() shows text on the screen— another comment, explaining what that line does.
That is source code in action!
Variables — Labeled Boxes for Information
A variable is a place in the computer's memory where you can store a piece of information. You give it a name so you can find it later. Think of it like a labeled box. You write a name on the outside of the box, and you put something inside.
Key Idea: Variables Are Like Labeled Boxes
Imagine a physical box with the word "age" written on the side. Right now the box holds the number 12. Later you can open the box, take out 12, and put in 13. The box — the variable — stays the same, but the value inside changes. That is exactly how variables work in Python.
In Python, you create a variable by choosing a name and then using the = sign to put a value inside it. The = sign in Python does not mean "equals" the way it does in math — it means "store this value in this variable."
1 2 3 | |
Variable names should describe what they hold. age is a much better name than a. Anyone reading your code will immediately know what the variable is for.
You can change the value in a variable at any time:
1 2 | |
Data Types — What Kind of Information?
Every variable holds a specific data type — the kind of information stored inside. Python has four basic data types you will use in nearly every program.
Integer — Whole Numbers
An integer is a whole number with no decimal point. Integers can be positive, negative, or zero.
1 2 3 | |
Float — Decimal Numbers
A float is a number that has a decimal point. The name comes from the idea that the decimal point can "float" to different positions in the number.
1 2 3 | |
String — Text
A string is a sequence of characters — letters, numbers, spaces, and symbols. You always put strings inside quotation marks. Single quotes and double quotes both work.
1 2 3 | |
Boolean — True or False
A boolean is either True or False — nothing else. Booleans are useful when you want to track whether something is on or off, yes or no.
1 2 | |
Notice that True and False start with capital letters. Python is strict about this. Writing true in all lowercase will give you an error.
The following table summarizes all four basic data types:
| Data Type | What It Stores | Example Value | Python Name |
|---|---|---|---|
| Integer | Whole numbers | 12 |
int |
| Float | Decimal numbers | 3.14 |
float |
| String | Text in quotes | "Hello" |
str |
| Boolean | True or False | True |
bool |
Now try classifying values yourself. The MicroSim below gives you a mystery value and asks you to pick the correct data type.
Diagram: Python Data Type Explorer
Python Data Type Explorer MicroSim
Type: microsim
sim-id: python-data-type-explorer
Library: p5.js
Status: Specified
Bloom Level: Understand (L2) Bloom Verb: classify Learning Objective: Students can classify a given Python value as integer, float, string, or boolean by examining its form.
Canvas layout: - Top row: four labeled colored boxes representing the four data types (Integer = blue, Float = green, String = orange, Boolean = purple). Each box shows the type name and a one-line description. - Center: a large "mystery value" card showing a Python literal in large monospace font. - Bottom: a feedback message area and a "Next Value" button.
Visual elements:
- Integer box (blue): label "Integer", description "Whole number — no decimal"
- Float box (green): label "Float", description "Number with a decimal point"
- String box (orange): label "String", description "Text inside quotation marks"
- Boolean box (purple): label "Boolean", description "Only True or False"
- Mystery value card: shows values like 42, 3.14, "hello", True, -7, 'Pico', False, 0.5, "GP15", -2.71
Interactive controls: - Four clickable type boxes. On correct click: the box glows and a message says "Correct! [value] is an integer because it has no decimal point." - On incorrect click: the box shakes gently and a message says "Not quite — look for quotation marks, a decimal point, or True/False." - Button: "Next Value" — loads a new mystery value from the randomized list. - Running score display: "X correct out of Y attempts."
Data Visibility Requirements: - Stage 1: Mystery value shown in large font. - Stage 2: After student clicks, show the correct type name and a one-sentence reason. - Stage 3: Show running score.
Instructional Rationale: A classify interaction at Understand level is appropriate because students must distinguish four mutually exclusive categories. Immediate corrective feedback builds the mental model without passive animation.
Values to cycle through (randomized): 42, 3.14, "hello", True, -7, 'Pico', False, 0.5, 100, "GP15", False, -2.71
Implementation: p5.js. Four clickable rectangles for classification. createButton() for "Next Value". Feedback text rendered below the boxes. Score tracked in a variable.
Monty's Tip
Not sure whether to use a float or an integer? Ask yourself: "Will this number ever have a decimal?" If the answer is yes — even sometimes — use a float. The Pico's voltage is always 3.3, never exactly 3, so voltage = 3.3 is the right choice.
Python Syntax — The Rules of the Language
Every programming language has syntax — the rules for how to write code correctly. Python's syntax rules are strict. If you break a rule, Python will refuse to run your code and show an error message.
Here are the most important Python syntax rules for beginners:
- Write one statement per line.
- Use parentheses
()afterprintwhen you call it. - Put all text strings inside quotation marks.
- Spell
TrueandFalsewith a capital first letter. - Use underscores
_instead of spaces in variable names: writeled_pin, notled pin.
Comments
A comment is a note you write in your code for yourself and other readers. Python ignores comments completely — they do not affect how your program runs. You create a comment by putting a # (hash symbol) at the start of the note.
1 2 | |
Good comments explain WHY the code does something, not just WHAT it does. Get into the habit of writing comments now. Your future self will thank you.
Indentation
Indentation means adding spaces at the beginning of a line to show that it belongs inside a group of code. Python uses indentation to understand your program's structure. This is different from most other programming languages.
Most code editors — including Thonny, the one we recommend — add four spaces automatically when you press the Tab key.
1 2 3 4 5 | |
Watch Out for Indentation Errors!
Python is very particular about indentation. If you mix spaces and tabs, or use the wrong number of spaces, you will get an IndentationError. Always use four spaces — or press Tab once in Thonny. Never mix tabs and spaces on the same line level.
The print() Statement
The print statement (print()) is how you tell Python to show output on the screen. Put whatever you want to display inside the parentheses. You can print text, numbers, variables, or a mix of all three.
1 2 3 4 5 | |
Here is what each line above displays:
| Code | Output |
|---|---|
print("Hello, maker!") |
Hello, maker! |
print(42) |
42 |
print(3.14) |
3.14 |
print(True) |
True |
print("My age is", 12) |
My age is 12 |
You can also print the value stored in a variable:
1 2 3 4 | |
Operators — Performing Actions on Values
An operator is a symbol that performs an action on one or more values. Python has three groups of operators you will use in almost every program.
Arithmetic Operators
Arithmetic operators do math. Before you look at the table below, here is a quick note about the two less-familiar ones. Floor division (//) gives you the whole-number part of a division result. Modulo (%) gives you the remainder.
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 10 - 4 |
6 |
* |
Multiplication | 3 * 7 |
21 |
/ |
Division | 15 / 4 |
3.75 |
// |
Floor Division | 15 // 4 |
3 |
% |
Modulo (remainder) | 15 % 4 |
3 |
Here is a practical example using floor division and modulo. Imagine you have 24 LEDs and you want to arrange them in rows of 8:
1 2 3 4 5 6 | |
Try changing the numbers yourself using the MicroSim below. Adjust the two sliders and click an operator to see the result instantly.
Diagram: Arithmetic Operator Explorer
Arithmetic Operator Explorer MicroSim
Type: microsim
sim-id: arithmetic-operator-explorer
Library: p5.js
Status: Specified
Bloom Level: Apply (L3) Bloom Verb: calculate Learning Objective: Students can apply the six Python arithmetic operators to compute results and predict outputs for given inputs.
Canvas layout:
- Top area: Two sliders labeled "Number A" (range 0–20, default 10) and "Number B" (range 1–20, default 3, minimum 1 to prevent division by zero).
- Middle area: A 2×3 grid of six operator buttons: +, -, *, /, //, %. The currently selected operator is highlighted in a bright accent color.
- Bottom area: A result card showing the full Python expression and computed result in large monospace font. Below the result: a one-sentence plain-English explanation of the operator. For // and %, also show a pizza-sharing analogy: "10 pizza slices shared by 3 people: each person gets 3 slices (//), with 1 left over (%)."
Interactive controls: - createSlider() for Number A and Number B. Values update live as sliders move. - createButton() for each operator. Clicking selects that operator and recalculates immediately.
Default state: A = 10, B = 3, operator = +, showing 10 + 3 = 13.
Data Visibility Requirements:
- Stage 1: Show A, B, and the selected operator.
- Stage 2: Show the full Python expression: A op B = result.
- Stage 3: Show a plain-English explanation and (for ///%) the pizza analogy.
Instructional Rationale: Slider-driven Apply-level exploration lets students verify mental predictions immediately. Showing the expression alongside the plain-English explanation bridges symbolic notation and meaning. The pizza analogy makes floor division and modulo concrete for 10-year-olds.
Implementation: p5.js with createSlider() and createButton(). Result displayed as large centered text with explanation below. Operator buttons styled as colored rounded rectangles; selected one gets a distinct border highlight.
Comparison Operators
Comparison operators compare two values and always produce a boolean result — either True or False. You will use comparison operators any time your program needs to make a decision.
Here is the key rule to remember before looking at the table: a comparison always produces exactly one of two answers, True or False — never a number or text.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 3 |
True |
> |
Greater than | 10 > 7 |
True |
< |
Less than | 3 < 1 |
False |
>= |
Greater than or equal | 5 >= 5 |
True |
<= |
Less than or equal | 4 <= 3 |
False |
You Can Do This!
The == operator trips up almost every beginner at least once. Remember: a single = stores a value in a variable (assignment), and a double == checks whether two values are equal (comparison). One equals sign stores; two equals signs compares. You'll get this, coder!
1 2 3 4 5 | |
Assignment Operators
You have already used the basic assignment operator (=) to store values in variables. Python also has a set of shortcut assignment operators that update a variable's value in place.
| Operator | Meaning | Example | Same as writing |
|---|---|---|---|
= |
Assign | x = 5 |
— |
+= |
Add and assign | x += 3 |
x = x + 3 |
-= |
Subtract and assign | x -= 2 |
x = x - 2 |
*= |
Multiply and assign | x *= 4 |
x = x * 4 |
/= |
Divide and assign | x /= 2 |
x = x / 2 |
These shortcuts are used constantly in hardware projects. For example, counting how many times a button has been pressed:
1 2 3 4 | |
Using button_count += 1 is shorter and easier to read than writing button_count = button_count + 1 every time.
Putting It All Together
Here is a short program that uses all 15 concepts from this chapter. Read each comment before you type it in — the comments are there to help you understand what every line does.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
When you run this program, you will see:
1 2 3 4 5 6 7 8 | |
Key Takeaways
Before you move on to Chapter 2, make sure you can answer each of these questions:
- What is a computer program? A list of instructions for a computer to follow.
- What is source code? The text you type that the computer reads.
- What is a variable? A named place in memory that stores a value.
- What are the four basic data types? Integer, float, string, and boolean.
- What does
#do in Python? It starts a comment — Python ignores it. - What is the difference between
=and==?=stores a value;==checks if two values are equal.
Quick Check: What data type is each value? (Click to reveal the answers)
42→ integer — a whole number with no decimal point3.14→ float — it has a decimal point"hello"→ string — it is text inside quotation marksTrue→ boolean — onlyTrueorFalseare booleans-7→ integer — whole numbers can be negative"42"→ string — the quotation marks make it text, not a number
Great Work, Maker!
You just learned the foundation of every Python program — variables, data types, syntax, comments, print, and operators. These are the building blocks you will use in every single project in this course. In Chapter 2, you will put them to work with if statements and loops so your programs can make decisions and repeat actions. You've got this, coder!