A list of small examples of code concepts for the basics of Python

By the end of the course you will be able to:

  • Read and describe what they do.
  • Write your own examples.
  • Combine the concepts to do more complex things.

Note that this is a small subset of the full capabilities of Python.

Variable assignment and algebraic manipulation

import math

a = 3
b = a + 1
print(math.sqrt(a ** 2 + b ** 2))

Output?



In [1]:
import math
a = 3
b = a + 1
print(math.sqrt(a ** 2 + b ** 2))


5.0

Single conditional statements

import random

n = random.randint(0, 5)
if n == 4:
    print(n)
else:
    print(-1)

Output?



In [2]:
import random
n = random.randint(0, 5)
if n == 4:
    print(n)
else:
    print(-1)


-1

Repeated conditional statements

import random

n = random.randint(0, 5)
number_of_samples = 1
while n != 4:
    number_of_samples += 1
    n = random.randint(0, 5)

Output?



In [3]:
import random
n = random.randint(0, 5)
number_of_samples = 1
while n != 5:
    number_of_samples += 1
    n = random.randint(0, 5)
print(number_of_samples)


2

Functions and lists

import random

def obtain_samples(upper_limit=5):
    """
    Returns the random samples from 0 till 
    `upper_limit` until the `upper_limit` is sampled
    """
    samples = [random.randint(0, upper_limit)]

    while samples[-1] != upper_limit:
        samples.append(random.randint(0, upper_limit))

    return samples

print(obtain_samples(upper_limit=5))

Output?



In [4]:
def obtain_samples(upper_limit=5):
    """
    Returns the random samples from 0 till 
    `upper_limit` until the `upper_limit` is sampled
    """
    samples = [random.randint(0, upper_limit)]
    
    while samples[-1] != upper_limit:
        samples.append(random.randint(0, upper_limit))

    return samples

print(obtain_samples(upper_limit=5))


[0, 0, 2, 2, 3, 4, 4, 1, 2, 4, 3, 1, 3, 5]

Iteration

number_of_days = 12
number_of_gifts = 0
for day in range(number_of_days + 1):
    for gifts in range(day + 1):
        number_of_gifts += gifts
print(number_of_gifts)

Output?



In [5]:
number_of_days = 12
number_of_gifts = 0
for day in range(number_of_days + 1):
    for gifts in range(day + 1):
        number_of_gifts += gifts
print(number_of_gifts)


364

Recursion

def fibonacci(n):
    """
    Returns the nth fibonacci number using recursion.
    """
    if n in [0, 1]:
        return 1
    return fibonacci(n - 1) + fibonacci(n - 2)

print(fibonacci(5))

Output?



In [6]:
def fibonacci(n):
    """
    Returns the nth fibonacci number using recursion.
    """
    if n in [0, 1]:
        return 1
    return fibonacci(n - 1) + fibonacci(n - 2)

print(fibonacci(5))


8

Object oriented programming

class ImaginaryNumber:
    """
    A class for imaginary numbers of the form $a + i b$
    """
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def __add__(self, other):
        return ImaginaryNumber(self.a + other.a, self.b + other.b)

    def __mul__(self, other):
        return ImaginaryNumber(
            self.a * other.a - self.b * other.b, 
            self.b * other.a + self.a * other.b
        )
    def __repr__(self):
        return f"{self.a} + {self.b}i"

z1 = ImaginaryNumber(1, -2)
z2 = ImaginaryNumber(51, 0)
print(z1 * z2)

Output?



In [7]:
class ImaginaryNumber:
    """
    A class for imaginary numbers of the form $a + i b$
    """
    def __init__(self, a, b):
        self.a = a
        self.b = b
       
    def __add__(self, other):
        return ImaginaryNumber(self.a + other.a, self.b + other.b)
    
    def __mul__(self, other):
        return ImaginaryNumber(
            self.a * other.a - self.b * other.b, 
            self.b * other.a + self.a * other.b
        )
    def __repr__(self):
        return f"{self.a} + {self.b}i"
    
z1 = ImaginaryNumber(1, -2)
z2 = ImaginaryNumber(51, 0)
print(z1 * z2)


51 + -102i