Introduction to projects
Intermediate Python Module
Updated Dec 2025
PYTHON PROJECTS OVERVIEW
CYBERSECURITY & CRYPTOGRAPHY
SECTION 1
SECTION 2
SECTION 3
OBJECT-ORIENTED PROGRAMMING
CODING GAMES
1.1. “Plus or minus”: Coding a number guessing game
Goal: find correct number in minimal number of guesses
With Google colab, open Project 1.1 Number guessing game - Task Sheet.ipynb
SECTION 1: CYBERSECURITY
What are the most common passwords?
Darkweb:�- difficult to access�- need specific software �- anonymous, many illegal activities
Source: CNBC
1. 123456�2. 123456789�3. Qwerty�4. Password�5. 12345
16. 654321�17. 123321�18. Qwertyuiop�19. Iloveyou�20. 666666
6. 12345678�7. 111111�8. 1234567�9. 123123�10. Qwerty123
11. 1q2w3e�12. 1234567890�13. DEFAULT�14. 0�15. Abc123
[?] What kind of password might be common, but did not make it to this list? Why not?
Project 1.2 Password generator
Let’s build a tool that generates secure passwords.
How can you make a password hard to crack?
=> randomness
For this project, you will need the “random” module!
Your program should let the user choose the desired length of their password, then generate a string of this length which is a random combination of numbers and letters.
With Google colab, open 1.2. Password generator - Task sheet.ipynb
Project 1.3 Password cracker
Let’s try to build a tool that guesses common passwords!
During this, we can practice our list of lists :)
Project 1.4 Steganography
Steganography cont.
Image = list of pixels
We can identify each pixel in an image by x & y coordinate (2D space)
E.g. image[1][2] = third pixel of the second line
Each element is an integer between 0 and 255 (=how greyscale colours are represented)
How does a computer represent a number between 0 and 255?
=> needs 8 numbers to represent this one numbers in just zeros and ones
These 8 numbers (bits) are a byte
In a byte, we can store colour information OR text. How?
Text:
Image:
Each pixel has a grayscale value between 0 (white) and 255 (black)
This pixel value can be represented by a byte (=8 bits)
Another example…
So we can represent pictures in numbers ranging from 0 to 255
How do we turn this into 0s and 1s for the computer?
Moving from B&W to colour: how does it affect the complexity of image encoding?
→ We will proceed to work with grayscale files
Steganography cont.
We can encode numbers and text within a byte.
Example: �10000000 = 128�10000001 = 129�00000001 = 1
Which part of the byte is most important? The leftmost or rightmost?
Steganography = storing the message in the rightmost bits of a byte!
Before we start… installing CV2 package
Open your command prompt or terminal, and type:
pip install opencv-python (either in your notebook or in your terminal)
import CV2
If there are no errors, the installation is successful.
Understanding how an image works in Python
img[0] gives you the first column of pixel values;
img[1] gives you the second etc.
len(img) gives you the width (=number of columns)
len(img[0]) gives you the height (=number of rows)
Let’s start coding!
What you’ll have to do…
-> Now, you have to set the rightmost bit of each pixel to the bits of the message
-> Note: we provided you with a function that transforms a message (string) into a list of 0s and 1s which we will hide
Part 1: Decoding challenge - get all the passwords
With Google colab, open 1.4 Steganography - Task sheet.ipynb
Download the encrypted images: https://tinyurl.com/Py2images
Back up -> https://drive.google.com/drive/folders/1MjkfO-ichmc_Tmk4e2T4RUtF1fskFIEd?usp=sharing
Decoding hints
Create an empty string called “byte” to collect the bits you found�For each bit in the message:� check whether the length of the byte string is already 8� if it is, print(byte)� update the message string by chr(int(byte[::-1],2))� create a new empty byte string to store the next byte in
add the next bit to byte�print decoded message string
Encoding hints, Pt I
def to_bit_generator(msg): => this function is given� return result
call this function and input a message of your choice
read the image using cv2.imread as greyscale - try finding the parameter necessary for this; if you can’t find it I’ll give it to you!
You can now check the shape of your image using img.shape which returns (x,y)
x = width or length,, y = height. You can also get x using len(img)
We will want to go through this image column by column
Encoding hints, Pt 2
if hidden_message[counter]==0 and img[h][w]%2==1:� img[h][w] = img[h][w] - 1
if hidden_message[i]==1 and img[h][w]%2==0:� img[h][w] = img[h][w] + 1
SECTION 2: GAME DESIGN
Project 2.1. Rock-paper-scissor
Goal: create a game where the user plays rock, paper, scissors against the computer
Libraries: you will need to import the “random” module so you don’t know what your computer will pick. The function you will need is random.choice. Read about this function and the type of input it requires here: https://www.w3schools.com/python/ref_random_choice.asp
With Google colab, open 2.1. Rock paper scissors - Task sheet.ipynb
Discussing your algorithm
The basic program
I would structure this into 3 functions.
�1. a function that collects the input from the player and also asks the computer to make a random choice between the three options.
2. a function that takes the outputs of function (1) as inputs and checks who won. Here you will have to encode the rules of rock paper scissors. You will need “if” statements to write the conditions in which the user wins and in which ones he loses.
3. a function which calls function (1) and (2)
You can then call function 3 in a loop; this will determine how many rounds you play.
Bonus tasks
a) Create a variable that keeps score
b) Let the user set the number of rounds
c) Make the game run for a maximum number of rounds, then determine the overall winner
d) Count how often players chose each of the 3 options and print those statistics at the end.
e) Create a program where the computer plays against itself.
Project 2.2: Hangman game
Before we start with this project, we need to briefly revisit the theory part about data types.
There is a few we did not learn about yet:
Python data types
What data types in Python are “collections” of information?
Let’s have a closer look at dictionaries and sets!
Dictionaries
Dictionaries are used to store data values in key:value pairs.�
We can retrieve the “brand” attribute like this:
print(thisdict["brand"])
Dictionaries cannot have two items with the same key.
If you use the same key twice (e.g. “year”) with different values, the second will just overwrite the first.
Sets
Hangman project
Prerequisites: you need to import 2 modules, random & string
I would write two functions; the first is quite short and it basically just generates your word while the second does most of the work.
The first one I called getword, and in here I put the list of possible words, and then retrieve a target word using a function from the random module.
With Google colab, open 2.2. Hangman - Task Sheet.ipynb
Hangman hints, Pt. 1
The second one I called hangman, and the algorithm I implemented is:
- Set the number of lives the user has
- Get the target word
- Create a new variable that only creates unique elements of the target word
- create an empty set where i store the letters the user has already guessed
- Create a set containing the alphabet
- Start of the while loop: while the target_letter variable is longer than 0, and you have more than 0 lives, execute the following steps:
- Get the program to print the letter if it was already used and otherwise a “-”
- Print this word with “-” at the letters you havent guessed yet, and say that this is the current word
- Get the user input
- Now there is 3 options: Ii) either the user_letter is not contained in alphabet or used letters, or (ii) the user_letter already occured in used letters, or (iii) it was not a valid letter.
- If that letter occurs in the alphabet, and isn’t in the used.letters, add it to used_letters
- Also make sure that if that letter is a target letter, remove it from target_letters. Remember that we delete a letter from the target_letters set every time we find it, so that we exit the loop and consequently win once len(targetletters) == 0
- Else, remove a life, and inform the player that this letter is not in the word
Hangman hints, Pt. 2
Once you have exited the loop, you need to check whether the lives are 0 - in that case you died. If you exited the loop, but lives are not 0, you must have guessed the word! Congrats!
Print a message with the result, then you’re done :)
SECTION 3: OBJECT-ORIENTED PROGRAMMING
To get a first feeling for objects and their methods, we will use CodeCombat:�https://codecombat.com/play/dungeon Level 1-5�
Alternative: https://www.ozaria.com/play/chapter-1-sky-mountain?hour_of_code=true
After playing, we will discuss:
RECAP - What is an object?
An object is a data structure that can contain variables and functions.
It is often used to represent some concretes things in your code.
We call the variables of the object attributes and the functions methods.
RECAP - What is a class?
A class is a template for an object, a way to create an object from a model.
For example we can create a class for our bonus cube, and then from this class we can create several bonus cubes with different parameters with different sizes for example.
An object is called an instance of a class.
In summary:
= the basic unit of OOP. An object (dog) is an instance of a class (pet)�Each object has attributes and methods.
A class is a user-defined data type�It consists of data members and member functions which can be used by creating an instance of that class�E.g. class cars(): there is many cars with different names and brands, but all of them have some common properties - 4 wheels, speed limit, mileage range tc.�Here, car is the class, and wheels, speed limits, mileage are its properties�Similarly, animal or pet could be a class
Why would we do this?
The point of OOP is basically to structure code into logical, self-contained objects.
Functional vs. object-oriented programming
The advantage of object-oriented programming
With classes, we create a generalised “blueprint” of what e.g. an animal usually is, and apply that blueprint to each new animal we create.
Classes are the blueprints, and objects are the different instances of these classes.
In the blueprint, we specify attributes our object can have, and methods it can use.
Attributes
Methods
= functions within a class that change the state of an object by modifying the objects’ attributes
3.1. Programming with the Turtles library
Turtle = an object which we are controlling using methods
Bonus challenge: Make the turtle write your name!
Possible challenges…
With Google colab, open 3.1. Drawing shapes with the Turtles library - Task sheet.ipynb
3.2. Ulam’s spiral
Ulam’s spiral is constructed by writing the positive integers in a square spiral and specially marking the prime numbers.
Let’s do this together and see what happens!
After seeing the pattern for ourselves, let’s see what mathematicians have to say about this!
3.2. continued
Coding Ulam’s spiral in Python isn’t that easy, but if you want to give it a shot, we have an instruction handout for you!
Otherwise, I will provide you with the code, and I’ve made a list of questions I’d like you to answer about the code.
After that, you can customise it the way you want :)
Questions sheet: https://docs.google.com/document/d/1jRpZwu1rhA9facdXE09yz8ACSfzCkecILucJDx7eJCE/edit?usp=sharing
Let’s revisit how to write our own classes in Python!
Recap - this is how you can structure a class
Now let’s do this with concrete parameters
Message passing
A few last OOP concepts!
Core concepts of OOP: encapsulation
Core concepts of OOP: inheritance
Core concept of OOP: Polymorphism
This basically just means that while one class may inherit attributes from another, it can still implement its own methods, which results in the desired differences.
3.3. Tic-tac-toe
With this knowledge, let’s have a look at this AI-powered game of Tic-Tac-Toe! https://github.com/kying18/tic-tac-toe/blob/master/game.py
Let’s go through the code together and understand what it does!
How can you make the artificial player you’re going up against “smarter” or “dumber”?
Note: Notebook missing!