Python tutorial

Hello world

print("Goodby World")
Goodby World

Variables:

x = 11 #Without type specification
y: int = 11 #With type specification
print(x)
print(y)
11
11

Strings

nev: str = "WaveG"
print(nev)
print(f"First character is: {nev[1]}")
print(f"Reversed: {nev[::-1]}")
WaveG
First character is: a
Reversed: GevaW

String interpolation:

print(f"There are {y} monkeys")
There are 11 monkeys

Operators

a = True
b = False
print(a and b)
print(a or b)
print(a and b)
print(a is b)

x = 1
x += 12
print(x)
False
True
False
False
13

For cycle

This is the part where the most controversial thing come in python. The indentation There aren't any curly brackets. The only thing that says wich line is in the cycle is the indentation, wich is 4 space long

for i in range(12):
    print(i)
0
1
2
3
4
5
6
7
8
9
10
11

There is a foreach in python as well

list = [1, 2, 3, 4, 5] #It's like an array we will se it later
for number in list:
    print(number)
1
2
3
4
5
list = [] #It's like an array we will se it later
for number in list:
    print(number)
else:
    print("empty list")
empty list
i = 1
while i < 11:
    print(i)
    i += 1
1
2
3
4
5
6
7
8
9
10

Arrys alias lists

list = [1, 2, 3, 4]
for number in list: #Interating through the list
    print(number)

print(list)
print(list[1]) #2nd element of the list
print(list[-1]) #last element of the list
print(list[1:-1]) #2nd to last element of the list
1
2
3
4
[1, 2, 3, 4]
2
4
[2, 3]

Funcions

def square1(num):
    return num**2

def square(num: int) -> int:
    return num**2

print(square1(2))
print(square(4))
4
16

You can store function (pointers) in variables

func = square
func(6)
36