r/AskComputerScience • u/scholastic_noob • 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.
7
u/slashcom Feb 14 '21
It's actually a super super deep question. Others have done a good job explaining, but just want you to know it's a very good question.
3
u/scholastic_noob Feb 14 '21
Oh that is such a relief. I am trying to be smarter because I am tired of being the “dumb kid” in my class. So I am glad this was a good question. I was worried people were going to think I was dumb lol.
2
Feb 14 '21
Trust me this question can go as deep as you want. From the simple answers they gave you above on how python is interpreting your code, to the whole branch of computer science related to compilers, programming languages, language processing and more, and that’s an area where some of the smartest minds work, it’s complex.
1
u/Jerome_Eugene_Morrow Feb 14 '21
I was worried people were going to think I was dumb lol.
Nah. I think you'll find this sub is pretty supportive of younger coders. We were all in your position at some point in our life, a lot of us when we were in our twenties or later - so you're ahead of the curve there.
Some other subs that might be useful for you as you're getting started:
Good on you for trying to improve yourself through learning! It'll take you a really long way in your life, no matter what anybody else thinks of you. :)
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
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.
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.
3
u/Miitch__ Feb 14 '21
To keep it simple: your computer has what's called an interpreter that can translate python code into a language that your computer is able to understand and run. When using the command in the terminal you are telling your computer to use the python interpreter to run the program written in the specified file. As you correctly guessed, other languages work in a similar way
1
1
u/Destroyer_The_Great Feb 15 '21 edited Feb 15 '21
I cant really help you with your question too much but just wanted to offer recouses and tips. I was at this point a while ago. I would suggest learning c++ first and once fundamentals are down move on to python, at least then you can go from compiler to interpreter. It's a but of a chuck in the deep end but worth it. I wasn't a python programmer until a few months ago and it was really simple to learn python once I had c++ knowledge. It all really depends on what your writing and what your gonna use it for. I use c++ mostly for game development. I would suggest learning c++. Could I also suggest these tutorials: https://youtu.be/HBxCHonP6Ro for python and the same channel does other languages too. If you follow along It will help you guaranteed. The solo learn app will also help you along pretty nicely too: https://play.google.com/store/apps/details?id=com.sololearn
Edit: If you do go the c++ route I would suggest using the Dev-C++ compiler, fast, easy to use and customizable. It is pretty outdated but my favourite ide then there is microsoft visual studio wirch I use for my bigger projects and projects requiring libraries. Dev-C++: https://sourceforge.net/projects/orwelldevcpp/
Visual Studio: https://visualstudio.microsoft.com/
29
u/[deleted] Feb 14 '21
python3 Hello.py runs the python interpreter on Hello.py. so it runs a program that goes through the text in Hello.py and inteprets it according to pythons syntax/rules. if you type python3 on its own it opens the python interpreter, and you can type python code and will do the stuff accordingly.
You are correct that every language has its own command, however not all work the same way pythons does. your computer comes with its own set of instruction and you can give it sequences of these instructions and it will go through them one by one and execute them. .this functionality is part of the computers actual circuit board see Assembly . some other languages commands run a program called a compiler which goes through the text and produces a program written in your computers basic list of instructions.