Show newer

# Using Python to create a solar system

If anyone is looking for a fun exercise to flex their fingers…

Using just gravitational attraction between bodies, you can create your own 2D solar system with as many stars and planets as you want. Here's a binary star system with some relatively stable planets

Here's the article, including a detailed step-by-step tutorial, if you want to read more: **[Simulating Orbiting Planets in a Solar System Using Python](thepythoncodingbook.com/2021/0)**

…and there's also a 3D version _(next post)_

@ambv @mariatta I agree with the retweets (I qualified my comment in a later reply, in fact – apologies for that) My comment was more on the stance in the links on cross-posting in general, rather than RTs

@ambv @mariatta PS: I realise your comment was about an RT with Twitter handles, but the links go on to discourage any cross-posting. I understand that the _platform_ will prefer that. But is it the right thing for the _people_ on the platform?

@ambv @mariatta but that also means that there will be less quality content on here. Many I’m sure are still focusing mostly on Twitter, so cross-posting primary posts (not RTs), particularly if they don’t have Twitter-specific references should be OK

I realise that those who’ve been here long will resent this, but it’s the price to pay for becoming a little more popular, I guess

We'll return to the Python functions series tomorrow!

/11

Show thread

They're easy to miss and ignore. But if you've read documentation, you will definitely have seen a * or a / in many function signatures

Now, when you see them next, you'll know perfectly well what they are and what they do

/10

Show thread

Or maybe you use Matplotlib? Here's `plt.scatter()`

Wait for it

scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)

Did you spot the lone * ?

/9

Show thread

Maybe you use NumPy a lot? Positional-only and keyword-only arguments appear very often in the NumPy codebase

Here's just one example, NumPy's `mean()` method:

a.mean(axis=None, dtype=None, out=None, keepdims=False, *, where=True)

/8

Show thread

Let's check:

>>> "hello".replace("l", '-')
'he--o'

>>> "hello".replace(old="l", new='-')
Traceback (most recent call last):
...
TypeError: str.replace() takes no keyword arguments

/7

Show thread

How about `str.replace()`? You've used that too?

Here's the signature:

replace(self, old, new, count=-1, /)

`old`, `new`, and `count` are all positional-only

/6

Show thread

`key` and `reverse` are keyword-only in `list.sort()`

/5

Show thread

>>> numbers = [4, 6, 7, 2]
>>> numbers.sort(reverse=True)
>>> numbers
[7, 6, 4, 2]

All fine here. But try to use a positional argument instead:

>>> numbers.sort(True)
Traceback (most recent call last):
...
TypeError: sort() takes no positional arguments

/4

Show thread

Have you ever used `list.sort()` with the `key` or `reverse` parameters?

I bet you have!

Here's the signature for `list.sort()`

sort(self, /, *, key=None, reverse=False)

/3

Show thread

Why do we need them? Are they even ever used?

I bet you've seen them many times and ignored them. I'll go further and bet you've used them, too, without knowing

/2

Show thread

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

@jimlawless yes, but I think the classic examples of drawing triangles and squares and so on are _very_ limited in scope and can get boring and childish very quickly.

The module can be used for a lot more than that…

@willmcgugan I know I’m _very_ late to the party, but I’m finally planning to jump in and start creating my first TUI soon

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](thepythoncodingbook.com/2021/1)_

---

# 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](thepythoncodingbook.com/2022/0)_

---

# 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](thepythoncodingbook.com/2021/0)_

and the many balls one which deals with **object-oriented programming**:
_[Bouncing Balls Using Object-Oriented Programming in Python](thepythoncodingbook.com/2021/0)

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](thepythoncodingbook.com/2021/0)_

_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

Show older
Qoto Mastodon

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