print("Hello, Colab!")Hello, Colab!
This lesson covers the building blocks: running code, doing arithmetic, storing values in variables, and understanding data types. Type these examples into Google Colab as you go.
Google Colab organises code into cells that you run independently.
+ Code to add a new cell.Shift + Enter to run it (or click the play button).Try it:
One thing to know: variables you create in one cell carry over to later cells. The order you run cells in matters. If something isn’t working, try running all cells from the top (Runtime > Run all).
Python works as a calculator.
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 5 / 2 |
2.5 |
// |
Floor division | 5 // 2 |
2 |
% |
Remainder | 5 % 2 |
1 |
** |
Power | 2 ** 3 |
8 |
Try this:
Python follows the standard mathematical order of operations: parentheses first, then exponents, then multiplication/division, then addition/subtraction. When in doubt, use parentheses to make your intent clear.
Variables store values so you can reuse them.
You create a variable with =, which means “assign” (not “equals”). The name goes on the left, the value on the right.
Variables become useful when you build on previous results:
Two rules:
snake_case). Names can’t start with a number.Compare:
Both produce the same result, but the second tells you what the numbers mean.
Python is case-sensitive: myvariable, MyVariable, and MYVARIABLE are three different names. Stick to snake_case and you won’t run into this.
Every value in Python has a type. The four you’ll use most:
int: Whole numbers like 5, -10, 0float: Decimal numbers like 5.0, -10.5str: Text (strings) like "hello", 'Madrid'bool: True or FalseYou can check a value’s type with type():
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Strings are text enclosed in quotes, either single (') or double ("):
If you need quotes inside a string, use the other kind as the outer quotes:
f-strings let you embed variables directly in text. Prefix the string with f and wrap variables in curly braces:
My name is Alice, and I am 25 years old.
You can put expressions inside the braces too:
You’ll use f-strings constantly. They’re the cleanest way to combine text and values.
Python won’t let you combine incompatible types:
If you need to combine types, convert explicitly:
(Or use an f-string, which handles the conversion for you.)
You can also convert strings to numbers when needed:
Integers and floats mix freely. Python promotes the result to a float:
If you get a TypeError, it almost always means you’re trying to combine types that Python doesn’t know how to mix. Check what types your variables actually are with type().
Create variables for your name, age, and a city you’d like to visit. Print a sentence using an f-string that reads: “My name is [name], I’m [age] years old, and I’d like to visit [city].”
Calculate how many minutes there are in a week. Use variables for each step and comments to explain your logic.
Convert 98.6°F to Celsius using the formula: C = (F - 32) × 5/9
Create two variables: num1 = "123" and num2 = "456". Add them together as strings, then convert them to integers and add again. Print both results. Why are they different?
Calculate the area and perimeter of a 5 × 3 rectangle. Use descriptive variable names and at least one comment.
Comments
Comments are notes in your code that Python ignores. Use
#to start one.Good comments explain why, not what. If the code already makes sense on its own, it doesn’t need a comment. If the reasoning behind a line isn’t obvious, a comment helps.
You don’t need to comment every line. Comment the parts that would confuse someone reading your code for the first time (including future you).