Calmcode - objects: introduction

Introduction

1 2 3 4 5 6 7 8 9 10

Let's say that you're interested in modelling a dice-roll in Python. Then you could do that with a function.

import random 

def roll_dice(sides, k=1):
    return random.choices([i+1 for i in range(sides)], k=k)

# Roll a dice with 6 sides 12 times
roll_dice(sides=6, k=12)

This works, but this only let's you simulate data. Maybe there are other things you would like to do with dice?

  • calculate the expected value?
  • calculate the variance?

You could implement functions for these features too, but maybe this is a good moment to make a Dice class so that we can build objects that come with all these interesting features.

This will be the motivating example for this first course.