Python 30‑by‑30 Course
Welcome to your first week! We'll start by setting up your coding workshop and learning the absolute basics. Each day is a short, focused session. Just give it 30 minutes, and you'll be amazed at what you can do.
Every new journey has a first step. Today, we're setting up your coding environment. Think of it as preparing your workshop. First, head over to python.org and download the latest stable release for your system (Windows, macOS, or Linux). When you install it, especially on Windows, make sure to check the box that says "Add Python to PATH." This little step saves a lot of headaches later!
Once installed, open your terminal (or Command Prompt) and type python3 --version
. If you see a version number, you're all set! Now, type python3
and hit Enter. You'll see a >>>
prompt. This is the REPL (Read-Eval-Print Loop), and it's your new best friend. It’s like a direct chat with Python. Try some maths like 2 + 2
or 5 * 10
. The REPL is perfect for quick experiments without having to save a file.
While the REPL is great for testing, most of your work will live in scripts. Open any simple text editor (like VS Code, Sublime Text, or even Notepad) and create a file named hello.py
. Inside, type this one line: print('Hello, world!')
. Save it. Now, in your terminal, navigate to where you saved the file and run it with python3 hello.py
. You just ran your first program! Let's make it interactive. Add these lines: name = input('What is your name? ')
and then print('Hello, ' + name + '!')
. You're now taking input and using a variable (name
) to store it. This simple cycle—edit, run, repeat—is the core of all programming.
Try making a script that asks for the user's favourite food and then replies with "Oh, I love [food] too!". Use an f-string (f"Hello, {name}!"
) for a cleaner way to print. Can you also ask for their age and tell them what year they'll turn 100?
# hello_age.py
import datetime
name = input("What is your name? ")
age = int(input("How old are you? ")) # input() gives text, int() turns it into a number
current_year = datetime.date.today().year
year_turn_100 = current_year - age + 100
print(f"Hello, {name}! You will turn 100 years old in {year_turn_100}.")
Python is pretty smart; it usually figures out what kind of data you're using without you having to tell it. Today, we'll focus on the two most common types: numbers and text. Python has two main types of numbers: integers (whole numbers like 10, -5, 0) and floats (numbers with a decimal point like 3.14 or -0.5). You can do all the usual maths with them: +
, -
, *
(multiply), /
(divide), and even **
for exponents (like 2 ** 3
is 8).
Text in Python is called a string, and you create one by putting quotes around it, like 'Hello'
or "Python is fun!"
. Strings can't be changed in place (they're "immutable"), but you can create new ones from them. You can join them with +
('snow' + 'ball'
becomes 'snowball'
) or repeat them with *
('ha' * 3
becomes 'hahaha'
). Strings also have handy built-in tools called methods. For example, 'Hello'.lower()
gives you 'hello'
, and ' some text '.strip()
removes the extra spaces.
One of the most important skills is converting between types. The input()
function always gives you a string, even if the user types a number. So, if you want to do maths with it, you need to convert it first using int()
or float()
. For example, age = int(input("Age? "))
. To put a number into a sentence, you can convert it back to a string with str()
, but it's much easier to use an f-string: f"You are {age} years old."
. The f-string automatically handles the conversion for you!
Write a simple tip calculator. Ask for the bill amount and the percentage of tip you want to leave. Calculate and print the tip amount and the total bill. Make sure to format the output to show exactly two decimal places (hint: use an f-string like f"{total:.2f}"
).
# tip_calculator.py
bill_amount_str = input("What is the total bill? $")
tip_percentage_str = input("What percentage tip would you like to give? ")
# Convert strings to numbers (floats) to do math
bill_amount = float(bill_amount_str)
tip_percentage = float(tip_percentage_str)
tip_amount = bill_amount * (tip_percentage / 100)
total_bill = bill_amount + tip_amount
# Use an f-string with formatting to show two decimal places
print(f"Tip amount: ${tip_amount:.2f}")
print(f"Total bill: ${total_bill:.2f}")
A lot of programming is about making decisions. Should the player get a bonus? Is the user's password correct? To answer these questions, Python uses a special type called a boolean. It has only two possible values: True
or False
. You get a boolean whenever you compare things. For example, 5 > 3
is True
, and 10 == 20
(notice the double equals ==
for comparison) is False
.
We can also combine these questions using logical operators: and
, or
, and not
. For example, to check if an age is suitable for a teen movie, you might write age >= 13 and age <= 18
. This whole expression will only be True
if both parts are true. Python is smart about this: if the first part of an and
is false, it doesn't even bother checking the second part!
The real power comes when you use these booleans with an if
statement. This lets your code take different paths based on a condition. The structure is simple: if a condition is true, do something. You can add an elif
(short for "else if") to check another condition, and an else
to catch everything that didn't match the previous conditions. Remember, the indentation (the spaces at the start of the line) is how Python knows which code belongs to which block. It's super important!
if condition:
# This block runs if the condition is True
elif another_condition:
# This runs if the first was False, but this one is True
else:
# This runs if nothing above was True
Write a program that asks for a temperature in Celsius. If the temperature is above 30, print "It's a hot day!". If it's between 15 and 30, print "It's a lovely day." Otherwise, print "It's cold, you might need a jacket."
# weather_checker.py
temp_celsius = float(input("Enter the temperature in Celsius: "))
if temp_celsius > 30:
print("It's a hot day! Don't forget sunscreen. ☀️")
elif 15 <= temp_celsius <= 30:
print("It's a lovely day. Perfect for a walk! 🌳")
else:
print("It's cold, you might need a jacket. 🧥")
So far, we've stored one piece of information in a variable at a time. But what if you have a collection of things, like a to-do list or a group of names? For that, we use a list. A list is an ordered, changeable collection of items, written inside square brackets []
. For example: tasks = ["buy milk", "walk the dog", "learn Python"]
.
You can access any item by its position, or index, which starts at 0. So tasks[0]
would give you "buy milk"
. Lists are flexible (or "mutable"). You can add items to the end with .append()
, remove them with .remove()
, or change an existing item like tasks[1] = "feed the cat"
. A for
loop is the perfect way to go through each item in a list and do something with it: for task in tasks: print(task)
.
Sometimes you have a collection that you know should never change, like the coordinates of a point (x, y). For this, Python gives us tuples. They are just like lists, but you create them with parentheses ()
and—this is the key part—they are immutable (unchangeable). Once you create point = (10, 20)
, you can't change what's inside. This makes your code safer and signals to other programmers (or your future self!) that this data is meant to be constant.
Create a list of your favourite hobbies. Use a for
loop to print out a sentence for each one, like "I enjoy [hobby]". Then, add a new hobby to the list using .append()
and print the whole list to see the change.
# hobbies_list.py
hobbies = ["Reading", "Hiking", "Coding"]
print("My current hobbies are:")
for hobby in hobbies:
print(f"- I enjoy {hobby}.")
# Let's add a new hobby
new_hobby = "Playing guitar"
hobbies.append(new_hobby)
print("\nI've picked up a new hobby! Now my list is:")
print(hobbies)
Lists are great for ordered items, but what if the data has a natural connection, like a word and its definition, or a person and their phone number? For this, we use a dictionary. A dictionary stores key-value pairs. You create them with curly braces {}
. For example: contact = {"name": "Jane Doe", "phone": "555-1234"}
. Here, "name"
is a key, and "Jane Doe"
is its value.
You look up values using their key, not an index: contact["phone"]
would give you "555-1234"
. It's super fast! Adding or changing an item is easy: contact["email"] = "jane@example.com"
. Dictionaries are incredibly useful for organising any kind of structured information, from user profiles to game settings.
Finally, let's talk about sets. A set is also made with curly braces, but it's an unordered collection of unique items. If you add the same item twice, it only gets stored once. This makes sets perfect for two things: quickly removing duplicates from a list, and performing membership tests (checking if an item is in the set). You can also do cool things like find the items that two sets have in common (intersection) or the items that are in one set but not the other (difference).
Create a dictionary to represent a simple user profile with keys for 'name', 'city', and 'favourite_song'. Print a sentence that uses all three values. Then, create two lists of numbers with some duplicates. Convert them to sets to find which numbers appear in both lists.
# dict_set_examples.py
# Dictionary practice
user_profile = {
"name": "Alex",
"city": "Lisbon",
"favourite_song": "Bohemian Rhapsody"
}
print(f"{user_profile['name']} from {user_profile['city']} loves listening to {user_profile['favourite_song']}.")
# Set practice
list1 = [1, 2, 3, 4, 5, 5]
list2 = [4, 5, 6, 7, 8, 4]
set1 = set(list1)
set2 = set(list2)
print("\nOriginal list 1:", list1)
print("Original list 2:", list2)
# The '&' operator finds the common items (intersection)
common_numbers = set1 & set2
print("Common numbers found in both lists:", common_numbers)