Show more

As we have a one day's break from the Python functions series which returns with Day 10 tomorrow, let's look back the the topic from the last three days:

• positional-only arguments using / in function definitions
• keyword-only arguments using * in function definitions

/1

One of the most underrated modules?

turtle

We know it’s used for teaching & not in the “real-world”

But it’s usually used only for basic drawings & simple animations for kids–a very limited scope

We can teach more advanced topics, too…

Here are some examples…


1. Data structures: lists, tuples, dictionaries, and sets

Here’s an animation that uses all four of these structures:

Practise Using Lists, Tuples, Dictionaries, and Sets in Python With the Chaotic Balls Animation


2. Named Tuples

And a bit further down the line, here’s an example of a turtle animation of a sunrise that introduces Named Tuples:

Sunrise: A Python Turtle Animation using Named Tuples


3. Demonstrating real-world processes

Maybe we want to use it model real-world processes in a quick-and-simple way.

Here is a demonstration of “simulating” bouncing balls

There’s the single ball version:
The Real World in Python: Coding A Bouncing Ball

and the many balls one which deals with object-oriented programming:
_Bouncing Balls Using Object-Oriented Programming in Python

And, one of my favourites, a simulations of planets orbiting one or more stars. The turtle version is in 2D:
Simulating Orbiting Planets in a Solar System Using Python

There’s also a 3D version which uses Matplotlib instead of turtle, but that’s off-topic here so I’ll post another time


Here are some more ideas from projects I’ve written up recently

thepythoncodingbook.com/catego

I have loads and loads more which I’ll try to write up and publish in the coming months

Hi I'm Matt 🙂👋

I'm a senior software developer @ Anaconda. I love #python, #rust & #golang. I'm super passionate about making data science and scientific computing accessible to everyone.

I came into tech after 8 years in the energy industry as an ocean engineer, where I did a whole lot of scientific computing and data science for floating platforms.

Before that, I got a PhD in Naval Architecture and Marine Engineering where I studied hydrodynamics of high-speed ships.

#introduction

RT @PythonMorsels@twitter.com

We have immutable objects in Python but we don't have constant variables.

The UPPERCASE_CONVENTION for constants is just a naming convention in #Python. It doesn't stop variables from being re-assigned.

pym.dev/python-doesnt-have-con

🐦🔗: twitter.com/PythonMorsels/stat

Want to learn Python?

There must be many here looking to and to

Happy to connect and help answer questions anyone might have

ask away…

Can a toot’s visibility be changed after it’s posted?

Although the early parts of the series are not on here, you can see them on my Typefully page: typefully.com/s_gruppetta_ct

Show thread

• Using keyword-only arguments, or the "rogue" asterisk * in function signatures

Next up:
• type hinting
• general best-practices when defining and using functions

Show thread

• Choosing to use arguments as either positional or keyword
• Using optional arguments by assigning default values
• *args and **kwargs, or variable number of positional and keyword arguments
• Using positional-only arguments, or the "rogue" forward slash / in functions
• …

Show thread

For those who've been following the Functions Series, today we'll take a short break and Day 10 will return tomorrow – There's still a related-thread later today, but not directly part of the series.

So far we've covered:
• terminology (boring but useful)
• …

Addendum (some sad news): Since taking those pictures for the article, that sole remaining espresso cup has suffered this fate

I was told I can’t say Rest In Pieces

Show thread

Instead, you should put a coffee cup in the machine when you turn it on:

>>> cup = make_coffee(
... electricity,
... water,
... my_favourite_blend,
... )

Now, you no longer make a mess on the kitchen bench you’re collecting what’s returned into something

There’s a fuller version of this analogy here, too:

thepythoncodingbook.com/2022/0

8/

Show thread

You called the function like this:

>>> make_coffee(
... electricity,
... water,
... my_favourite_blend,
... )

and therefore, the coffee returned is not collected and stored in anything…

7/

Show thread

The coffee flows directly into the drainage tray at the bottom of the machine and will likely overflow, making a mess on your kitchen bench…

6/

Show thread

— 4 —

When the coffee machine does its thing, hot, liquid coffee will emerge from the machine.

You could say the coffee machine _returns_ liquid coffee.

but, there's one problem…

5/

Show thread

— 3 —

When you call the function/press the "On" button on the coffee machine, the machine will "do stuff" inside.

If you just care about drinking your coffee, you don't care what's happening inside the machine…

If you're an engineer building your own coffee machine, or you want to open up your coffee machine to fix it or—ahem—make improvements, then you _do_ care about what's happening inside the coffee machine.

4/

Show thread

— 2 —
You press the "On" button—this is equivalent to calling the function.

You can almost see the similarity between the typical "On" button and the parentheses ( ) used to call a function in Python!

3/

Show thread

A function is like a coffee machine

– 1 –

It needs inputs to work (arguments):
— water
— electricity
— ground coffee beans (or pods if so inclined)

You can choose to put different blends of coffee into your machine (choice of what argument to pass to function)

2/

Show thread

As it’s time for my morning coffee (the coffee not the biscuits), it’s as good a time as any to share my one of my favourite analogies

(narrated from a -viewpoint but general enough for in general)

The Coffee Machine - Function analogy

Let’s make some coffee…

[read on]

/1

people who use , is there any link on what flavour of Markdown and what features it has or doesn't have?

Just tried using the triple back tick ``` to create a code block but it didn't seem to work. Single back ticks are fine

Show more
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.