Python

2025

Decorators in Python

Python Decorators

Decorators are a way we can use higher order functions to modify other functions. Typically they are used to wrap a function with another function.


def hello(name="Ian"):
    def greeting(func):
        def wrapper(*args, **kwargs):
            print("A man is sitting at the bar, you go up and greet him.")

            if name == "Ian":
                print("My name is Ian, what's yours?")
            else:
                print(f"My name is {name}, what's yours?")

            print("The man glances your way momentarily, then moves to the other end of the bar.")
            result = func(*args, **kwargs)
            return result
        return wrapper
    return greeting

@hello("Alice")
def bar_talk():
    print("You start a conversation about the weather with the bartender instead.")

@hello()
def another_chat():
    print("You shrug your shoulders and ask the bartender about the local sports team.")


print("=== First Interaction ===")
bar_talk()

print("\n=== Second Interaction ===")
another_chat()

In the example above, you would get the following print out:

Read more →

Functions

What Are Functions?

Functions are blocks of code that perform a given task any time they are called.

A function could process one action a single time and move on to the next block of code in the script, or it could iterate through itself multiple times (infinitely if you wrote it that way).

In Python, your declare a function with the reserved keyword def - which defines the function.

Read more →

Types in Python

Type()

In Python you can check for what type of variable you are dealing with by using the type() formula. It takes one input and tells you what type of variable it is.

See below:

>>> num = 3
>>> num_str = "3"
>>> bool = True
>>> num_float = 3.0
>>> type(num)
<class 'int'>
>>> type(num_str)
<class 'str'>
>>> type(bool)
<class 'bool'>
>>> type(num_float)
<class 'float'>

In the example above, we see that the function returns what class the variable belongs to. This can be very useful if you are trying to debug existing code that has variables written somewhere other than where you are looking or trying to fix the bug.

Read more →

Pure Functions

Pure Functions

Pure functions are functions that return the same result every time, assuming they receive the same inputs. This is made possible by writing the function to be self-contained, meaning it doesn’t rely on variables outside it’s local scope.

Pure functions do not change the external state of the program. They are great for keeping clean code that is easy to debug, but you can’t write an entire program of pure functions, because it wouldn’t actually do anything.

Read more →

Variables

What Are Variables?

If you are brand new to coding, the term variables might not immediately register. It’s important to understand what a variable is, as most coding languages do use them.

So What Are Variables?

Variables are containers that store values, such as:

Numbers:

  • Integers (3)
  • Float (3.0)
  • String (“3”)

Text:

  • Strings are plain text and are wrapped in parenthesis to tell Python to interpret the text as text only. “For example, this would be a string”

Boolean:

Read more →

Hello, World!

Your First Bit of Code

Everyone starts with the first program that was ever ran on a computer with a visual interface:

Hello, World!

The purpose of the program is to tell the Python interperter to show “Hello World” on the screen.

Go to your terminal and type the following to check for Python:

python3 --version

If you get an error, be sure to install it. On MacOS, I recommend HomeBrew, but you can use your package installer of choice. For HomeBrew, do the following:

Read more →