So many nuances with string methods :)

The str.replace() method takes an optional count argument so you for example can only replace the first match:

>>> s = "hello world, hello again"
>>> s.replace("hello", "bye")
'bye world, bye again'
>>> s.replace("hello", "bye", 1)
'bye world, hello again'

#python

You can use typing.get_type_hints() (or __annotations__) to get the type annotations of an object.

#python

Do you know the offside rule? Here's a step-by-step tutorial to write a Python Offside Rule Quiz using Object-Oriented Programming

I score 10 out of 10 all the time. How about you?

Great article for anyone:
- who wants to learn object-oriented programming
- who wants to learn the offside rule

thepythoncodingbook.com/2022/1

Offside Rule Quiz using Python's `turtle`. I scored 10 out of 10. No need for VAR if I'm the referee, then?!

Article coming soon*

small print:
*soon is a relative term

#LearnToCode

There are so many fascinating insights in this article by Lambert Meertens about design principles and decisions in Python's precursor languages, B and ABC.

Here's one of them which says so much about Python and about "language wars" some still have today.

Context: It's early 80s, talking about deciding to only allow integers as array indices, despite the fact it violated one of their design principles, with Guido van Rossum joining the ABC team at around this time:

"This design decision had been driven by fear: we had been concerned that aiming too high would make our language unimplementable on the small personal computers that were starting to appear on the market. But Dewar, in particular, convinced me that this meant we were designing for the past, not the future."

And this observation should be seen in the context of another consideration that the designers of B and ABC had, which was common with another high-level language they were exposed to at the time. Here's the quote:

"The design of SETL was predicated on the philosophy that, as computers became faster and faster, human time would be more valuable than computer time "and that the efficiency of the tool set SETL provided should not be measured in terms of speed of computation, but in their conceptual power."

Some still have the debate nowadays about whether computer time or programmer time are more precious. Although this still depends on the case, more and more applications are programmer-time limited today.

But in the past, most (all?) applications were computer-time limited. Way back in the very early days it could take several hours to get the result of a program execution–no wonder languages at the time prioritised computation speed over development speed.

But things have moved on. Python took off because it came at a time when the switch over between computer-time limited applications and programmer-time limited ones was happening as computers became faster.

Yes, we still need the languages that prioritise computer-time today – there are still applications that need them, but the shift has happened and languages such as Python which prioritise the programmers' time over the computer's time will get more and more relevant (they already are, of course)

Article in Inference

inference-review.com/article/t

When does talking about sound like Philosophy 101?

Teaching `None` in (or similar in other languages) is one of them

`stuff = None`

I can be heard saying the following when teaching (not quite, but dramatising a bit for effect!)

"`stuff` is not empty. But we want it to be empty. But we need to put something in. So we put Python's way of representing nothing. But `None` is still an object. So it's still something. But it's something that represents nothingness. Is that all clear?!"

It's a bit like when you're told to listen to silence in a meditation exercise?!

Latest @pythonbytes episode is out with your #python news, just in time for your holiday travel:

#311: Catching Memory Leaks with ... pytest?

pythonbytes.fm/episodes/show/3

By @brianokken and @mkennedy + special guest @murilocunha

— Code Wars: Episode IV – The Language Wars —

Which is better? Java or Python or Javascript or C++ or Rust or Julia or …

You often see the language wars being waged on many fronts – books, online tutorials, and of course here on social media

Each language has its pros & cons, strengths & weaknesses

The reason there isn't one language to rule them all (I'm mixing my fictional references!) is because all have or had a place

The language that's best for one purpose may not be the ideal for another

Where do I stand on this?

Although you only ever see me post about Python, and all my current work is in Python, you may (or may not) be surprised to know that I won't participate in this feud

Yes, I think Python is a brilliant language – and lots has been written elsewhere about this

But the "language A is better than language B" argument or the "language Z is rubbish" line doesn't lead to anything fruitful. Ever!

So these childish debates are not for me, sorry!

Think of the programming language as a tool to achieve your objective. What matters is achieving your objective and not which tools you use

Of course, some tools are more suited for certain objectives…

On the other hand, if you know how to use one particular tool very well and want to use it for most of your tasks, there's nothing too wrong about that, either!

---
Coming Soon: Code Wars Episode V

---

As I've been re-watching the Star Wars trilogy of trilogies (kids are now interested, so I'm doing it for them!), I thought of creating a Code Wars series – where are the battles relating to programming fought?

So, we start with Episode IV, of course. Here it comes…

I'm still surprised at how many hits this article gets every day, so let me reshare it here in case it's of interest to anyone.

It's rather niche, but it's a fun tutorial to follow even if it's not really your field:

Recreating any image from sines and cosines

thepythoncodingbook.com/2021/0

Hey #introduction, I am a software developer and coach.

I love building practical tools that make other people's lives easier.

And I am passionate about helping people build their Python, developer and mindset skills to succeed in their tech careers.

I do this via Pybites, which I co-founded back in 2016 with Julian Sequeira (hope he joins me here soon haha ...)

Teaching #Python and interested in adding physical computing? Or using the BBC #microbit in class and wanting to explore text-based coding? I'm co-hosting two more webinars this month on the new micro:bit Python Editor.
It's designed to overcome barriers to learning with a simulator, reference section with code snippets you can drag and drop into your code, auto-complete, indentation highlighting and more.
Webinar details here: microbit.org/news/events/webin
#microPython

Do you know the offside rule? Sure? To have some fun, I'm writing a quiz using Python's `turtle`

This is what it looks like so far (and no, I'm not bothering with fancy graphics – life's too short)

In this video:
- you can see direction in which team is attacking from the arrows
- each snapshot shows when player with the ball kicks the ball forward
- program (hopefully) detects whether it's offside or not
- there are 15 (random) scenarios in this video

Still to do:
- deal with when forward player is in own half (no offside)
- deal with when forward player is the one with the ball (no offside)
- turn into a quiz
- write article for The Python Coding Book blog about it to document the code - there are some interesting aspects I'm using in the code that hopefully some will find useful.

Weird Python line of the day:

`>>> [10, 20, 15, 12, 9][_]`
`[20, 15]`

**How can you achieve this?**

_Note: this is not something you'll want to do in real code, just a Python curiosity!_

Work in progress - An Offside Rule Quiz using Python's `turtle`

Will aim to get it done in the next few days… (will write it up as an article, too)

For those who don't follow football (that's soccer for some around the world), the offside rule is one of those "notorious" rules that not everyone gets.

This is a great resource for teachers who are eager to teach Python. Teaching Python teachingpython.fm/ #python #edutooter #edtech @edutooters

There are often heated arguments, often with many holding "absolutist" positions, on various topics in . However, as with other areas of life, it's the more nuanced pragmatic view that (should) prevail.

Let's take static and dynamic typing. There are reasons why not all leading languages use either one or the other. They're different tools for different jobs.

If you need to prioritise safety in production code: static typing may be your choice.

If you want to prioritise prototyping speed and writing code rapidly, including by programmers who don't just program but use coding as part of other professional skills, dynamic typing options may be better.

It's all about using the right mindset for the right language. If someone is used to coding in C++, say, and tries to write Python code in the same way they write C++, that can (and will) lead to problems! The same is true the other way round, too, of course.

Take duck typing in languages such as : that's a mindset–prioritising what an object can do rather than what it is. Once you get it, and you think about whether something is a sequence or an iterator rather than a list or string, say, then you're better placed to code in Python

There's no "this is the better one", instead, there's "this is the better one for this application"

One of my (many) 2023 plans is to add finishing touches to ThePythonCodingBook.com and make it available in some other format, too:

- ebook only
- self-published book
- published via a publisher

My preference is the latter but publishers shy away from beginners’ books…

I’d need to spend time carefully pitching to them why I think it’s different from other books. Not sure I can justify time to do that

More thinking needed

In the meantime, the content is all there for beginners to enjoy, and many have done just that

thepythoncodingbook.com/what-p

One thing that many find very confusing when they first learn about generators is that once they're exhausted they're, erm, exhausted.

Eh?!

Let's see what this means with an example. Let's start with a list, first:

`numbers = [item for item in range(10)]`

`# Loop through numbers`
`for number in numbers:`
`print(number)`

`# And let's loop one more time…`
`for number in numbers:`
`print(number)`

We've used a list comprehension to create a list of numbers–it's not the most exciting list you'll see, but it will do here

You're them looping through the list twice, printing out the values each time

Here's the output from this code:

0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9

I did say it's not an exciting list. I know!

The numbers are printed out twice. Of course they are, since you've repeated the loop twice

Let's see what happens with a generator instead of a list:

`# Notice that we've now made this a generator`
`numbers = (item for item in range(10))`

`# Loop through numbers`
`for number in numbers:`
`print(number)`

`# And let's loop one more time…`
`for number in numbers:`
`print(number)`

Notice how we're now using parentheses (round brackets) instead of square brackets when creating `numbers`

This creates a generator

If you `print(type(numbers))` you'll get:
`<class 'generator'>`

and if you `print(numbers)`:
`<generator object <genexpr> at 0x103309ff0>`

So, what's the output from the two `for` loops?

Let's find out:

0
1
2
3
4
5
6
7
8
9

The numbers are only printed out once. The generator was "used up" when you looped through it the first time, so you can't use it again

Here's why this happens (very abridged version):

A generator doesn't store the data within it. Instead, it refers to data which is stored or created elsewhere

So, when you try and fetch the first item in `numbers`, the generator fetches `0` and it now "knows" it's taken the first value…

So the next time you need a value from `numbers`, it will get the second one, and so on…

Once it fetches the last item, there's nothing left
The generator is exhausted
When you try to fetch another item, there's nothing there

Think of a generator as single-use

Today for #projects: another hashtag I coined, the #pythonoddity hashtag.

Years ago I decided I was going to try tweeting one "python oddity" (anything that might be a gotcha for a new Python user). I decided to make a hashtag for it: #pythonoddity.

I still collect #python oddities but I don't share them as regularly as I used to. Maybe I should start that hashtag up again here?

Here's a collection of some of my favorite #pythonoddity tweets over the years: twitter.com/i/events/871564334

Show older
Qoto Mastodon

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