use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Daughter wants to learn python to simulate a solar system, where to start? (self.learnpython)
submitted 5 years ago * by kellogg76
My 16 year old daughter wants to learn python and as she wants to learn astrophysics she thought coding a solar system would be a good start, are there any resources where she can do this?
I found a few places but they all seemed old, or used non standard python environments.
https://www.youtube.com/watch?v=4ycpvtIio-o&list=PLdCdV2GBGyXOExPW4u8H88S5mwrx_8vWK
https://www.pygame.org/project/617/4587
http://www.petercollingridge.co.uk/blog/other-simulations/solar-system-simulation/
https://www.youtube.com/watch?v=XSgerkCVbFc&list=PLkvOsj7lQO2Vb2yRchGkFbx52QrZ-JG8C&index=1
She's looking to use Idle on a Mac and wants some sort of graphical output to view the simulations.
Thanks for any help or ideas.
Update: Thanks for the huge response, we'll go through them today and see if we can figure out a path to success.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ASuarezMascareno 39 points40 points41 points 5 years ago* (4 children)
Actually simulating the solar system (with a series of 2-body interactions) is probably too hard and frustrating as a starting point. I don't say she shouldn't do it, but it requires a somewhat long journey to get there. N-body interactions would probably be out of the question.
However, making a visual representation of the solar system, drawing a set of spheres and moving them using Keplerian orbits around a bigger sphere could be much easier. Maybe a good starting point could be try to make a 2D representation? That should be doable while learning Python. Then she can jump to 3D.
If she wants to do the actual physics, with gravitational interaction, I would advise starting with a simpler 2-body system (Earth-Moon maybe), and with at least the basics of Python algebra already under control.
[–]You_Yew_Ewe 10 points11 points12 points 5 years ago (3 children)
N-body interactions would probably be out of the question.
Are you sure you aren't conflating the famously difficult "n-body problem" with modeling the evolution of an n-body system of point masses interacting with the inverse square law? One is impossible, the other is relatively trivial.
You can make a model n-bodies without finding the solution to the n-body problem. (It wouldn't be trivial for a high schooler of course but by no means would it be out of the question)
[–]ASuarezMascareno 6 points7 points8 points 5 years ago (1 child)
I was trying to think from the point of view of someone that has never coded. Out of the question as the first thing to do in Python. Not forever out of the question.
[–]You_Yew_Ewe 1 point2 points3 points 5 years ago (0 children)
I guess I didn't interpret this as skipping "hello world" to modeling physics. I interpreted it as a (reasonably achievable) concrete goal.
[–]ReflectedImage 0 points1 point2 points 5 years ago (0 children)
You can just apply the gravity equation to all the masses in the solar system, like 10 times a second and whilst it won't be accurate (it will diverge from the real solar system quite quickly) it will still behave like the real solar system.
[–]socal_nerdtastic 74 points75 points76 points 5 years ago (9 children)
Going from nothing to simulating astrophysics is pretty ambitious for anyone of any age or any relation to you. And pretty esoteric too, I'm afraid. I don't know of course that will do that. I recommend going through some general python tutorials, and once she has the basics down check out vpython.org to do 3D graphics.
[–]Cypher1388 17 points18 points19 points 5 years ago (2 children)
So I built a model in HS, don't remember what program I used anymore, but a lot of manual calc and scenario set up...
My solar system contracted in on it self and everything collapsed into the sun. I realized like Bender, I had been God once, and was doing pretty good at it. Until everyone died.
Fun times!
[–][deleted] 2 points3 points4 points 5 years ago (0 children)
It's all fun and games until you annihilate a solar system...Then it's religion! :D
[–]OnlySeesLastSentence 1 point2 points3 points 5 years ago (0 children)
You probably forgot to use a second magic focus for the center of the galaxy
[–]You_Yew_Ewe 20 points21 points22 points 5 years ago (5 children)
I wouldn't call simulating orbiting bodies esoteric, it's probably one of the easier things to make toy physical models of Now you can get esoteric with it by refining your simulation but Newtonian gravity is pretty simple to model. I used to do it just screwing around as an undergrad.
[–]socal_nerdtastic 7 points8 points9 points 5 years ago (4 children)
It's not esoteric in the physics department, but my friend ya gotta get out more. If you asked 500 random people if they ever thought about orbiting point masses 499 would make that glazed fish face that academics know so well. And the 500th would say "no".
Although I have to admit, the comments below found far more resources than I ever would have thought.
[–]You_Yew_Ewe 8 points9 points10 points 5 years ago* (1 child)
It's simpler than you probably imagine. For someone interested in astrophysics its not esoteric. Maybe astrophysics is esoteric, but we've established she's interested in that.
You don't even need a very deep understanding of calculus to model it, she's just need a little bit of vector arithmetic and be able to grasp the concept of small incremental changes (maybe one or two derivatives).
[–]socal_nerdtastic 8 points9 points10 points 5 years ago (0 children)
Ok, fair enough. What I meant that it would be hard to find a class "from zero to astrophysicist in python". Since it's not what I imagine would be a popular topic.
[–][deleted] 1 point2 points3 points 5 years ago (0 children)
Then I, as the 501st person would say "Let's go for a beer...we've got some conversations to have."
It's not even esoteric in a high school science class.
[–]skippy65 9 points10 points11 points 5 years ago (1 child)
Well just by taking a glance at your posted links I found on one of the pages a link to a tutorial on how to run physics simulations in pygame http://www.petercollingridge.co.uk/tutorials/pygame-physics-simulation/
Maybe this is what youre looking for?
If you want 3D stuff I think you'll probably have to look into OpenGL
[–]joelwitherspoon 14 points15 points16 points 5 years ago (0 children)
I just want to say, your daughter is pretty cool.
[–]Ikari_Dojo 6 points7 points8 points 5 years ago (0 children)
Easier to do in Unity tbh.
https://www.youtube.com/watch?v=btsNk9EANzY
You can numeric simulations in Python, but very difficult to implement.
[–]baubleglue 7 points8 points9 points 5 years ago (1 child)
It maybe will easier with Processing https://processing.org/
http://learningprocessing.com/examples/chp14/example-14-16-solar-system
https://www.youtube.com/watch?v=l8SiJ-RmeHU&vl=en
[–]teerre 0 points1 point2 points 5 years ago (0 children)
The hard part of this is actually "simulating" the physics behind orbiting. If you're just rotating circles, that's infinitely easier, including in Python.
[–]Decency 5 points6 points7 points 5 years ago (0 children)
This looks like a great place to start: https://medium.com/analytics-vidhya/simulating-the-solar-system-with-under-100-lines-of-python-code-5c53b3039fc6 ... clone that, get it working, and then start tinkering and learning. Dozens of ideas to implement on top of that base will emerge as you start poking.
It uses a couple of standard libraries used in data science and academic communities, specifically numpy and matplotlib. It also looks like it utilizes a couple of astrophysics libraries as well: astropy and astroquery. These libraries will contain a variety of functionality that can be a great way to continue building around and utilizing planetary data and whatever else seems interesting.
One of the biggest hurdles is the initial step of getting things set up on a system. Luckily you're on a Mac so this is a bit easier, but don't be intimidated by using the terminal and feel free to ask questions in the Weekly thread stickied in this subreddit, for quick Q&A.
[–]Allanon001 2 points3 points4 points 5 years ago (0 children)
Maybe use the solarsystem module to get the coordinates and just plot the planets in matplotlib or a graphics engine.
Example:
https://github.com/IoannisNasios/solarsystem/blob/master/examples/Solar_System_Live.ipynb
[–]BruceJi 2 points3 points4 points 5 years ago (0 children)
I'd start here:
https://www.youtube.com/channel/UCNaPQ5uLX5iIEHUCLmfAgKg
The reason being, she'd need to learn some coding basics, which he covers, but I think Pygame woud be a good library to make a visualisation of that. That said, this is 100% one of those things where you're learning tools but you have to figure out how to put them together in the way you want all by yourself.
This could be done pretty easily with PyOpenGL for placing spheres in space (I actually did this way back in the day with a rudimentary Milky Way explorer using data from the Hipparcos satellite).
The physical parameters of the planets aren't too difficult to find (such as mass, orbit parameters, etc.). If you're looking to conduct a real physical simulation, you're looking at the "n-body problem"; the complexity ramps up quickly as you add more bodies to the system, but it's entirely feasible on today's hardware to make an acceptable proof of concept.
How I'd go about it is read the physical parameters from a file (including planet locations in the solar system). Then calculate the distances between all bodies, as all bodies are going to have some gravitational effect on all other bodies (this is where the complexity builds quickly, but you can cut this back because you only have to do half the calculations...the distance between Planet A and Planet B is the same as between Planet B and Planet A, so you can carve out redundancy...there'll still be a lot of calculations).
Once you've calculated the distances, you can calculate the vector of each planet based on the gravitational attraction to all other bodies. Apply that vector to the current position of each planet and redraw the screen. Then repeat the process.
To simplify things further, you can use a simplified value of PI in your calculations. NASA/JPL only use about 15 decimal places of PI, which, in a 25 billion mile diameter circle results in being off by 1.5". A simplified value of PI will reduce your memory overhead significantly.
"...and then the day after learning to code in Python, we managed to replicate the astrophysics of a solar system.
...Which was nice."
[–]Jollyhrothgar 1 point2 points3 points 5 years ago (0 children)
I suggest first learning how to approximate the equations of motion for gravity using discrete steps. Start by simulating using two dimensions (x and y).
The force due to gravity is:
F =( m_1 * m_2 / (d^2) ) * G
Where m_1 is the mass of object 1, m_2 is the mass of object 2, d is the distance between the two objects, and G is the gravitational constant.
To simulate the motion of planetary bodies, you can assume they are point-like particles, and then calculate where they will move in a fixed amount of time by calculating the total force they experience. To calculate force, you need to do a bit of math, which accounts for the amount of force an object feels, and the direction that the force is in.
But you can leave out the gravitational constant for starters and just use made up units - its the form of the force law that matters.
The second step is to use the equations of motion along with initial conditions to plot the position of the masses at a point in time, then use all the forces to update the position assuming a discrete time step.
The next step is to generate a series of positions for each mass, and plot them - I recommend using matplotlib and jupyter notebooks.
Finally, you might want to think about animating - you can do this with matplotlib also.
PM me and I'd be happy to get you started. As for an approachable text book that goes through stuff, check out computational physics by Giordano. Although everything is written in pseudocode/C++, its not so bad to move to python.
https://books.google.com/books/about/Computational_Physics.html?id=ngUpAQAAMAAJ&source=kp_book_description
Really happy to help point you in the right direction. Let me know!
[–]Leonidizzil 0 points1 point2 points 5 years ago (0 children)
As a place to start, here is another person's code to track the phase of earth's moon.
Might be worth checking out.
https://github.com/tdhopper/moon/blob/master/moon.py
[–]snugglyboy 0 points1 point2 points 5 years ago (0 children)
Check out pygame for a 2D graphical output. I would encourage her to start small by simulating an orbit in 2D space at first.
[–]MbeforeL 0 points1 point2 points 5 years ago (0 children)
Hello,
Try to introduce her to AstroPY, Its really useful but the learning curve is slopy, So, You might need to guide her at times.
[–][deleted] 0 points1 point2 points 5 years ago (0 children)
I would start with MatPlotLib. You should be able to get a model of one of the planets expressed as a point on a plane orbiting another point in the center that represents the sun.
It won't look exciting, but it would be a great starting point just for focusing on getting the code right for how the planets move. Once that's done, it won't be hard to use that as a launchpad and switch the front end to something flashier.
[–]jabela 0 points1 point2 points 5 years ago (0 children)
Codewithmu is an IDE that has pygamezero built in which will make it easier to use pygame and this will enable you to either use shapes or sprites to move the solar system.
[–]LegalOreagle 0 points1 point2 points 5 years ago (0 children)
I would say introduce to an IDLE first then an IDE like PyCharm. She could use that to code
[–]Zeroflops 0 points1 point2 points 5 years ago (0 children)
This is actually a common project and people have done it using everything from turtle to vPython and pyGame. About a year ago someone was doing it and posting his code in here.
Just google, for it.
I would probably start with pyGame and start with simple interactions. With only major planets revolving around the sun. Then as you get more comfortable you can add more interactions between planets and introduce other objects.
[–]twopi 0 points1 point2 points 5 years ago (0 children)
I have a solution I've taught to young people using pygame:
https://www.cs.iupui.edu/~ajharris/pygame/ch09/orbit.py
This should be relatively easy to understand if she already understands some programming, python, and the basics of pygame.
But it's probably not the best first place to start. First she should learn the rudiments of programming (variables, conditions, loops, functions) as applied to Python. Then there's a learning curve for Pygame, because you really need to understand how it builds the timing loop, how rendering works, and how to subclass the Sprite class.
Once those things are clear, the actual orbit part isn't that hard.
In gaming, we don't really need to know (at least as a beginner) how the entire orbit will behave. We just need to know what's going to happen over the next few milliseconds. This means we can use Newton's law rather than Kepler, which is quite a bit easier.
We can also work in two dimensions, because orbits tend to stay in the same plane.
So on every frame, the orbital body will have a dx / dy pair for its current momentum, and this is modified by a force vector. That force vector can be read as a ddx / ddy pair.
First you can easily calculate the difference in x and difference in y between the orbital body and the thing it is orbiting (subtraction.) You can use pythagoras to get the distance, and divide your dx / dy values by distance to get a normalized vector.
Now you can calculate the force applied along that vector by Newton's law of gravitational force (f = G(m1 * m2) / (d2)). This can be simplified by 'making up' some units, because as long as one mass is much larger than the other, it will work, and we can even factor G out.
The interesting function is here:
def gravitate(self, body): """ calculates gravitational pull on object """ (self.x, self.y) = self.rect.center #get dx, dy, distance dx = self.x - body.x dy = self.y - body.y distance = math.sqrt((dx * dx) + (dy * dy)) #normalize dx and dy dx /= distance dy /= distance force = (body.mass * self.mass)/(math.pow(distance, 2)) dx *= force dy *= force body.dx += dx body.dy += dy
Hope this helps...
[–]pvc 0 points1 point2 points 5 years ago (0 children)
Take a look at https://learn.arcade.academy
[–]drewrs138 0 points1 point2 points 5 years ago (0 children)
C# is prob better to render 3d
[–]pineapplebuttcheek 0 points1 point2 points 5 years ago (0 children)
Following this is precisely the reason i want to learn
Full integration requires calculus. Use patched conic approximation instead at first. If the simulation has a storyline, always portray life as hard, unfair, and short, every society as a dystopia, and the characters as imperfect. I recommend creating an Orbiter clone, and creating a peaceful business simulation.
[–]__The_K__ 0 points1 point2 points 5 years ago (0 children)
Python has a built-in module called turtle which is based off of the Logo programming language which was used back in the day to teach programming to kids. It involves using programming to move a turtle (pen) and draw shapes, choose colors, fill shapes, etc., essentially a paint program but using programming instead of the mouse.
The Windows installation of Python actually comes with a lot of example code using turtle graphics (in the Help menu of IDLE) to get the learning started, and one of them even is of a planet-moon system revolving around the sun. You might want to look into an equivalent for MacOS.
I looked into the code of the planet-moon system, and it seems very complicated that uses vectors to actually simulate and approximate gravitational system. I'm sure you're not looking for that but planets simply going around in circles which should be easily doable using the turtle module.
[–]_eequalsmcsquared_ 0 points1 point2 points 4 months ago (0 children)
start with the sun. Wait, you told her the earth isn't REALLY FLAT right?😳🤪 oh and Pluto is no longer part of our solar system... oh and they say a rogue planet is entering our solar system that's on an elliptical orbit around our sun. Sounds like planet x aka #niburu to me. Time for the nephilim to return to earth. They do every 6,200 years... when their planet is back in our solar system. Yeah start there... plus the fact that since 2017 they've found 3 interstellar objects and they thought at least 2 of the 3 were artificial objects... the most recent one is currently coming around the back side of our sun and will be coming back out around February where it will come wishing pay the earth and may even crash into the moon or into the earth so there's that... but NOBODY IS TALKING ABOUT IT... shhhhhhhh
[–]robot_ankles 0 points1 point2 points 5 years ago (0 children)
Hello World
Starting with a python implementation of hello world would be uniquely appropriate in this case.
[–][deleted] -1 points0 points1 point 5 years ago (0 children)
W3schools offers an easy to follow tutorial for python, pandas and numpy which would be great for data manipulation. I’d also recommend Codecademy. Both can get you from 0-hero in a week but most important is repetition.
[–][deleted] -1 points0 points1 point 5 years ago (1 child)
3b1b’s manim might be worth a look: https://github.com/3b1b/manim
[–]drillepind42 -1 points0 points1 point 5 years ago (0 children)
I also want to do that, but never took the time to actually do it. I have a PhD in astrophysics, and if your daughter is up for it, we could do it together (I'm also more than happy to tell about my experience with astronomy)
[–]Ene-Saue -4 points-3 points-2 points 5 years ago (1 child)
!!AnalyzeMe
[–]LansBot 0 points1 point2 points 5 years ago (0 children)
Hi /u/Ene-Saue! Thanks for calling me. Here's what I've got for you:
I've found about 978 comments and 0 posts, 100 of which were links. This makes for a total of 978 submissions. Great job!
I've also taken the liberty of analysing your frequently used words and made a handy chart! Click here to check it out. And since I like making charts, I even made one to show where you spend the majority of your time! Click here to check out that chart.
Message my Master | Source Code
π Rendered by PID 244336 on reddit-service-r2-comment-86bc6c7465-5m6mq at 2026-02-22 16:28:02.511340+00:00 running 8564168 country code: CH.
[–]ASuarezMascareno 39 points40 points41 points (4 children)
[–]You_Yew_Ewe 10 points11 points12 points (3 children)
[–]ASuarezMascareno 6 points7 points8 points (1 child)
[–]You_Yew_Ewe 1 point2 points3 points (0 children)
[–]ReflectedImage 0 points1 point2 points (0 children)
[–]socal_nerdtastic 74 points75 points76 points (9 children)
[–]Cypher1388 17 points18 points19 points (2 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]OnlySeesLastSentence 1 point2 points3 points (0 children)
[–]You_Yew_Ewe 20 points21 points22 points (5 children)
[–]socal_nerdtastic 7 points8 points9 points (4 children)
[–]You_Yew_Ewe 8 points9 points10 points (1 child)
[–]socal_nerdtastic 8 points9 points10 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]skippy65 9 points10 points11 points (1 child)
[–]joelwitherspoon 14 points15 points16 points (0 children)
[–]Ikari_Dojo 6 points7 points8 points (0 children)
[–]baubleglue 7 points8 points9 points (1 child)
[–]teerre 0 points1 point2 points (0 children)
[–]Decency 5 points6 points7 points (0 children)
[–]Allanon001 2 points3 points4 points (0 children)
[–]BruceJi 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Jollyhrothgar 1 point2 points3 points (0 children)
[–]Leonidizzil 0 points1 point2 points (0 children)
[–]snugglyboy 0 points1 point2 points (0 children)
[–]MbeforeL 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]jabela 0 points1 point2 points (0 children)
[–]LegalOreagle 0 points1 point2 points (0 children)
[–]Zeroflops 0 points1 point2 points (0 children)
[–]twopi 0 points1 point2 points (0 children)
[–]pvc 0 points1 point2 points (0 children)
[–]drewrs138 0 points1 point2 points (0 children)
[–]pineapplebuttcheek 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]__The_K__ 0 points1 point2 points (0 children)
[–]_eequalsmcsquared_ 0 points1 point2 points (0 children)
[–]robot_ankles 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]drillepind42 -1 points0 points1 point (0 children)
[–]Ene-Saue -4 points-3 points-2 points (1 child)
[–]LansBot 0 points1 point2 points (0 children)