1 of 57

Introduction to projects

Intermediate Python Module

Updated Dec 2025

2 of 57

PYTHON PROJECTS OVERVIEW

CYBERSECURITY & CRYPTOGRAPHY

  • Intro to password security
  • Random password generator
  • Password cracker
  • Steganography

SECTION 1

SECTION 2

SECTION 3

OBJECT-ORIENTED PROGRAMMING

  • Intro with CodeCombat
  • Theory recap
  • Turtles library, drawing shapes
  • Predicting prime numbers with Ulam’s spiral
  • Code understanding: Tic-tac-toe*

CODING GAMES

  • Rock-paper-scissor
  • Introducing Python data types for collections
  • Hangman
  • Our first project: Number guessing game�Objectives include learning how to make a new project, how to run it, practicing Python concepts

3 of 57

1.1. “Plus or minus”: Coding a number guessing game

  1. Computer chooses a random integer between 0 and 5000
  2. User also chooses an integer and gives it to the program
  3. Program tells player if the number entered was higher or lower than the correct number

Goal: find correct number in minimal number of guesses

With Google colab, open Project 1.1 Number guessing game - Task Sheet.ipynb

4 of 57

SECTION 1: CYBERSECURITY

5 of 57

What are the most common passwords?

  • Common password => easy for a hacker to guess
  • We want to make sure our own password is not in this list
  • But how would we have data on this? => data breaches, which are then uploaded to the darkweb

Darkweb:�- difficult to access�- need specific software �- anonymous, many illegal activities

6 of 57

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?

7 of 57

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

8 of 57

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 :)

  • Make lists of some common words that could be found in passwords
  • Also collect user inputs on things that are commonly used in passwords - their name, age, mum’s name, pet name etc, and store the answers in a list
  • Randomly combine these to create likely passwords
  • Let your neighbour come up with a rather unsafe password and see whether your program can crack it! Then ask them to think of something safer!

9 of 57

Project 1.4 Steganography

  • Steganography is a form of cryptography (=secure communication techniques that allow only the sender and intended recipient of a message to view its content)
  • Concealing a message within another message
  • Benefit: doesn’t look encrypted, doesn’t arouse suspicion
  • Steganography does not only protect the content, but also conceals the fact that a secret message is being sent

10 of 57

11 of 57

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

12 of 57

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)

13 of 57

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?

14 of 57

Moving from B&W to colour: how does it affect the complexity of image encoding?

→ We will proceed to work with grayscale files

15 of 57

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!

16 of 57

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.

17 of 57

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)

18 of 57

Let’s start coding!

What you’ll have to do…

  • Part 1: Decryption�-> Add image into your project�-> Load it into your code�-> Loop over pixels and take the most right bit�-> Once 8 bits have been collected, transform to int, then to character
  • Part 2 (bonus): Encryption

-> 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

19 of 57

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

20 of 57

Decoding hints

  • Load encrypted image (cv2.imread)
  • Create an empty “message” list to store elements of the message in
  • Create an empty string to store the message in
  • For-loop through rows of the image:
    • For loop for pixels in each row
      • append to the list “message” the following: str(pixel & 0x01)

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

21 of 57

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

22 of 57

Encoding hints, Pt 2

  • Create a counter, set to zero
  • Loop through the image, column by column => for h in,..
  • If counter is equal to, or larger, than length of the secret message, stop encoding
  • In a nested loop through each row =>for w in…. => this will allow you to access each pixel
  • Again check if counter is equal to, or larger, than the length of secret message => if yes, also exit loop
  • if not, this is the code you need to write the message into the least significant bit:

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

  • Increase counter by 1
  • Write the image using cv2.imwrite

23 of 57

SECTION 2: GAME DESIGN

24 of 57

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

25 of 57

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.

26 of 57

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.

27 of 57

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:

  • tuples
  • dictionaries
  • sets

28 of 57

Python data types

  • To store text: strings
  • To store numbers: integers, floats
  • To store true/false information: Booleans
  • To store binary information: bytes
  • To store ordered sequences: lists, tuples
  • To store sets of values: sets
  • To store key:value mappings: dictionaries

29 of 57

What data types in Python are “collections” of information?

  • There are 4 collection data types in the Python programming language �=> so far we have learned about lists
  • Lists are ordered, changeable, and allow duplicate values.�We create them like this: mylist = [“apple”, “banana”, “peach”, “apple”]
  • Tuples are ordered, unchangeable, and allow duplicate values.�We create them like this: mytuple = (apple, banana, peach, apple)
  • Sets are unordered, unchangeable (but you can add/remove), and unindexed�We create them like this: myset = {“apple”, “banana”, “cherry”}
  • Dictionaries are used to store data values in key:value pairs.�Dictionaries are ordered, changeable, and do not allow duplicates.�

Let’s have a closer look at dictionaries and sets!

30 of 57

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.

31 of 57

Sets

  • Sets are collections of unique items, i.e. there can be no duplicates.
  • This is helpful, for example, if we want to remove duplicate values.
  • We can transform a list into a set using set()
  • E.g. names = [“John”, “Henry”, “Anna”, “John”, “Anna”]
  • names = set(names)
  • If we now print(names), the output will be “John”, “Henry”, “Anna”

32 of 57

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

33 of 57

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

34 of 57

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 :)

35 of 57

SECTION 3: OBJECT-ORIENTED PROGRAMMING

36 of 57

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:

  • In Codecombat, what is an example of an object you saw?
  • What is an example of a method you used?
  • Do you see the advantages of using objects and methods?

37 of 57

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.

38 of 57

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.

39 of 57

In summary:

  • OBJECTS

= the basic unit of OOP. An object (dog) is an instance of a class (pet)�Each object has attributes and methods.

  • CLASSES

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

40 of 57

Why would we do this?

  • Python works perfectly fine without packaging your stuff into objects and classes
  • However, using this framework is a prerequisite for advanced coding
  • OOP is faster and easier for the computer to execute
  • It makes code easier to modify and for multiple people to collaborate on it
  • It also makes it easier to reuse code, because similar classes can “inherit” from each other

The point of OOP is basically to structure code into logical, self-contained objects.

41 of 57

Functional vs. object-oriented programming

  • Functions directly map an input into an output
  • Let’s say you would want to create a farm of animals that have distinct appearance and behaviour
  • Have to program every animal from scratch
  • => a lot of repetition
  • => inefficient
  • => what if there was some mistake?

42 of 57

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.

43 of 57

Attributes

44 of 57

Methods

= functions within a class that change the state of an object by modifying the objects’ attributes

45 of 57

3.1. Programming with the Turtles library

Turtle = an object which we are controlling using methods

46 of 57

Bonus challenge: Make the turtle write your name!

Possible challenges…

47 of 57

With Google colab, open 3.1. Drawing shapes with the Turtles library - Task sheet.ipynb

48 of 57

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!

Video 1: https://www.youtube.com/watch?v=iFuR97YcSLM

Video 2: https://www.youtube.com/watch?v=3K-12i0jclM

49 of 57

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

50 of 57

Let’s revisit how to write our own classes in Python!

Recap - this is how you can structure a class

51 of 57

Now let’s do this with concrete parameters

52 of 57

Message passing

  • Objects communicate with each other by sending and receiving info to each other
  • To do this, a function in the receiving object needs to be called that generates the desired results
  • Message passing involves specifying the name of the object, the name of the function, and the information to be sent

53 of 57

A few last OOP concepts!

54 of 57

Core concepts of OOP: encapsulation

  • The properties and methods of an object are hidden from other objects
  • There is private variables, which can never be changed by other objects (e.g. in the car example, colour, year, and model), and public ones (e.g. the method “start”, which can be called by the object “person” to start the car)
  • Example 1: role-playing game; hero has different attributes and methods. Some of these are private (name, skin colour) while others are public (health => can be modified by enemy)
  • Example 2: imagine a company with different sections (finance, accounting, sales etc).
  • They all have their own methods of handling their transactions and storing that data.
  • If one wants to have info from the other, they cannot directly access it.
  • Rather, they need to call the division they need something from and request to get that particular data.
  • So different objects can interact, but only if you explicitly tell them to!

55 of 57

Core concepts of OOP: inheritance

  • We might want to create classes of objects that are similar
  • Do we now have to copy and paste all that code, and then start modifying that?
  • => No, due to inheritance
  • If you want to have a class of objects that are busses or vans, they can inherit attributes like year, colour and mode, as well as methods like start() or accelerate().

56 of 57

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.

57 of 57

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!