Course 1 – Introduction to Python

Hardware

The computer contains a lot of hardware pieces, such as the CPU (Central Processing Unit), the HDD (Hard-disk), RAM (Random Access Memory) and so on – each of them having their own functionality.

Hard Disk Drive (HDD)

HDD is the persistent memory (long term) – everything we have on the computer (files, applications) can be found there – the data remains stored on the disk even if we close the computer.

To make an analogy with the real world, we can imagine the HDD like a room in an apartment. Let’s say we have an apartment with 2 rooms, 50 mp2 (storage room – like the HDD size). We buy some furniture and place it on the room, and it stays there even after we leave the room. (Furniture = our files).

Random Access Memory (RAM)

RAM is the volatile memory (short term) – it stores the data for the applications that are currently running on the computer. Let’s take the browser, for example: We start it, and a new process is created by the operating system. It then receives a piece of this RAM to use to load itself in memory, and store information such as the data we see on the screen when we browse the web.

As an analogy, we can see it as the hallway in our apartment. When we want to do some exercises, we take the dumbbells, take them on the hallway, and start exercising. When we’re done, we take them from there and move them back to the storage closet.

Central Processing Unit (CPU)

The CPU is the one that is doing all the work – it’s the brain of the computer.

He can perform 3 operations:

  • Fetch: Read an instruction from RAM
  • Decode: Decode instruction
  • Execute: Execute the instruction (arithmetic, data comparison, moving data in memory)

CPU can only understand 0s and 1s. What are those things more specifically?

Well, that’s binary.

Inside the computer, there are cables and circuits that transport data – using electricity to do so.

If we have only one circuit through which the electricity is moving, the signal can be either on or off. Based on this, we can represent the value of 0 (closed – electricity is not moving through it), and 1 (open, electricity is moving through it).

Software

We speak about the CPU and how it understands only 0 and 1 – but we’re writing the software side – and we don’t use 0s and 1s.

When we write code, we use text – language specific words. That text need to be transformed in 0 and 1 for the CPU to understand it.

Compiled and Interpreted languages

Interpreted languages need a tool (interpreter) to execute the text, line by line – more specifically, it transforms it in 0 and 1, and sends the instruction to be further processed.

Compiled languages are using a compiler to transform the text in 0 and 1, before the application is run. After the code is there, you run the compiler, and it creates an executable (process/binary) – a file that can be run on the CPU.

Long story short – the code needs to be transformed in 0 and 1 – the difference between these two is the moment that is done – one is performed before being executed (compiled), and the other in real time (interpreted)

About Python

Python is an interpreted language. In order to run our code, we will need an interpretor – I recommend PyCharm, or you can use something online for the mooment, such as https://www.onlinegdb.com/online_python_interpreter

The first python program can consist of a single line:

This code will display Hello World on the screen.

Data types in Python

In python, when we declare data, we need to give them a name and a value.

int

Let’s say we want to have a variable called number, of type integer, with a value of 5.

bool

The boolean type (bool) is a data type that can only have 2 values, True or False.

string

string is a set of characters, and we can use it to represent a word, a text, and so on.

Comments

In python, we can comment a line using #

Comments tell the compiler not to execute the line – they are mainly messages for other programmers.

User input

We saw how to display something to the screen, but we also need to get some value from the keyboard. This is performed using the input method.

Indentation

Indentation is the way Python recognizes what is a block of code. Other programming languages usually use { and } to do the same.

We need to use the same number of spaces in the same block of code, otherwise we will get an error!

Let’s talk about conditionals and we’ll see an example of indentation right away!

Conditional statement: If, else if, else

The next step would be to talk about conditionals: if, else if, else.

As an analogy:

  • If it’s raining outside, i’ll take an umbrella with me.
  • If it’s not raining, but it’s snowing, i’ll take my jacket.
  • Else (not raining, not snowing), i’ll take my hat.

Let’s say we want to check if a variable’s value is positive or negative.

Notice that if the value is positive, the lines that follow are the code that needs to be executed when the conditional is true – and they are indented, as they form a separate block of code.

Loops: While

Let’s talk about loops. While is a loop that validates a condition, and execute the block of code inside the condition. Afterwards, the whole process is repeated.

As an analogy:

  • While it’s raining, i’m holding an umbrella over my head.

Let’s say that we want to print a value as long as it’s less than 10, and then increment it (so we will stop at some point).

You may also like...