r/AskComputerScience • • Feb 14 '21

How does a python program work?

Hi,

So sorry if this is a stupid question but I am trying to learn computer science and someone suggested I learn to code and they suggested the python language. So I do this exercise that is very simple and all I type in the code is:

print(“Hello World!”)

I then learned to maneuver to the program directory in the terminal and to run the program using the command:

python3 Hello.py

However, what I am confused about is how exactly the computer knows from python to display the text. What is the python3 command actually do here? Because if it isn’t used it does not work. Now my guess is that for other languages I need to use a command similar to python3 for each respective language. I am just not sure what the command is doing and I am hoping someone could explain it to me. Sorry if this is a dumb question, I am only thirteen and I am trying to get smarter.

35 Upvotes

16 comments sorted by

View all comments

3

u/A_kirisaki Feb 14 '21

Let's think how computer works with bottom-up approach.

First, basic computer is consisted with CPU and memory. Roughly speaking, to display letters needs to write data into memory, to read keyboard input needs to read data from memory. To calculate some values requires to translate data from memory to CPU and execute a instruction. These operations are written in machine code and CPU can read only it.

To call CPU instructions directly is bother to developers and unsecure to users, so OS wraps these instructions as system calls in other programming language (generally C-lang). C-lang and so on are compiled into machine code through assembly language.

Python itself is a compiled program written in C-lang (Of course, I know there are other implementations). It call system calls internally and operate computer.

You run python and it compiles your Hello.py code into format called bytecode which it can read easily first. Then, bytecode is parsed into AST (Abstract Syntax Tree). Finally, python decides what itself does by traversing AST.

1

u/scholastic_noob Feb 14 '21

Okay this is starting to make sense. Would you recommend learning machine code and working my way up to python then? That way I understand programming from the ground up.

2

u/A_kirisaki Feb 14 '21

It's just fine to understand this structure:

Python code -> Python bytecode -> Python AST -> OS System call -> CPU instruction
|-------------------- Python -----------------|------- OS ------|----- CPU -----|

It's not necessary to learn machine code because it's too difficult for human to read and write. And machine code is different between kinds of CPUs. Just only those who make assembler need to know it.

The one of best ways to understand how python works is to learn C-lang and read source code of CPython. This must be the long and winding road, so first you should be much good at python itself. If you can deal with python to a certain degree (can use class appropriately), you should learn C-lang concurrently.

The above is shortest way to understand how python works, but NOT how you enjoy programming. If you feel painful, try other programming languages in which you are interested.

1

u/spaztheannoyingkitty Feb 14 '21

Not OP but I recommend against that. Machine code is only something you would need to know of you were doing very very low level programming which is pretty uncommon. Understanding the concepts is good, but going and programming at that level would be a pain, especially for someone new to programming.

1

u/Jerome_Eugene_Morrow Feb 14 '21

Would you recommend learning machine code

Nah. This is kind of like how I don't need to know how to forge a steel frame to be able to ride a bike.

First step is fine to just figure out how a programming language like Python works. Get super comfortable until you feel like you can "express yourself" easily.

One day maybe you'll want to try something "lower level" like Java or C (like in a couple years maybe - and only if you have a reason you want to try them out), but when you're starting out as a programmer it's good to just pick one language and take your time with it until it's comfortable to build some stuff. After that you'll find the other languages are pretty similar, just with different things built in (or not built in).

Python's a really good starter language because it's got a lot of existing tools and you can focus on the logic part of programming, which is really the most important at the end of the day. If you never learn anything lower level than Python by the time you're 18 you'll still be way ahead of the curve as long as you can use Python well.

1

u/[deleted] Feb 15 '21

i wouldn't suggest seriously learning it, but i would reccomend exploring and messing about with it to get a better idea of how computers work.

Little Man computer