MAchine Learning
Section (1&2) – Python Basics
Installing Python
We’ll be using the Anaconda Python distribution,
which can downloaded through this link:
[Link]
d-section
Download the Graphical Installer of Python 3.8
according to your system (32-Bit or 64-Bit).
2
Installing Anaconda
3
Installing Anaconda
4
Installing Anaconda
5
Opening Anaconda
6
Opening Jupyter Notebook
7
Opening Jupyter Notebook
8
Having trouble with Anaconda or Jupyter?
If you’re having trouble setting up or installing
Anaconda, you can use this instead:
[Link]
9
Variables
? Declaring variables in Python is easy. You don’t need to
specify the datatype, you just type the variable name and
assign a value to it.
x=1
y = 1.99
check = True
name = “Ahmed”
favorites = [“Apples”, 3, True]
info = {“name”: “Mostafa”, “age”: 24, “hobby”: “Video games” }
10
Printing a variable or a message
? Using the print function, you can output a message, a
variable, or a combination to the console.
print(name)
Result : Ahmed
11
Accessing items in lists or dictionaries
? To access an item in a list, you specify the index between
square brackets. While to access an item in a dictionary, you
specify the name of the key of the value you want.
print(favorites[1])
print(info[“name”])
Result :
3
Mostafa
12
Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Exponentiation (power)
// Floor division
13
Arithmetic Operators
print(1 + 1)
print(1 – 1)
print(2 * 3)
print(6 / 2)
print(4 % 2)
print(5 // 2)
Result :
2
0
6
3
0
2 14
Conditionals
? If conditionals in Python are similar to other languages,
but instead of curly brackets, white space is used.
Also, else if statements are called elif.
if(check):
print(“This is true”)
if(x < 0):
print(“Less than 0”)
elif(x == 0):
print(“Equals 0”)
else:
print(“Greater than 0”)
Result :
This is true 15
Greater than 0
Logical Operators
Operator Equivalent to
and &&
or ||
not !
if ((x == 1 and y == 1.99) or (not(check))):
print(“One of the conditions above is true.”)
16
Loops
? While while loops stays the same, for loops on the other
hand are a bit different.
while(check):
print(“This is an infinite loop”)
for i in range(10):
print(“This will loop 10 times”)
for x in favorites:
print(x)
for key, value in [Link](): 17
print(“Key: ” + key + “ Value: ” + value)
Comments
? In Python, # is used for single line comments, while three “
are used for multi-line comments
# This is a single line comments
“””
This
is
a
multi-line
comment
“””
18
Example
list_of_favorites = [
{“name”: “Ahmed”, “likes”: ”watermelon”},
{“name”: “Michael”, “likes”: “apples”},
{“name”: “Sara”, “likes”: “grapes”}
{“name”: “Mariam”, “likes”: “mango”}
]
for person in list_of_favorites:
print(person["name"] + " likes " + person["likes"])
19
Try it out yourself
? Code:
[Link]
PyP5bucYbkdNPa6PrP8g?usp=sharing
20
Thank you for your attention!
21
Further reading
? [Link]
? [Link]
22