Stephen Gruppetta Profile picture
Constantly looking for simple ways to explain complex things in Python https://t.co/jLEyJqmURW Young coders: https://t.co/NBKw65QD0c
Matthew Kane Profile picture Ch Saibabu Profile picture Learning in Public - Coding - DataSci Profile picture YassinGUREY Profile picture 4 subscribed
Apr 5, 2023 33 tweets 7 min read
Object-Oriented Python at the Hogwarts School of Codecraft and Algorithmancy
---
Year 5:
Inheritance
---

The students are growing up! This Year they'll learn about a key property of OOP - how to create a class that inherits from another one

Here's the code so far

/1 Image We've got four classes:
• Wizard
• House
• Wand
• Spell

Each one has its own attributes — these are data attributes and methods

The classes represent "things" as seen from a human's perspective. Each class contains all the data attributes and methods needed by an object

/2
Apr 3, 2023 20 tweets 6 min read
Object-Oriented Python at the Hogwarts School of Codecraft and Algorithmancy
---
Year 4:
More interaction between classes
---

Year 3 was a busy year. Here's were we left in Year 3

/1 See year 3 thread for code in ALT text In earlier Years, we discussed how we should think of the problem from the point of view of the "objects" that make sense to a human describing the problem

In our example so far, we have:
• Wizard
• House
• Wand

An object that belongs to these classes has _attributes_

/2
Apr 1, 2023 4 tweets 2 min read
Slightly off-topic from my usual on this account—but a lot of my work is also linked to children's education (coding primarily, but let's think beyond subjects)

I've taken a stab at what "homework" in the near-future could look like, using a GPT-first, cheat-proof approach…

/1 1. Write an outline of your... It solves reasonably well the problem of "cheating" by incorporating GPT into the process and requiring the whole conversation to be submitted, including first drafts, suggestions by GPT, second drafts, and so on

Teachers can craft the right prompts that all students use

/2
Mar 5, 2023 15 tweets 3 min read
What Python data type would [a multi-storey car park] be?
–––
You drive into a multi-storey car park, find a spot to park, and then you ask yourself: "what data structure would best represent this car park", of course

Let's explore

1/ Model of a multi-storey car... Let's name the object `car_park` and it contains several levels. I'll also look at `level` which represents one the levels. This will be a different data type

The car park has a fixed number of levels which don't change. Therefore, `car_park` is immutable

2/
Mar 4, 2023 7 tweets 2 min read
These thought experiments help us focus on real decisions we need to make when choosing what data types to use in programs

It's not just about the data itself but how you're likely to use it

For example, in this restaurant tables scenario, I opted for dictionaries, but…

/1 It depends whether we want to prioritise the use case when the tables are being used during normal service, with diners sitting on them and waiters taking their orders and bringing them food [dictionary is a good choice]…

/2
Feb 26, 2023 23 tweets 9 min read
Let's write this game in 20 tweets…
Yes, just 20

Ready?

Go…

• Balls appear in random positions every few seconds and are tossed upwards with random velocities
• Click close to the ball to "bat it up"
• How long until you lose 10 balls?

/1 You'll work in 2 scripts–the 1st is `juggling_balls.py` where you'll define the class `Ball` which inherits from `turtle.Turtle`

The bounds of where the ball is created are passed as arguments to the function's `__init__()` method

Ball's colour, shape, and position are set

/2 # juggling_balls.py  import random import turtle  class Ball
Feb 24, 2023 6 tweets 2 min read
What happens when you reverse a `range` object in Python?

A range object is a sequence. This means it is:
• An iterable
• A container
• A sized object (it has a length)
• Indexed using an integer

It also means it can be reversed as all sequences are reversible

/1 # A `range` object is a seq... You can refresh your memory about the ins and outs of the key data structure categories in this series:

/2
Feb 19, 2023 17 tweets 5 min read
––– Mid-Series Roundup (Day 5.5) –––

Let's review the terms we've seen so far but in a different order to how I presented them in this series

Let's start with three “top-level” categories:

• Iterable
• Container
• Sized

1/ Each category provides one characteristic:

• Iterable: can be iterated

These classes normally have `__iter__()` defined, but could also only have `__getitem__()`

2/
Feb 18, 2023 18 tweets 3 min read
––– Day 5 –––
––– Collection –––

Here’s another term that’s easy to confuse with container, in part because they both start with the same letters

An object is a collection if:

• It is a container
and
• It is iterable
and
• It has a length

/1
A collection comes further down the hierarchy of categories – more on this hierarchy soon

A collection needs to satisfy more conditions than a container

/2
Feb 17, 2023 6 tweets 1 min read
––– Day 4 –––
––– Container –––

You may see the term container used when reading the documentation

This categorisation applies to lots of data structures

The hand-waving description is that a container is an object which can contain other objects

/1
So, an integer is not a container. However, a list is

You can think of a container as sitting near the top of the hierarchy of data structures

More on this hierarchy in the next couple of days in this series

/2
Feb 17, 2023 7 tweets 2 min read
I rarely do introspective tweets. And I don't care about round number milestones either, which is why I'm tweeting this 30 days "early"

335 days ago I picked up this dormant account I had, and its 279 followers, and decided to start sharing and engaging with the community

/1 Image I enjoyed every day of these 335
I learned a lot from tweets by others
I learned a lot from replies to my tweets
I learned a lot from the research into my tweets and threads
I got to know, albeit in the Twitter sense of the word "know", lots of great people

/2
Feb 14, 2023 25 tweets 5 min read
––– Day 1 –––

––– Iterable –––

Let’s start with iterable

Easy 'definition' first:
Any data type which can be used in a `for` loop

/1
If all you want to know for now is how to determine whether something is an iterable or not, just put it at the end of a `for` statement and see whether you get an error

/2
Jan 30, 2023 23 tweets 7 min read
There's a peculiar little shop at the end of my street where I get my morning coffee from

Here's the price list written on a chalk board hanging precariously behind the owner…

Let's replicate this sign using Python's f-strings

/1 Price list on a chalk board for a peculiar shop. The sign sa Let's start with yesterday's thread in case you missed it

(How could you have missed it? What were you doing all day on a Sunday if not scrolling through Twitter?)

/2
Jan 29, 2023 15 tweets 4 min read
f-strings?
fantastic-strings
formidable-strings
fancy-strings
fabulous-strings

Let's look at a number of ways we can deal with strings in Python

(Spoiler alert: f-strings are the best—did you get the clues that I quite like f-strings? Or was I too subtle?!)

/1 >>> name = "Mary" >>> profession = "scientist Let's look at three options for creating a string from smaller parts

# 1.
Using `+`
The simplest way of concatenating strings is to add them

<Code in ALT text in all code snippets>

/2 >>> first_name = "Stephen" >>> last_name = "G
Jan 28, 2023 10 tweets 4 min read
Time for some `turtle` fun again today…

Did you know you can use _any_ image as your turtle?

Not just the very basic built-in options…

Let's see how by creating this animation

/1 You can change the shape of the turtle using the `.shape()` method. However, there aren't many built-in options available

This is the most interesting one:

/2 import turtle  fred = turtl...
Jan 27, 2023 14 tweets 4 min read
Assignment using slices can lead to some surprising results in Python

Let's explore this behaviour and see how slicing (using a range of indices) differs from indexing (using just one index)

/1 >>> some_numbers = [0, 1, 2, 3, 4, 5] >>> some_numbers[1:4] Let's start with probably the most straightforward case

I'm deliberately using numbers that happen to match their indices in the original `some_numbers` list to make it easier to see the changes

/2
Jan 25, 2023 7 tweets 2 min read
Yesterday we looked at `zip()` with three (or more) iterables

But the iterables all had the same length

What happens if you try to zip iterables with a different length?

Let's look at the default behaviour and a number or alternatives to the default…

/1 Here's an example similar to yesterday's, but one of the lists has an extra value compared to the others

When you zip these three lists, the extra value in `points` is not included. `zip()` will stop when the shortest iterable is exhausted

/2 names = ["Stephen"...
Jan 7, 2023 33 tweets 8 min read
Yesterday I looked at the built-in `max()` function. Today, I'll explore #NumPy's version:

`np.max()` or `np.amax()`

There are many differences between the built-in function and NumPy's version. So let's explore…

/1 import numpy as np  # Let's create some random numbers in a Here's yesterday's thread about the built-in `max()` if you'd like to start from there:

/2
Jan 6, 2023 21 tweets 5 min read
Let's see how to use `max()` to the max
[Sorry! Couldn't resist. I won't do that again, I promise]

You probably have used the built-in `max()` function

Let's look at all the ways we could use it

<Code in ALT text in all code snippets>

1/ some_words = [     "hello",     "goodbye" The most common use of `max()` is probably with a list (or any iterable) of numbers

2/ numbers = [4, 10, 2, 19, 3, 12]  print(max(numbers)) # OUTPU
Dec 26, 2022 18 tweets 4 min read
This is a common gotcha in Python

It isn't a mistake or inconsistency
There's a reason behind it

And once you understand when you use data attributes (no parentheses) and when methods (with parentheses), it will all make sense

Let's look at this example from `datetime`

/1 >>> import datetime >>> today = datetime.datetime.now() >>> You're creating an instance of the `datetime.datetime` class when you use `now()`

As most objects, this instance will have some data attributes (you may call them instance variables). These are the attributes that store data (that is the variables linked to the instance)

/2
Dec 25, 2022 6 tweets 1 min read
A few days ago I posted a "countdown to Christmas" code. Here's the updated version…

This should work before, on, and after Christmas day…

See below for a brief walk through…

<code in ALT text>

/1 import datetime import math  today = datetime.datetime.now() We start by getting today's date using datetime's `now()` function

/2