Introduction to python
By:
Irandufa Indebu
[email protected]
Outlines
Python Language overview
Machine Learning with python
Overview of python language
Python is a high-level scripting language which can be used for a
wide variety of text processing, system administration and
internet-related tasks.
Python is a true object-oriented language, and is available on a
wide variety of platforms.
Python support dynamic data type
It is Independent from platforms
High-level internal object data types & Automatic memory
management
Why python?
Python has become the lingua franca for many data science
applications.
Python is reach in libraries for data loading, visualization,
statistics, natural language processing, image processing, and more
This vast toolbox provides data scientists with a large array of
general- and special-purpose functionality.
One of the main advantages of using Python is the ability to
interact directly with the code, using a terminal or other tools like
the Jupyter Notebook, which we’ll look at shortly.
Why python?
Machine learning and data analysis are fundamentally iterative
processes, in which the data drives the analysis.
It is essential for these processes to have tools that allow quick
iteration and easy interaction.
As a general-purpose programming language, Python also allows
for the creation of complex graphical user interfaces (GUIs) and
web services, and for integration into existing systems.
Special Feature of python
Python statements do not need to end with a special character;
The python interpreter knows that you are done with an
individual statement by the presence of a newline, which will be
generated when you press the “Return” key of your keyboard.
Thus, Use a newline to end a line of code
Some statement such as if statement might not end with one line,
thus we have to use \ at the end of the line to tell the python the
line is continuing.
Special Feature of python
Python uses indentation (that is, the amount of white space
before the statement itself) to indicate the presence of loops,
instead of using delimiters like curly braces ({}) or keywords (like
“begin” and “end”) as in many other languages
The amount of indentation you use is not important, but it must
be consistent within a given depth of a loop, and statements which
are not indented must begin in the first column.
Variables in python?
Variables are nothing but reserved memory locating to store
value
This means when you create the variable, you reserve some space
in memory
Based on the data types of the variables, the interpreter allocates
memory and decides what can be stored the reserved memory.
Assigning values to variables in python
In python, no type declarations, just assigned!
Variable types don’t need to be declared. Python figures out the
variable types on its own
Once a variable is assigned string, it only accepts values
with string type
So, you can’t just append an integer to a string. You must first
convert the integer to a string itself.
Basic Data type in python
Data stored in memory can be of many types
Python has various standard data types that are used to define the
operation possible on them and the storage method for each of
them.
Python has 5 standard data types:
1. Numbers 4. Tuple
2. Strings 5. Dictionary
3. List
Basic Data type in python
1. Numbers: Used to store numeric values
Types of numbers:
Python supports several different numeric types
A. Integers (default for numbers): Examples: 0, 1, 1234, -56
Note: dividing an integer by another integer will return only the
integer part of the quotient, e.g. z = 5 / 2 # Answer is 2, integer
division.
Basic Data type in python
B. Floating point numbers Examples: 0., 1.0, 3.456, 1e10,
3.14e-2, 6.99E4
Division works normally for floating point numbers:
x = 7./2. = 3.5
Operations involving both floats and integers will yield floats:
y = 6.4 – 2 = 4.4
Basic Data type in python: Operation on numbers
Basic algebraic operations arithmetic operations (like C/C++): a+b, a-
b, a*b, a/b, a%b
Exponentiation: a**b
Comparison operators: Greater than, less than, etc.: a < b, a > b, a <= b, a
>= b
Identity tests: a == b, a != b
To add or subtract: +=, -= etc… (no ++ or --) sum += x
Basic Data type in python: Operation on numbers
Assignment using = (but it has different semantics!) a = 1 a = "foo" #
OK
You can also assign to multiple names at the same time.
Basic Data type in python: Strings
2. Strings data types
Characters are strings of length 1
There is no char type like in C++ or Java
Strings are ordered blocks of text Strings are enclosed in single or
double quotation marks E.g.: 'abc', “abc” (they have the same meaning)
Use triple double-quotes for multiple line strings or strings that contain
both ‘ and “ inside of them:
Basic operation on Strings
Concatenation and repetition
Strings are concatenated with the + sign
Strings are repeated with the * sign:
Methods
Len(‘string’) – returns the number of characters in the String
Basic operation on Strings
Methods
str(Object) – returns a String representation of the Object
‘string’.upper() - string operations to perform some formatting
operations on strings:
Basic Data type in python: Substrings
Python starts indexing at 0. A string s will have indexes running
from 0 to len(s)-1 (where len(s) is the length of s) in integer
quantities.
s[i] fetches the ith element in s
s[i:j] fetches elements i (inclusive) through j (not inclusive)
Basic Data type in python: Substrings
s[:j] fetches all elements up to (not including) j
s[i:] fetches all elements from i onward, inclusive
Basic Data type in python: Substrings
s[i:j:k] extracts every kth element starting with index i (inlcusive)
and ending with index j (not inclusive)
Sequences
Sequence represents ordered set of objects indexed by
nonnegative integers
It includes strings, lists and tuples
Strings are sequences of characters
Lists and Tuple are sequences of arbitrary python object such as
numbers, strings, and/or nested sub lists
Lists are mutable and allow insertion, deletion and substitution
of elements.
– Strings and tuples are immutable
Compound data types: Lists
Lists are ordered collection of data, contained in square brackets []
Lists in Python look and act a lot like arrays in other languages
Basically, it allows us to group a bunch of values under one variable
name
The values can be any python objects
Unlike arrays in many languages which all must be of the same type
We can create a list by using square brackets and separating the values
with commas:
Numbers: L1 = [0,1,2,3]
Strings: L2 = ['zero', 'one']
Nested sublists: L3 =[0,1,[2,3],'three',['four,one']]
Nothing/empty: L4 = []
Compound data types: Lists
Having a list of a bunch of values does us little good unless we
can access the individual elements of the list
We can access individual elements by their index
to print element at index 1
Lists are mutable: individual elements can be reassigned in
place. Moreover, they can grow and shrink in place
Operations on Lists
Index and slice assignment:
Indexing: L1[i], L2[i][j]
Slicing: L3[i:j]
Concatenation:
Repetition:
Built-in Functions on Lists
Sorting: sorts items of mylist in place
Reversal: reverse items of l in place
Shrinking:
Appending:
Built-in Functions on Lists
Nested List
Example:
Note: it is possible to use append, delete, update and reverse in
nested list, however impossible to use sort method.
Tuples
Tuples are contained in parentheses ()
Tuples can contain
• Numbers: t1 = (0,1,2,3)
• Strings: t2 = ('zero', 'one‘)
• Nested sub-tuples: t3=(0,1,(2,3), 'two‘ ,(‘ six, one'))
• Nothing (Empty) : t4 = ()
As long as you're not nesting tuples, you can omit the
parentheses
• Example: t1 = 0,1,2,3 is the same as t1 = (0,1,2,3)
Tuples are immutable: individual elements cannot be
reassigned in place.
Operations on Tuples: basic operations
Tuple indexing works just like string and list indexing
Indexing: L1[i]
Concatenation: same with list
Repetition: same with list
Length: (this also works for lists and strings) len(t1)
Note: we cannot use append operation for tuples
To check whether the given element is within the tuple or not, use
elements to be checked followed by in followed by tuple name;
Example: >>t=(25, 20, “Hello”)
>>25 in t
Return True, Note: This can works for lists too (try it..).
Dictionary
Dictionaries consist of key-value pairs (an unordered set of
key/value pairs)
A Dictionary consists of a name, followed by a key, which then
hold a certain amount of values
A key is surrounded by square brackets ( [ ] ) while values are
surrounded by curly braces ( { } ).
A dictionary works in a similar way to a real dictionary. You
can sort of think of a key as if it were a word, and then the value as
the definition of the word, or the value that the word holds.
Dictionary
A key can be almost any python data type, but are often
words or numbers, while the values held by a dictionary can
be anything.
“dict” we declare each key and value separately.
As you can see its very similar to a real dictionary, where we
essentially create a word and attach a value to it.
Dictionaries can be contained in lists and vice versa.
Dictionaries are unordered and mutable
Dictionary
The main difference between dictionary and list is: list is
ordered sequence, whereas dictionary is unordered sequence
In addition, the main difference is that items in dictionaries are
accessed via keys and not via their position.
The values of a dictionary can be any type of Python data
Output
Dictionary
In the above examples, Amharic words such as አምበሳ, ድመት,
ደፓርትመንት, ማስተማር …. Are called keys whereas their
corresponding definition in Afaan-Oromo is called Values.
Note that, the Keys of a dictionary are unique
Unlike lists, which keep your objects arranged in the order in which
you inserted them, Python’s dictionary does not
This means you cannot assume that the rows in any dictionary are in
any particular order; for all intents and purposes, they are unordered.
Dictionary
Remember: a key in the dictionary is used to look up a
value.
Thus, unlike lists, which use numeric index values to access
data, dictionaries use keys to access their associated data
values
output
Dictionary
It is possible to add the elements to the dictionary
However, whenever we add the new element, we have to add
it with the key/value format
In this case we have to use the square bracket instead of curly
bracket.
Example: amharic_oro_dict[ “አስተዳደር”]=“Bulchiinsa”
This will add the አስተዳደር/Bulchiinsa to our previous dictionary
of amharic_oro_dict.
Dictionary
Generally, insertion order has no meaning in dictionary
Like that of lists, it is possible to grow or shrink the dictionary
Accessing data in a dictionary uses the square bracket
notation. Put a key inside square brackets to access its
associated value.
Python’s for loop can be used to iterate over a dictionary. On
each iteration, the key is assigned to the loop variable, which
is used to access the data value.
Python Operator
Arithmetic operators
For arithmetic operations the usual symbols are used (such as +, -
, *, /, %).
Special use of + for string concatenation.
Special use of % for string, integer and float formatting
Logical operators
For Logical comparison the following words are used (such as
and, or, not)
Impossible to use symbols such a (&&, ||, !) like in Java
Python Operator
Assignment operators
Assignment operators is = (e.g. x=8)
Comparison uses ==if x == 8: sum += x
Naming rules in Python
Names are case sensitive and cannot start with a number.
They can contain letters, numbers, and underscores.
bob Bob _bob _2_bob_ bob_2 BoB
There are some reserved words:
and, assert, break, class, continue, def, del, elif, else, except, exec,
finally, for, from, global, if, import, in, is, lambda, not, or, pass,
print, raise, return, try, while, range …
The basic output printing command is “print.”
Basic input command: raw_input() for python2, for python3 input()
Output in Python: The print Statement
You can print a string to the screen using print
Elements separated by commas print with a space between them
Elements separated by commas print with a space between them
“Print” automatically adds a newline to the end of the string. If
you include a list of strings, it will concatenate them with a space
between them
How to take input from the user
We use raw_input for python version 2 and input for python v.3
The input(string) method returns a line of user input as a
string
The parameter is used as a prompt
Exercise;
write a program to ask the user for an integer, and then print
out whether that number is EVEN or ODD
Solution?
Basic Statements: The If Statement
# inside a script
if condition:
action
Notes: No Braces???
blocks delimited by indentation! Python uses indentation instead
of braces to determine the scope of expressions
All lines must be indented the same amount to be part of the
scope (or indented more if part of an inner scope). This forces
the programmer to use proper indentation since the indenting is
part of the program!
Basic Statements: The If Statement
A statement typed into an interpreter ends once an empty
line is entered, and a statement in a script ends once an
unindented line appears. The same is true for defining
functions
colon (:) used at end of lines containing control flow keywords
(such as if, elif, else, while, …)
Basic Statements: The If Statement
The following asks Students for their grade.
The indented block will only be executed if the grade is “A". If the
Students enters another grade, nothing happens.
In the above program, we used or operation to enable our
program to accept student grade in small letter or capital letter.
Basic Statements: The if/else Statement
In the if/else statement, we have two condition: the if part and the
else part
The else part is returned if the if part is failed (false)
Syntax: if condition: Example
statement 1
else:
statement 2
Here, the poor is returned if the student entered grade
other than “A” or “a” letter.
Note: both if and else should be in the same position
(indentation)
Basic Statements: if/elif/else Statement
If statements can be combined with else if (elif) and else statements
as follows:
if condition1:
action1
elif condition2:
action2
else:
action3
Note: if we have more condition, we use elif in python ,
not else if like in java or c++
Basic Statements: Combining If Statement
Conditions in if statements may be combined using and & or
statements
if condition1 and condition2:action1 # if both
condition1 and condition2 are true, execute action1
if condition1 or condition2: action2 # if either
condition1 or condition2 is true, execute action2
Conditions may be expressed using the following operations:
<, <=, >, >=, ==, !=, in
Basic Statements: The While Statement
While statements have the following basic structure:
# inside a script
while condition:
action
As long as the condition is true, the while statement will
execute the action
While statements are intended to be used with changing
conditions.
If the condition in a while statement does not change, the
program will be stuck in an infinite loop until the user hits ctrl-
C.
Basic Statements: The While Statement
inserting elements (items) to list using while loop:
output
This snip-set code insert the elements automatically to the empty
list we created.
Output will be: [0, 1, 2, 3, 4]
Note: 5 will not be included since we are using greater operator.
While it the i value reach 5, the iteration exit.
Basic Statements: The For Statement
For statements have the following basic structure:
for item i in set s:
action on item i
Example: for i in range(1,7):
print (i, i**2, i**3, i**4)
Basic Statements: The For Statement
The item i is often used to refer to an index in a list, tuple, or
array
Example:
L = [0,1,2,3] # or, equivalently, range(4)
for i in range(len(L)):
L[i] = L[i]**2
print L
The value of L is updated to L = [0,1,4,9]
Of course, we could accomplish this particular task more compactly
using arrays:
>>> L = range(4)
>>> L = L**2
>>> L[0,1,4,9,]
Basic Statements: Loop Control Statements
Basic Statements: Break Statement
Break statement is used to come out of loop like for, while.
Example:
i=0 Output
while (i<10):
i+=1
print(i)
if (i==5):
break
print(“Loop exited”)
Remember: It is possible to use break statement with
other statements such as for loop, if statements even
with function
Basic Statements: continue Statement
‘Continue’ statement skips the current iteration of loops like for, while.
Example output
Remember: It is possible to use continue statement
with other statements such as for loop, if
statements even with function
Function in Python
Functions are objects and treated like any other variable in
python, the def statement simply assigns a function to a variable
Usually, function definitions have the following basic structure:
def funcName(parameter_list):
return values //function body
The parameter list consists of none or more parameters.
Parameters are called arguments, if the function is called. The function
body consists of indented statements
The function body gets executed every time the function is called.
Function in Python
Example:
Function body
Here, we called the function
using its name and we
passed the arguments
Here, we created the
function with no argument
Function variables
The variable that is declared inside the function is called local
variable
It is impossible to use local variable outside the function
These local variable is executed if only their function is called
The variable that is created outside the function is called global
variable
We may use global variable inside the function but the reverse is
not true
x=10 # here x is global variable def myfunc():
def myfunc(): x=10 #local variable
#function body print(x)
Function in Python
Example: write the program that calculate your body mass
index (BMS) using function
Solution ?
The range() Function
The built-in function range() is the right function to iterate over a sequence of
numbers
It generates an iterator of arithmetic progressions: Example
range(5) output= range(0, 5)
We can use it in a for loop and you will see what is meant by this:
This program will print out the number from 0 to 4
range(n) generates an iterator to progress the integer numbers starting with 0
and ending with (n -1).
The range() Function
range() can also be called with two arguments:
range(begin, end)
The above call produces the list iterator of numbers starting with begin
(inclusive) and ending with one less than the number end .
Example: print(range(4, 10))
The output of this program is list of number from 4 to 10
including 4 but excluding 10.
The range() Function
So far the increment of range() has been 1.
We can specify a different increment with a third argument
The increment is called the step . It can be both negative and positive,
but not zero: Syntax: range(begin, end, step)
Example: print(range(4, 50, 5))
Output= 4, 9, 14, 19, 24, 29, 34, 39, 44, 49
Note: It can be done backwards as well:
File Handling in python
Computer file is a chunk of logically related data or
information which can be used by computer programs.
Usually a file is kept on a permanent storage media, e.g. a
hard drive disk…
unique name and path is used by human users or in
programs or scripts to access a file for reading and
modification purposes.
Files: Input & Output
input = open(‘data’, ‘r’) Open the file for input
S = input.read() Read whole file into one String
S = input.read(N) Reads N bytes (N >= 1)
L = input.readline() Returns a list of line strings
input.close() Close opened file for reading
output = open(‘data’, ‘w’) Open the file for writing
output.write(S) Writes the string S to file
output.writeline(L) Writes each of the lines in list L to
file
output.close() Close opened file for writing
Reading and writing a file in python
The syntax for reading and writing files in Python is similar to programming
languages like C, C++, Java, Perl, and others but a lot easier to handle.
We will start with writing a file.
Note: whenever we write a file, we have to use triple quato in our python
IDLE
From the above code, if you see in your folder, it create the text file with the
name myfile having the above contents.
Reading and writing a file in python
For other language such as Amharic letter, unless we format the
encoding, python will not write to the file.
Thus, we have to use encoding value to utf-8 which is common for
such language scripting. See the following example:
If we didn't use encoding, python interpreter cause error for this code.
Reading and writing a file in python
Now, we want to see how to read this file from Python.
We can read the whole text file into one string, as you can see in the
following code
text=open(“myfile.txt”).read()
Note: if our code and our file is in different place, we have to
use the full path of our file location.
If you call print(text) , you will see the text from above again in
python console.
Reading in a text file in one string object is okay, as long as the
file is not too large
If a file is large, we can read in the file line by line
See the next code ….
Reading and writing a file in python
Some people don't use the with statement to read or write
files. This is not a good idea. The code above without with
looks like this:
Both achieve same task, however, they are different.
With the second code, we used close, however if we use
with we don’t need to close the file.
Reading and writing a file in python
Thus, with our first code, the file will be closed
automatically, when the with blocks ends.
Therefore, better to use with to open the file
Possible to use loops with file
while loop expression for file reading:
f = open(filename, "r")
line = f.readline()
while line: # do something with line
print line
line = f.readline()
f.close()
open() and file()are identical:
f = open(filename, "r")
f = file(filename, "r")
Instead of using while loop to iterate through file, more
concisely one can write with for loop:
for line in file(filename, "r“):
print line
Python modules
Module is a file, which contains definitions. We can define classes,
modules, variables etc., in module file and reuse them in other python
scripts.
Importing modules
Import to access other codes
Such as: import sys, import string, import math, import
random
Math functions: Many math functions in the math module
>>> import math
>>> dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2',
'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod',
'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow',
'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
>>>
Importing modules
As program gets longer, need to organize them for easier access and
easier maintenance.
Reuse same functions across programs without copying its
definition into each program
Python allows putting definitions in a file
use them in a script or in an interactive instance of the interpreter
Such a file is called a module
definitions from a module can be imported into other modules or
into the main module
Modules
Reusing code is key to building a maintainable system.
And when it comes to reusing code in Python, it all starts and
ends with the humble function and module
Take a collection of functions and package them as a file, and
you’ve got a module (which can also be reused).
A module is a file containing Python definitions and statements.
The file name is the module name with the suffix .py appended.
Within a module, the module’s name is available in the global
variable __name__.
How to Import modules?
Python: classes and objects
Class is a blue print to create objects.
Syntax
class ClassName :
< statement- 1 >
.
.
< statement- N>
‘class’ keyword is used to define a class
You can instantiate any number of objects from a class
See the syntax from next slide …
Python: classes and objects
objName = new ClassName(arguments). See this Example
Python: classes and objects
In the above code, we created the Class called Employee
Under the Employee line, we created two function: one in
which we define the attribute of the class, and the other where
we instantiated the class.
Lastly we created different objects such as emp1, emp2,
emp3… from the class by passing the argument as the
constructor.
Whenever we create the object, the created object will take
the behavior of its parent or its Class.
Python: classes and objects
__init__(arguments)
__init__ is a special function called constructor used to
initialize objects.
In Employee class, __init__ method is used to initialize id,
firstName, lastName to an object at the time of creation.
Nb. Use double underscore to write init function.
_init_ and __init__ are different
Use the second one.
Python: classes and objects
Instance variables
Instance variables have values unique to an object. Usually these
are defined in __init__ method
The previous Employee class has 3 instance variables id,
firstName, lastName
Note: The first parameter of any method in a class must be self
This parameter is required even if the function does not use it.
‘self’ is used to refer current object.
Python: classes and objects
Instance variables VS Class variable
Class variables are associated with class and available to all the
instances (objects) of the class
where as instance variables are unique to objects, used to
uniquely identify the object
Here, Organization is Class variable which can be accessed by all
objects
Python libraries
Python provides its large library support for various stages in data
science
Python libraries contain various tools, functions, and methods to
analyze data.
Each library has its focus on data mining, data visualization, etc.
Data processing and Machine Data visualization Libraries:
Leaning Libraries: • Matplotlib
• NumPy • Seaborn
• Pandas
• Pydot
• SciPy
• Scikit-learn • Plotly
Python libraries for machine learning
1. NumPy
NumPy is the fundamental package for machine learning with
Python
numpy is an acronym for Numerical Python.
NumPy is a very popular python library for large multi-
dimensional array and matrix processing, with the help of a
large collection of high-level mathematical functions.
It is very useful for fundamental scientific computations in
Machine Learning
Python libraries for machine learning
It offers powerful tools including the following:
The N-dimensional array ndarray class and several subclasses
representing matrices and arrays
Various sophisticated array functions
Useful linear algebra capabilities
How To import numpy ?
Python libraries for machine learning
2. Pandas
Pandas is a free Python data analysis and data handling software
library.
Pandas provides a variety of high-performance and convenient data
structures along with operations for data manipulation.
The standard form to import the pandas module is:
Note: pd is just an alias. Any name can be used as an alias.
Python libraries for machine learning
Load and examine the dataset:
DataFrame is a two-dimensional array where each feature (column) is
simply a shared index series.
A DataFrame can be viewed as a generalization of the NumPy array.
A dataset can be read and loaded to the pandas DataFrame using the
below code syntax:
The first glance at the dataset can be made using the following
functions:
Python libraries for machine learning
i. head() – This built-in function displays the first five rows by default. Passing
any integer in the function will display that many certain rows starting from the
top.
ii. tail() – This built-in function displays the last five rows by default. Passing any
integer in the function will display that many certain rows from the bottom.
iii. sample() – This built-in function displays a random row from the dataset.
Passing any integer in the function will display that many random rows.
iv. info() – This built-in function displays a complete summary of the dataset,
including the no. of rows, columns, the data types of the features, the null values
v. describe() – This built-in function displays comprehensive statistics that sums
up the central tendency, the shape of the dataset, and dispersion
Thank
s