Skip to content

Instantly share code, notes, and snippets.

@awesmubarak
Created November 29, 2024 13:56
Show Gist options
  • Save awesmubarak/400b05091ecd4a78dd795ae5c75f2684 to your computer and use it in GitHub Desktop.
Save awesmubarak/400b05091ecd4a78dd795ae5c75f2684 to your computer and use it in GitHub Desktop.

Style

https://google.github.io/styleguide/pyguide.html

  • Make sure your code lints with Ruff Linter
  • Include docstrings and comments to Google standard
  • Use appropriate function and variable names
  • Try to use type hints

Data structures

Lists

student_grades = [85, 92, 78, 90, 88]

Task 1

Find the average of the student grades.

Task 2

What about if there were multiple students,

class_grades = [
    [85, 90, 92],  # student 1
    [88, 82, 95],  # student 2
    [76, 89, 91]   # student 3
]

Find each average of each student

Task 3

Print the highest and lowest average.

Dictionaries

This is a dictionary:

dict_directions = {
  "foward": 10,
  "left": 10,
  "right": 10,
  "back": 0
}

This is how you get a value given a key:

value = dict_directions["forward"]

Task 1

Print out all of the movements the robot will take given that set of commands, e.g.

Forward
Forward
...

Task 2

Imagine this robot is moving on a grid starting at (0,0). Work out the coordinates of where the robot ends up.

Task 3

Given this coordinate, provide an optimised way to end up there.

Imports

from datetime import datetime, timedelta

# Example usage
due_date = datetime(2024, 3, 1)
return_date = datetime.now()

Task 1

Calculate how many days overdue a book is

Task 2

Calculate late fees (50p a day)

Classes

This is a class:

class BankAccount:
    def __init__(self, name: str, balance: float = 0):
        self.name = name
        self.balance = balance

    def deposit(self, amount: float):
        self.balance += amount

Task 1

Add a withdrawl method that also prevents the balance from dipping below 0.

Task 2

Create multiple accounts and move money between them

Task 3

Try to add interest rates (yearly), and history tracking (stretch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment