I am currently starting to learn the Python programming language, programming languages make up all the programs that we use, that is enough for it to fascinate me if it isn't for you then maybe start reading this and you will quickly see how powerful it is. This is my first programming language to learn (kinda) and this post is going to be my attempt at passing my learning of Python along to you as I go. This post will be updated as regularly as I go along learning. I am going from the ground up here so if you already know programming languages the beginning of this post may be boring for you, fear not as I plan to take this well beyond just beginners Python material as I work deeper into Learning Python which takes you pretty far into the language. This will be for my benefit as well so I will try my best, re-teaching what I am learning will help cement my knowledge of Python and more advanced people on here can tell me where I may be going wrong.
I will be using Python 3 and hopefully at some point we can draw on some of the main changes between Python 2 and Python 3 as it is my understanding that Python 2.x is still a widely used language.
I will probably make my fair share of mistakes going through this so please do not hesitate to share if I have messed something up or if any spelling/grammar is bugging you. Let's begin:
Python is an interpreted programming language as opposed to a compiled language such as C or C++. Let's take a basic look and see what happens to a Python program when you run it to understand this completely.
1. When you execute a program, it compiles the source code into something called 'byte code' which is a lower-level platform independent representation of the programs source code, this makes a .pyc file (source code files are just .py). The .pyc file is your source code compiled into 'byte code'.
2. Your byte code is then ready for something called the Python Virtual Machine (PVM) which is the runtime engine of Python, the PVM is just a loop that works through the byte code and this is where each instruction that you made is carried out.
*Keep in mind you don't do any of this, this is what happens between the time when you double click or otherwise execute a Python script/program and when what it is running shows up on your screen*
Everything happens as the program runs in Python, even function declarations, whereas in a compiled language like C++, it all happens before execution. Compiled languages get compiled straight to machine code which in turn makes them a lot quicker than Python.
The .pyc files that are made when you execute a program are stored on your computer (in the same directory that your source code file is in, and you should be able to see them) your source codes have timestamps on them and if they have not been changed since the last time your computer has run the source Python skips step 1 and just runs the .pyc file to save time and computer resources.
There are variations that alter these steps as well like 'Jython', which takes your Python source code and spits out a Java compiled file instead of Python compiled code. There are a few different types of these but it will probably be more beneficial to go into after we have gotten Python down.
Another brief intro to a few different types of programming language right here on this website that I found useful to begin: https://null-byte.wonderhowto.com/how-to/novice-guide-teaching-yourself-program-learning-resources-included-0131969/
In Python a hierarchy of how things are put together is as follows:
A Program is made up of modules ---> Modules are made up of statements ----> Statements are made up of expressions ---> expressions create and process objects.
The way that I am learning it, and going to be regurgitating it to you all, is currently from the bottom to the top, so we will be starting with Objects.
Quick note first, the import command. Since all your .py files are considered modules, you use the import commands to load file1.py information and make it available to file2.py to use. Import is an expression not an object we are just jumping ahead to this since it is used extensively and will be used while learning. The expression 'import' is processing the 'object' 20 stored in the variable x in file1 seen below:
Every/Any Python file ending in .py is considered by Python to
be a 'module' and other files can use the contents of this
file by 'importing' the files contents into what you are using, be it an interactive environments or another Python file etc. There is one main file that is your main file, this is the one you would click to launch etc and it would have all the other modules that you need imported so you can use them all. This modules idea is a foundational concept in Python so keep that in mind. Below is an example of import being used at the interactive prompt. You must be in the directory (folder) that your file is in to be able to import it.
In this case, there is a file named file1.py and all I have written in it is x = 20, once I import this I can now use print(attribute) command, as you can see print simply takes the parameter of file1.x kinda like a web address of where this variable is stored.
This may be a bit fuzzy but is just kind of a prerequisite to move along so if you think I suck at explaining this just read on, these things should become clearer as we go.
Python is dynamically typed, this means that it keeps track of the type of objects that are made and you do not have to set it explicitly (you do in other languages).
However it is also strongly typed meaning that once your object is automatically assigned its type, you can only perform operations that are allowed for its respective type.
(More to come, sorry for the short update!)
Want to start making money as a white hat hacker? Jump-start your hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from cybersecurity professionals.
Other worthwhile deals to check out: