Before we start learning Python programming language, it is important to learn a few things about how it works under the hood, and that is exactly what we are going to discuss in this article. So let's get into the details of how python works.
What is a programming language?
So, before we dive straight into the working of python programming language, let's first see what is a programming language.
Programming languages are High-level or Low-level languages depending on how close they are to computers or the English language as we speak. Language, like Assembly, is known as low-level language as it is closer to the system and language like Python or JavaScript are High-Level language that means these languages are closer to the English language.
Programming is basically giving instructions to the computer. So Programming language(s) is nothing but the language(s) in which we give our computer some sort of instructions. You can easily understand it by comparing programming languages to the languages that we human speak, why do we have languages for us?... Right, so that we can communicate, now computers don't speak so we have programming languages through which we communicate with our computers.
- But how does the computer understands programming language(s)?
Now, this question might have popped up in your head, well to be honest computers don't understand any programming language... wait what? but you just said that programming languages are the way to communicate with computers and now they don't understand programming language(s) Yeah you're right we (humans) use a programming language(s) as a way to communicate with our computer(s), but computers only understand the language of 0's and 1's (called binary language) which takes us to our second question.
How does programming language(s) work?
Now as we know that computers only understand binary language, the question is, then how does the programming languages work?... Let's take a look at what happens after you write some code in a programming language.
As soon as you write your program and try to run it, it is needed to be converted into binary language, so that our computer can understand and perform the specified task. Now, this conversion is done by another program that is either Compiler or Interpreter.
Compiler: A compiler takes our code and converts it into machine-readable code at once.
Interpreter: An interpreter converts our code into machine-readable code line by line.
There are a lot of programming languages, some are compiled that is they use a compiler and some are interpreted that is they use an interpreter. Now, if you want to know more about compiled and interpreted programming languages you can read it here. But for now, all we need to know is that Python is an interpreted language. So let's get to the next section and learn how Python works under the hood.
Python Interpreter
Okay... it's time to see how the Python program runs using an interpreter under the hood.
So when we first download python on our system (I'll show you at the end), we actually download CPython (interpreter for python written in c-language). When you write a python program and run it, it is first converted line by line into byte code (machine level language, closer to the system) by the interpreter, then that byte code is run inside CPython VM (installed with python) and then that code runs into our machine and we see the output. If it is confusing for you don't worry I'll explain it by writing our very first python program in the next section.
Installing Python on our system
Before we write our very first python program let's just first install python on our system. Follow the steps below to download python on your system.
i. Head over to python.org
ii. Go to the downloads section and download the latest version available
I have already installed python on my system. So, I can not show you it installing but, when you install it on your system make sure you check the option to add it to the environment variable
Now you don't have to install python on your system at the exact moment you start learning, what you can do is sign up for an account on repl.it and use it as your code editor until you feel comfortable writing code in python and then you can install Python or hey you can install various code editors available for python like PyCharm which is a really great tool but first you need to be comfortable with writing code in python so make an account on repl.it and then read the next section to write your first python code and better understand the python interpreter.
Our very first Python Program
Whenever we start learning a new programming language, we always start with the "Hello World" program so let's write our Hello World program in python language.
#write the below line into a new file in repl.it and see the output
print("Hello World")
When we run the above code, it is converted to byte code by the interpreter, and that byte code looks like this:-
0 LOAD_GLOBAL 0(print)
2 LOAD_CONST 1('Hello World')
4 CALL_FUNCTION 1
6 POP_TOP
8 LOAD_CONST 0(None)
10 RETURN_VALUE
Then this byte code goes and run into the CPython VM and then eventually runs on our system and prints "Hello World" on the screen.
Now you don't need to know or understand the byte code. But, it's good to know how things work underneath the hood.
Thanks for taking your time and reading this article and I hope you got to know something interesting from this article and I could have helped you.