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

11

u/Cybyss Feb 14 '21 edited Feb 14 '21

"Python" is two things:

  • it's the name of the language you're writing in, but

  • it's also the computer program named Python3.exe

The program Python3.exe takes in a .py file, reads all of the Python language commands you typed into it, and then performs those actions on your computer.

When you type "python3 Hello.py" into the command prompt, that is telling Windows to run the Python3.exe program on the Hello.py file.

Now my guess is that for other languages I need to use a command similar to python3 for each respective language.

Only "interpreted" languages like Python or Java. That is, those which have an "interpreter" program for them like Python3.exe or Java.exe to read the code files and perform the actions described therein.

Some languages, however, come with a "compiler" program instead of an "interpreter" program. C and C++ are probably the most popular of this category.

There is one special language, called "x64 machine code", that your computer hardware understands natively. It's a binary language that isn't human readable. The .exe files on your computer are much like .py or .java or .cpp files, but which contain x64 machine code instead of Python or Java or C++ code.

When you run a compiler program on a C++ code file, it gets translated into x64 machine code which is then written to a .exe file you can run.

That's about the gist of it, at least.


I should point out... I have oversimplified a lot of steps.

Executable (.exe) files in particular are in reality a lot more complicated than merely a sequence of x64 machine language instructions.

Interpreter programs are also a bit more complicated. In Java for example, your .java code files are first translated into a special "JVM Bytecode" language (also a binary non human-readable language). This is what the Java.exe interpreter program actually reads.

It turns out Python works the same way, but its bytecode translation step is done automatically for you and so you don't see it, whereas in Java it has to be done manually.