First Principles
Interpeter and compilers
What is a compiler and how does it differ from Interpreter?
To understand these two terms we need to understand what is a source code. Source Code is software that contains instructions that the computer follows to execute a task.
The compiler translates this source code into a machine code. You might have heard of binary bits (1, ones, and 0, zeroes) that the CPU can process and understand. The program to be translated is written inside an editor. This process is known is called compilation.
An interpreter is a program which also converts a high-level programming language (like Python, PHP, Perl) into machine code. Although similar to a compiler, the way that code is executed is different for both. Unlike a compiler that simply converts the source code to machine code, an interpreter can be run directly as an executable program. Contrary to a compiler, it converts source code to machine code when the program is running. When we execute a source code, Python compiles it into a byte code.
The Python compiler package is a tool for analyzing Python source code and generating Python bytecode. The compiler contains libraries to generate an abstract syntax tree from Python source code and to generate Python bytecode from the tree. … It can be modified more easily than the built-in compiler.
Numerical Operators
Numerical operators
After understanding numerical data types (integer, float), we now like to introduce Numerical operators. As a modern programming language, Python can perform the most basic numerical operations. We list them below and encourage you to try them out in the console:
Name | Operation | Example | Output | comments |
+ | Addition | 13 + 7 | 20 | |
– | Subtraction | 13 – 7 | 6 | |
* | Multiplication | 13 * 7 | 91 | |
/ | Float Division | 13 / 2 | 6.5 | |
// | Integer Division | 13 // 7 | 1 | 13 can be divided only once without the number becoming a float |
33 // 7 | 4 | 33 can be divided only four times without the number becoming a float | ||
** | Exponentation | 2 **3 | ||
% | Remainder | 13 % 7 | 6 | 13 can be divided only once times without the number becoming a float, the remainder will be 6 |
33 // 7 | 33 can be divided only four times without the number becoming a float, the remainder will be 5 |
String operators
Apart from numeric operators, Python also has string operators:
Let’s say we have to two variables called: “a” and “b”
a = hello
b = world
putting in the console
a+b will give: helloworld
3 * b will give: worldworldworld
Why not try the following out in the console:
X = goodbye
Y = friend
What will be the output of
X + Y
2 * X
String Operators
String operators
Apart from numeric operators, Python also has string operators:
Let’s say we have to two variables called: “a” and “b”
a = hello
b = world
putting in the console
a+b will give: helloworld
3 * b will give: worldworldworld
Why not try the following out in the console:
X = goodbye
Y = friend
What will be the output of
X + Y
2 * X
print()
“print()” function
You will be seeing the “print” function quite often so it would be good to give some explanation about it from the start. We will be discussing functions more elaborately in [insert link here].
As the word “print” is followed by “()” it means that this is a function name. A function in Python does two things:
- evaluate a value or more values
- execute an action
Using the “print ()” function will evaluate the tasks within the brackets and then show the outcome on the screen.
Functions take arguments. This is what we put in the brackets. The “print()” function also takes argument in the brackets.
The arguments that print takes can be different data types. You will read more about data types here.
While we are at it, let’s introduce another interesting function: input().
The input() function reads a line/value/variable entered on a console by an input device such as a keyboard and convert it into a string. This can then be used for further calculation/processing.