LEARN PYTHON: CHAPTER TWO

Now that you’ve had a very simple taste of Python, it is time we become familiar with some of the basics and necessities required to understand most, if not all programming languages. Python was written by Guido Van Rossum a retired Dutch programmer, who has mostly also retired from maintaining Python.

Python is a “multi-paradigm” programming language, what this means is that several styles of programming are supported – styles like Object-oriented programming, structured programming, and functional programming. While these various styles are supported, they are not to be confused with each other, they can be used simultaneously, but it is better to to stick with one style of programming when possible.

DATA TYPES

Python is one of the best languages out there because it handles data types very well. To put it into laymen’s terms, data types are the types of data that is stored. A quick example would be printing the term “Hello World”. Whenever we enclose a a set of characters within quotes, Python will automatically categorize this datatype as a String. If we use a variable to store this value, that variables datatype would be returned as a string.

#!/usr/bin/python3 
import os
import sys 

# a function that multiplys two inputs
def multiplyNumbers(a, b)
    return a * b

# storing an int
myInt = 1

# storing a float
myFloat = 0.5

# storing a string 
myString = "Hello String"

a = 4
b = 5

# calling a function that returns an int (any value)
myFunction = multiplyNumbers(4, 5)

# calling a function that returns an int (variables values)
myFunctionVars = multiplyNumbers(a, b)

print(f"An Int: {myInt}")
print(f"A float: {myFloat}")
print(f"A string: {myString}")
print(f"Calling a function that returns a value: {myFunction}")
print(f"Calling a function that returns a value from variables: {myFunctionVars}")

From the above code, you can see how easily python handles various datatypes, you can set the datatype and just return values without being explicit of what those values are. In other programming languages such as C or C++, datatypes are handled explicitly.

#include <stdio.h>
/* include "headers" or "library" which allow your 
programming to use the input and output functionality of your system */
  
// multiply int function which receives two parameters and returns the multiplied value
int multiplyNumbers(int x, int y) {
	return x * y
}
  
// main function which receives no parameters but runs your main program
int main(void) {
	int a = 4, b = 5;
    float myFloat = 0.5;
    char myString[] = "Hello String";
  
    // calling function with input
    int myFunction = multiplyNumbers(4, 5);
      
    // calling function with vars
    int myFunctionVars = multiplyNumbers(a, b);
    
    // output all of our statements
    printf("myInt: %d %d", a, b);
    printf("myString: %s", myString);
    printf("myFunction: %d", myFunction);
    printf("myFunctionVars: %d", myFunctionVars);

    return 0;
}

As you can see, in C each datatype has to be explicitly declared when using them, a decimal is declared as float, and integer is declared as int, a string is declared as char myString[] and almost every statement in your main(void) { ... } has to end with a semicolon terminator. While this is necessary for C programming, in Python, a variable can be declared and any datatype can be associated without explicitly defining the datatype, this is where many programmers find Python to be more elegant and beautifully and dynamically typed in nature versus C/C++, and other programming languages statically typed nature. So when you hear or read about dynamic typing, or static typing, keep these ideas in mind and it will help you understand more of the “why” it matters.

Why does it matter?

– You

Depending on the application you are writing, it does matter, much of the software that exists today (at least publicly) utilizes data acquired or requested from the internet.

Data TypeValue
Integer or Int120
Float1.251436
Array[1,2,3,4,5]
Dict{ 1 : 2 }, {“1” : “2”}, {“1” : “two”}
String“Hello World!\n”
Commonly used data types

Language Differences

Python is also a little different in comparison to other languages. To simplify, let us look at Python and C/C++. Python is an interpreted language, what this means is that Python doesn’t require a compilation phase in order to run, instead, it is quickly executed within the “Python Interpreter” (also known as the Python Interactive Shell). The interpreter takes the code you write, and converts it into an intermediate language which is then again translated into machine language that is executed. Python code you write is compiled into a .pyc file (byte code). Normally your computer doesn’t understand Python by default. After translation, this code is executed much faster, and is then ran inside of the Python Virtual Machine (which is essentially the runtime of Python).

C and C++ execute code written by you the developer a little more differently, with these languages it is necessary for you to compile your code after it is written. While this is a machine dependent process, this code can be compiled in several different ways, but the most common way to compile your code is by using the Gnu C Compiler, or GCC. GCC is responsible for translating all of the code you have written into machine code.

The above diagram displays what the GNU C Compiler is doing when it is compiling code. First the sourcefile (.c) is written in human readable code. The compiler is ran and it first runs through the preprocessor. The preprocessor produces a (.i) file, which is then sent to the compiler. The compiler produces a (.s) file which is assembly code that gets sent to the assembler. Once that happens, the assembler produces a (.o) object code, and the GNU C compiler also produces (.lib, .a) files for linking the libraries. Finally, the object code and the libraries are linked together and an executable is created. Again libraries are used to tell the program to use something from the system, like say, input and output (input devices such as your keyboard or mouse. Output devices such as your screen or monitor), etcetera.

C involves a lot more when writing programs, where as Python removes the complication and just lets you focus on writing simple, beautiful, and fast code without focusing so much on semantics or system complexity. It breaks down the barriers of programming and lets you focus on the problem you are trying to solve.

So thats it for this chapter, you’ve learned a little bit about data types and why they are important. In the next chapter we will learn more about Variables, Arrays, Loops, and Functions.

This should be considered as sort of a living book, some things aren’t perfect but they are subject to change, some things can be removed, improved, or added. If you have any suggestions or questions feel free to comment or contact me.