Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Last updated: Monday 03 November 2025 @ 12:43:19

Learning Python

We will be doing this lab inconjuction with the Python lecture. Key concepts will be explained and you will put into practice.

0. Interpreter for Python

  • We should not need to do anything for the university machines to run python as this is already part of the PATH, you can run the following commands:

    Terminal

    which python
    python --version
    which pip
    pip --version
    

    Output

So let's make our first program.

Exercises 1. Hello World

  1. Frist we need to create a directory and file, call directory learning_python and create a file in side it called helloworld.py:

  2. Modify the files content to contain the following:

    Code

    def main():
        print("Hello World!")
        print("Goodbye World!")
    
    if __name__=="__main__":
        main()
    

Exercise 2: Variables and Data Types

Instructions:

  1. Create a new file called variables.py

  2. Create a variable called name and assign it your name as a string.

  3. Create a variable called age and assign it your age as an integer.

  4. Create a variable called height and assign it your height in meters as a float.

  5. Print each variable on a new line.

    Code

    name = "Your name"
    age = 25
    height = 1.75
    
    print(name)
    print(age)
    print(height)
    
  6. and then run it.

    Terminal

    python variables.py
    

    Output

    Your name
    25
    1.75
    

Exercise 3: Basic Operations

Instructions:

  1. Create a new file call it basicMaths.py

  2. Create two variables, a and b, and assign them any two numbers.

  3. Perform and print the results of addition, subtraction, multiplication, and division between a and b.

  4. Use the modulus operator to find the remainder of a divided by b and print the result.

    Code

    a = 10
    b = 3
    
    print("Addition:", a + b)
    print("Subtraction:", a - b)
    print("Multiplication:", a * b)
    print("Division:", a / b)
    print("Modulus:", a % b)
    
  5. and then run it.

    Terminal

    python basicMaths.py
    

    Output

    Addition: 13
    Subtraction: 7
    Multiplication: 30
    Division: 3.3333333333333335
    Modulus: 1
    

Exercise 4: Lists and Indexing

Instructions:

  1. Create a new file called lists.py

  2. Create a list called fruits that contains the following fruits: "apple", "banana", "cherry".

  3. Print the first fruit in the list.

  4. Add a new fruit "orange" to the list and print the updated list.

  5. Remove "banana" from the list and print the list again.

    Code

    fruits = ["apple", "banana", "cherry"]
    print("First fruit:", fruits[0])
    
    fruits.append("orange")
    print("Updated list:", fruits)
    
    fruits.remove("banana")
    print("List after removing banana:", fruits)
    
  6. and then run it

    Terminal

    python lists.py
    

    Output

    First fruit: apple
    Updated list: ['apple', 'banana', 'cherry', 'orange']
    List after removing banana: ['apple', 'cherry', 'orange']
    

Exercise 5: Conditional Statements

Instructions:

  1. Create a file called conditionals.py

  2. Write a program that checks if a number is positive, negative, or zero.

  3. Assign a value to the variable number and use if, elif, and else to print:

    • "Positive" if the number is greater than 0
    • "Negative" if the number is less than 0
    • "Zero" if the number is exactly 0.

    Code

    number = 5
    
    if number > 0:
        print("Positive")
    elif number < 0:
        print("Negative")
    else:
        print("Zero")
    
  4. and then run it.

    Terminal

    python conditionals.py
    

    Output

    Positive
    
  5. change 5 to 0 and run again

    Terminal

    python conditionals.py
    

    Output

    Zero
    

Exercise 6: Loops

Instructions:

  1. Create a file called loops.py

  2. Write a for loop that prints numbers from 1 to 5.

  3. Create a list of your favorite colors and use a for loop to print each color.

  4. Write a while loop that prints numbers from 10 down to 1.

    Code

    # For loop to print numbers from 1 to 5
    for i in range(1, 6):
        print(i)
    
    # For loop to print each favorite color
    favorite_colors = ["blue", "green", "red"]
    for color in favorite_colors:
        print(color)
    
    # While loop to print numbers from 10 down to 1
    n = 10
    while n > 0:
        print(n)
        n -= 1
    
  5. run the program

    Terminal

    python loops.py
    

    Output

    1
    2
    3
    4
    5
    blue
    green
    red
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    

Exercise 7: Functions

Instructions:

  1. Create a new file called functions.py

  2. Write a function called greet that takes a name as a parameter and prints "Hello, <name>!".

  3. Write a function square that takes a number as a parameter and returns its square.

  4. Test both functions by calling them with sample inputs.

    Code

    def greet(name):
        print(f"Hello, {name}!")
    
    def square(number):
        return number ** 2
    
    # Testing the functions
    greet("Alice")
    print("Square of 4:", square(4))
    

    Terminal

    $ python functions.py
    > Hello, Alice!
    > Square of 4: 16
    

Exercise 8: Dictionaries

Instructions:

  1. Create the a file called dictionaries.py

  2. Create a dictionary called student with keys: name, age, and courses. Assign appropriate values to each.

  3. Print the student's name and age.

  4. Add a new key graduation_year with a sample year and print the updated dictionary.

    Code

    student = {
        "name": "Your name",
        "age": 21,
        "courses": ["Subject1", "Subject2"]
    }
    
    print("Name:", student["name"])
    print("Age:", student["age"])
    
    student["graduation_year"] = 2025
    print("Updated student dictionary:", student)
    
  5. and run it.

    Terminal

    python dictionaries.py
    

    Output

    Name: Your name
    Age: 21
    Updated student dictionary: {'name': 'Your name', 'age': 21, 'courses': ['Subject1', 'Subject2'], 'graduation_year': 2025}
    

Pylings

Pylings, made by yours truly, is an interactive Python learning tool heavily inspired by the renowned Rustlings. It provides small, focused exercises to help you learn Python by fixing code snippets and experimenting with them.

Pylings is designed to help beginners and experienced developers alike improve their Python skills through hands-on practice. Each exercise covers core Python concepts such as variables, data structures, loops, and more. This includes reading and responding to compiler and interpreter messages!

  1. Got to https://github.com/CompEng0001/Pylings and give it a star

    • Fork the repo if you want too!
  2. If you haven't already download pylings from pip

    • py -m pip install pylings

    Gif of installation

    Tip

    For best experience run in Visual Studio Code

Running Pylings

  1. Once installed via pip or git, navigate to a directory of your choice...

    • ... and run:

      • py -m pylings init
    • or provide the path as an argument:

      • py -m pylings init --path path/to/initialise-pylings
    • If a directory already exists with the same name you can use:

      • py -m pylings init --force [--path path/to/initialise-pylings]

    Gif of initialisation

  2. Then you can launch pylings in the initialised directory

  • py -m pylings
    

Tip

Of course you could always add the following to your PATH, and you call pylings directly:

  • Windows

    • $HOME/AppData/Local/Programs/Python/Python313/
  • Linux/Unix

    • $HOME/.local/bin

Working environment

The exercises are sorted by topic and can be found in the subdirectory exercises/. For every topic, there is an additional README.md file with some resources to get you started on the topic.

We highly recommend that you have a look at them before you start.

Most exercises contain an error that keeps them from compiling, and it's up to you to fix it!