Python beginners course [Tutorial] – Learn python from scratch

This is summary of the python course I’m teaching on the video.
I did break down the lesson into 11 simple characters so you can easily follow it.

I suggest you to use this only as a review and follow the entire lesson on the video.

If you want to learn more about Python, I suggest you to follow the original documentation on https://www.python.org/doc/

Installation:

Ubuntu:
Type on the terminal:

sudo apt update && upgrade
sudo apt install python3

Windows:
On windows you need to download the latest version of python from this page: https://www.python.org/downloads/windows/ and then you can install it easily following the instructions you see on the page.

1. Arithmetic Operators

In python we use arithmetical operators to make common mathematical operations.

>>> 5 + 5  #Addition
10
>>> 20 - 5 #Subtraction
15
>>>10 / 2 #Division
5.0
>>> 10 * 5 #Multiplication
50
>>> 2 ** 3 #Exponentiation
8
>>> 100 // 51 #Floor division
1
>>> 100 % 51 #Modulus
49

2. Variables and Strings

We use variables to save informations. The process with wich we store an information is called “Variable assignement”.
The Strings are characters one after another. To define a string in python we put the value between quotes “this is a sring”.

animals = 15  # 15
print(animals)

sentence = "the cat is on the table"  #the cat is on the table
print(sentence)

3. Data types

The data types define what type of information the variable contains. If for example the variable contains an integer number, the type is “int”, if it’s text, the type is “str”.

>>> bootles = 5
>>> type(bootles)
int

>>> sentence = "Hi"
>>> type(sentence)
str

>>> minimumage = True
>>> type(minimumage)
bool

4. Boolean and Comparison Operators

The boolean is a data type with 2 possible values: True or False.
The comparison operators compare the values of two objects and return True or False.

minimumage= 18
myage = 17

myage > minimumage # False
myage < minimumage # True
myage == minimumage # False
myage != minimumage # True
myage >= minimumage # False
myage <= minimumage # True
 

5. If, Elif, Else

We use the condition If, Elif, Else to make decisions.
If executes the condition if it’s true.
Elif executes the condition if none of the previous ones is true.
Else executes teh condition if neither the If condition neither the Elif conditions are true.

minimumage = 18
userage = 20

if userage > minimumage:
    print("You are too young")
elif userage == minimumage:
    print("Just on time")
else:
    print("Welcome you're old")

6. For Loop

The For loop is used to iterate trough a list or a sequens so that we can access each single element individually.

numbers = [2, 5, 87, 4, 3, 5, 9, 1]

for n in numbers:
    print(n)

7. While Loop

The while loop is a loop that only stops when the condition is not true anymore.

count = 0
while 0 < 10:
    print(count)
    count += 1
print("Loop ended")

8. Break, Continue, Pass

The Break statement breaks the loop.
The Continue statement doesn’t execute the code that is after Continue if a certain condition is satisfied.
The Pass statement does nothing. We use it only if we want to keep the syntax but we don’t want to do anything wiht a loop or a funcion.

# Break
count = 0
while 0 == 0:
    print(count)
    count += 1
    if count == 10:
        break
print("Loop ended")

# Continue
programming_languages = ["Python", "C++", "Java", "Javascript", "C#"]
for p in programming_languages:
    print(p)
    if p == "Java"
        continue
print("Extra Code")

#Pass
programming_languages = ["Python", "C++", "Java", "Javascript", "C#"]
for p in programming_languages:
    pass

9. Functions

The Function contains a block of code which will run only if it is called later.
We can use functions to simplify the code for repetitive tasks.

def calculate_mean(num1, num2 ):
    result = (num1 + num2) / 2
    return result

result = calculate_mean(51, 31 )
print(result) # 41.5

10. Standard Library

Python has built-in a wide variety of functions and modules that can be user. These modules belong to the “Python Standard Library”.
You can read more about it on the official documentation: https://docs.python.org/3/library/

import time
time.sleep(5)
print("Thanks for waiting")


from math import sqrt
print(  sqrt(9) ) # 3.0 
 

11. Project: Language Translator

We’re going to build a Language Translator which translates english sentence into spanish in real time.

eng_esp = {"hi": "hola",
           "fine": "bien",
           "thanks": "gracias",
           "how are you": "como estas"}

print("Write a sentence and I'll translate it into Spanish for you:")
print("type  \"esc\" and press enter to quit")
while True:
    sentence = input()
    sentence = sentence.lower()
    
    if sentence == "esc":
        break
        
    for word in eng_esp:
        translation = eng_esp.get(word)
        sentence = sentence.replace(word, translation)
    print(sentence)