Machine Learning Lab - CS633
Introduction to Python
● Guido van Rossum created it in 1991
● It is an interpreted language
● Code can be written in a procedural, object-oriented,
or functional way
● File extension: .py
Please follow the below guidelines for basic Python understanding:
1. Python release for different Operating systems
For Windows: [Link]
For Mac OS: [Link]
For Linux/Unix:
[Link]
2. Running your first Python program:
>>> print("Hello, World!")
Hello, World!
>>> print('Hello, World!')
Hello, World!
3. Python Comments:
● Single line comments
○ #this is my comment
● Multiline Comments
○ """This is a
comment written in
more than just one
line """
4. Python Variables:
● must start with a letter or the underscore character,
cannot begin with a number
● can only contain alpha-numeric characters and underscores
● names are case-sensitive
●
>>> x = 5
>>>
print(x) 5
>>> y = 'Python'
>>>
print(y)
Python
>>> print(type(x))
<type 'int'>
>>> print(type(y))
<type 'str'>
5. Python Lists
● List items are ordered, changeable, and allow
duplicate values
● List items are indexed, the first item begins index 0
>>> fruits = ['apple', 'banana', 'cherry']
>>> fruits
['apple', 'banana', 'cherry']
>>> type(fruits)
<type 'list'>
>>>
fruits[0]
'apple'
>>>
fruits[-1]
'cherry'
>>>
len(fruits) 3
>>> [Link]('data')
>>> fruits
['apple', 'banana', 'cherry', 'date']
6. Python Tuple
● Tuple items are ordered, unchangeable, and allow
duplicate values
>>> fruits = ("apple", "banana", "cherry")
>>> fruits
('apple', 'banana', 'cherry')
7. Python Set
● Set items are unordered, unchangeable, and do not
allow duplicate values
>>> fruits = {"apple", "banana", "cherry", "apple"}
>>> fruits
set(['cherry', 'apple', 'banana'])
8. Python Dictionary
● Dictionary items are ordered, changeable, and do not
allow duplicates
● Dictionary items are presented in key: value pairs, and
can be referred to by using the key name
>>> fruits = {"apple": 1, "banana": 2, "cherry": 3}
>>> fruits
{'cherry': 3, 'apple': 1, 'banana': 2}
>>>
fruits['apple'] 1
9. Python Conditions and If-else
statements x, y = 2, 5
if x > y:
print('x >
y') elif x == y:
print('x is equals to
y') else:
print('y > x')
Output:
y > x
10. Python Loops
● For loop
fruits = ["apple", "banana",
"cherry"] for x in fruits:
print(x)
Output:
apple
banan
a
cherr
y
● While loop
fruits = ["apple", "banana",
"cherry"] i = 0
i += 1
Output: (0,
'apple')
(1, 'banana')
(2, 'cherry')
For writing and executing code, use either Google Colab or Jupyter
Notebook
1. Jupyter Notebook
● [Link]
● Install the Jupyter Notebook with
○ pip install notebook
● To run the notebook
○ jupyter notebook
● Tutorial
○ [Link]
2. Google Colab
● Basic Overview
○ [Link]
_o [Link]
● Tutorial
○ [Link]
● “Welcome to Colab” Notebook
○ [Link]
Introduction to Numpy
● [Link]
● Install the Numpy with
○ pip install numpy
● Numpy stands for Numerical Python It is created by was created
in 2005 by Travis Oliphant as a python library that is used for
working with arrays
● Python lists serve the purpose of arrays, but they are slow
to process
● Array object in NumPy is ndarray
● Numpy supports a lot of mathematical functions
including algebraic operations, matrix multiplication,
and random matrix/array generation
Numpy Tutorial
[Link]
jG wUB-SzfwSBM3Q#scrollTo=siFNOWna0LS5&line=1&uniqifier=1
Introduction to Pandas
● [Link]
● Install the Pandas
with
○ pip install
pandas
● It was created by Wes McKinney in 2008
● It is a Python library that is used for working with data sets
● It has functions for analyzing, cleaning, exploring,
and manipulating data
Pandas Tutorial
[Link]
jG wUB-SzfwSBM3Q#scrollTo=RcIdXL84Eai_&line=3&uniqifier=1