Event 2 - 2025-10-15

Don't forget your homework, it's at the bottom of this page.

Great coders steal...

We did spend a bit of time of getting everybodys laptops up to speed. After that I exclaimed that we should practive the most important programming skill - stealing!
...so we took the code from the invitation and pasted into our code editors.
After trying to run this code, we learn that you shouldn't blindly trust code you find or get, not even from someone you trust, not even from Kenneth! ...or did he do this on purpose!?! ;)

If

We spoke about what is True and False in Python. Do your remember (or can you find out) what the things in this list are, True or False?
  • 0
  • 4
  • 400
  • "Some text"
  • ""

Identation

We spoke about how to tell Python what code should run for example if an if-statement is true... with indentation, that is spaces and tabs.

def function_name():

We spoke about functions, reusable code, and wrote a function to make the invitation work, and then added an argument to it
def is_missing_laptop(name):
    if name == "Kenneth":
        return True
    if name == "Bertil":
        return True
    return False
I then wrote a very naive function function to calculate how much tax you should pay on your early income. We looked at this table of grundavdrag and decided to only implement the few first income brackets to save time.
def get_grundavdrag(my_income):
    if my_income <= 24900:
        return 24900
    if my_income <= 58400:
        return  25000
    if my_income <= 58900:
         return 25100
    return 17300

def how_much_tax_on_yearly_pay(my_income):
    taxable_income = my_income - get_grundavdrag(my_income)
    if taxable_income < 0:
        taxable_income = 0
    skatt_stockholm = 0.33
    return taxable_income * skatt_stockholm

skatt1 = how_much_tax_on_yearly_pay(4000)
print("skatt för 4000", skatt1)
skatt2 = how_much_tax_on_yearly_pay(40000)
print("skatt för 40 000", skatt2)
skatt3 = how_much_tax_on_yearly_pay(400000)
print("skatt för 400 000", skatt3)

Homework

Your task for this time is to practice writing some functions on your own.

Part 1

# This function takes an argument, that is a number of years
# Your task is to calculate how many months this is
#
# Example years_to_months(3) gives 36
def years_to_months(number_of_years):
    return 0

Part 2

# Do some math to check is this year is a leap year!
# 
# How to Know it is a Leap Year:
# yes 	Leap Years are any year that can be exactly divided by 4 (such as 2020, 2024, 2028, etc)
#   	not 	but if it can be exactly divided by 100, then it isn't (such as 2100, 2200, etc)
#    	  	yes 	except if it can be exactly divided by 400, then it is (such as 2000, 2400)
#
# https://www.mathsisfun.com/leap-years.html
#
# Examples:
# is_this_a_leap_year(1999) gives False
# is_this_a_leap_year(2004) gives True
# is_this_a_leap_year(2100) gives False
# is_this_a_leap_year(2900) gives False
# is_this_a_leap_year(2000) gives True
# is_this_a_leap_year(2400) gives True
def is_this_a_leap_year(year):
    return False

Wise words?

Use ChatGPT, Google stuff etc.. but I strongly encourage you to not copy the code with Ctrl+c / Ctrl+v, but type it with the keyboard instead. This is to train your nervous system so you get to know how to format Python code without actively thinking about it.
2025-10-17Kenneth Hedmanprogrammingpython