Python for Non-Programmers
Agenda
1 Installing Python 5 Object Oriented Programming
2 Python Basics 6 Numerical Computing with NumPy
3 Data Structures in Python 7 Data Manipulation with Pandas
4 Python Functions 8 Data Visualization with Matplotlib
How do Humans Communicate?
hello Olá
Bonjour नमस्कार
Grammar in Language
Every Language has Grammar associated with it
I am Sam Am Sam I
Language for Computers
Java Python
C++
Syntax for Computer Language
import pandas as pd
pandas import pd as
Why do we need Programming?
Why do we need Programming?
Why do we need Programming?
Applications of Programming Languages
Gaming Banking Machine Learning
What is Data?
13.4
My Name is Sam
287
(a+b)2 0 1
How to Store Data?
“John”
123
TRUE
Need of Variables
Data/Values can be stored in temporary storage spaces called variables
Student
“John”
0x1098ab
Example of Variable
a+b
10 a
a-b
Multiple operations on the variables
20 b a*b
a/b
Decision Making Statements
If
else
It’s raining:
Go out and Play Football
Sit inside
Decision Making Statements
If
else
Marks > 70:
Give Practice Test
Get Ice-cream
if…else Pseudo Code
If(condition){
Statements to be executed….
}
else{
Statements to be executed….
}
Looping Statements
Looping statements are used to repeat a task multiple times
Looping Statements
Looping Statements
While Loop Pseudo Code
while(TRUE){
Keep executing statements….
}
Functions in Real Life
Eating Running Cycling
Functions in Programming World
Function is a block of code which performs a specific task
Deposit Function to deposit money
Withdraw Function to withdraw money
Balance Function to check balance
Object Oriented Programming
Classes
Class is a template/blue-print for real-world entities
Properties Behavior
• Color • Make Calls
• Cost • Watch Videos
• Battery Life • Play Games
Objects
Objects are specific instances of a class
Apple Motorola Samsung
How do you solve a problem?
Step by Step Approach
1 Check if you have all the ingredients
2 Take lemon and cut into two halves
3 Squeeze lemon into a glass of water
4 Add sugar and stir it well
5 Serve it cold with ice cubes
What is an Algorithm?
Step by step approach to solve a problem is known as algorithm
Input Steps to be followed Output
Algorithm to find if number is even/odd
Start
Take Number
If number
Even Number Odd Number
% 2 == 0
End
Introduction to Python
Cross-Platform
Compatible
Free & Open Source Large Standard Library
Object Oriented
Installing Python
This is the site to install Python -> https://www.python.org/downloads/
Installing PyCharm
This is the site to install PyCharm -> https://www.jetbrains.com/pycharm/
Installing Anaconda
This is the site to install Anaconda -> https://www.anaconda.com/
Intro to Jupyter Notebook
Jupyter Notebook is a browser-based interpreter that allows us to interactively
work with Python
Variables in Python
“John”
“Sam”
“Matt”
Variables in Python
Data/Values can be stored in temporary storage spaces called variables
Student
“John”
0x1098ab
Variables in Python
Data/Values can be stored in temporary storage spaces called variables
“John” “Sam” “Matt”
DataTypes in Python
Every variable is associated with a data-type
10, 500 3.14, 15.97 TRUE, FALSE “Sam”, “Matt”
int float Boolean String
Operators in Python
Arithmetic Operators
Relational Operators Logical Operators
Python Tokens
Smallest meaningful Component in a Program
Keywords
Identifiers
Literals
Operators
Python Keywords
Keywords are special reserved words
False class Finally Is Return
None continue For Lambda Try
True def From Nonlocal While
and del Global Not With
as elif If Or Yield
Python Identifiers
Identifiers are names used for variables, functions or objects
Rules
No special character expect _(underscore)
Identifiers are case sensitive
First Letter cannot be a digit
Python Literals
Literals are constants in Python
Python Strings
Strings are sequence of characters enclosed within single quotes(‘ ’), double
quotes(“ “) or triple quotes(‘’’ ‘’’)
‘’’ I am going to
‘Hello World’ “This is Sparta”
France tomorrow’’’
Extracting Individual Characters
String Functions
Finding length of string Converting String to lower case Converting String to upper case
String Functions
Replacing a substring
Number of occurrences of substring
String Functions
Finding the index of substring
Splitting a String
Data-Structures in Python
Tuple List
Dictionary Set
Tuple in Python
Tuple is an ordered collection of elements enclosed within ()
tup1=(1,’a’,True)
Extracting Individual Elements
Modifying a Tuple
You cannot modify a tuple because it is immutable
Tuple Basic Operations
Finding Length of Tuple
Concatenating Tuples
Tuple Basic Operations
Repeating Tuple Elements
Repeating and Concatenating
Tuple Functions
Minimum Value
Maximum Value
List in Python
List is an ordered collection of elements enclosed within []
l1=[1,’a’,True]
Extracting Individual Elements
Modifying a List
Changing the element at 0th index Popping the last element
Appending a new element
Modifying a List
Reversing elements of a list Sorting a list
Inserting element at a specified index
List Basic Operations
Concatenating Lists
Repeating elements
Dictionary in Python
Dictionary is an unordered collection of key-value pairs enclosed with {}
Fruit={"Apple":10,"Orange":20}
Extracting Keys and Values
Extracting Keys
Extracting Values
Modifying a Dictionary
Adding a new element
Changing an existing element
Dictionary Functions
Update one dictionary’s elements with another
Popping an element
Set in Python
Set is an unordered and unindexed collection of elements enclosed with {}
s1={1,"a",True}
Set Operations
Update one dictionary’s elements with another Removing an element
Updating multiple elements
Set Functions
Union of two sets
Intersection of two sets
If Statement
If
else
It’s raining:
Go out and Play Football
Sit inside
If Statement
If
else
Marks > 70:
Give Practice Test
Get Ice-cream
if…else Pseudo Code
If(condition){
Statements to be executed….
}
else{
Statements to be executed….
}
Looping Statements
Looping statements are used to repeat a task multiple times
Looping Statements
Looping Statements
While Loop
Syntax:
Enter While loop
while condition:
Test Execute Statements
Expression
Exit While loop
True
Body of While
For Loop
For Loop is used to iterate over a sequence(tuple, list, dictionary..)
for val in sequence:
Body of for
Functions in Real Life
Eating Running Cycling
Python Functions
Function is a block of code which performs a specific task
Deposit Function to deposit money
Withdraw Function to withdraw money
Balance Function to check balance
Python Object Oriented Programming
Classes
Class is a template/blue-print for real-world entities
Properties Behavior
• Color • Make Calls
• Cost • Watch Videos
• Battery Life • Play Games
Class in Python
Class is a user-defined data-type
int float
bool str
Mobile
Attributes and Methods
color
Attributes
cost
Play Game
Methods
Make Call
Objects
Objects are specific instances of a class
Apple Motorola Samsung
Objects in Python
Specific instances of Mobile data type
Apple Motorola Samsung
a = 10 b = 20 c = 30
Specific instances of integer data type
Creating the first Class
Creating the ‘Phone’ class
Instantiating the ‘p1’ object
Invoking methods through object
Adding parameters to the class
Setting and Returning the
attribute values
Creating a class with Constructor
init method acts as the constructor
Instantiating Object
Instantiating the ‘e1’ object
Invoking the
‘employee_details’
method
Inheritance in Python
With inheritance one class can derive the properties of another class
Man inheriting
features from his
father
Inheritance Example
Creating the base class
Instantiating the object for base class
Inheritance Example
Creating the child class
Instantiating the object for child class
Invoking the child class method
Over-riding init method
Over-riding init method
Invoking show_details()
method from parent class Invoking show_car_details()
method from child class
Types of Inheritance
Single Inheritance
Multiple Inheritance
Multi-level Inheritance
Hybrid Inheritance
Multiple Inheritance
In multiple inheritance, the child inherits from more than 1 parent class
Parent 1 Parent 2
Child
Multiple Inheritance in Python
Parent Class One Child Class
Parent Class Two
Multiple Inheritance in Python
Invoking methods
Instantiating object of child class
Multi-Level Inheritance
In multi-level Inheritance, we have Parent, child, grand-child relationship
Parent
Child
Grand-Child
Multi-Level Inheritance in Python
Grand-Child Class
Parent Class
Child Class
Multi-Level Inheritance in Python
Invoking class methods
Instantiating object of GrandChild class
Libraries in Python
Python library is a collection of functions and methods that allows you to perform many actions without writing your code
NumPy
Python NumPy
NumPy stands for Numerical python and is the core library for numeric and
scientific computing
NumPy
Creating NumPy Array
Single-dimensional Multi-dimensional
Array Array
Initializing NumPy Array
Initializing NumPy array with zeros
Initializing NumPy Array
Initializing NumPy array with same number
Initializing NumPy Array
Initializing NumPy array within a range
Initializing NumPy Array
Initializing NumPy array with random numbers
NumPy-Shape
Checking the shape of NumPy arrays
Joining NumPy Arrays
vstack() hstack() column_stack()
Numpy Intersection & Difference
NumPy Array Mathematics
Addition of NumPy Arrays
NumPy Array Mathematics
Basic Multiplication
Basic Addition
Basic Subtraction Basic Division
NumPy Math Functions
Mean Standard Deviation
Median
NumPy Broadcasting
Numpy Array Addition
NumPy Broadcasting
10 20 30 40 50 + 5
10 20 30 40 50
5 5 5 5 5
NumPy Broadcasting
NumPy Broadcasting
NumPy Matrix
NumPy Matrix Transpose
NumPy Matrix Multiplication
NumPy Save & Load
Saving Numpy Array
Loading Numpy Array
Python Pandas
Pandas stands for Panel Data and is the core library for data manipulation and data
analysis
Pandas Data-Structures
Single-dimensional Multi-dimensional
Series Object Data-frame
Pandas Series Object
Series Object is one-dimensional labeled array
Changing Index
Series Object from Dictionary
Changing index position
Extracting Individual Elements
Extracting a single element Extracting elements from back
Extracting a sequence of elements
Basic Math Operations on Series
Adding a scalar value Adding two Series Objects
to Series Elements
Pandas Dataframe
Dataframe is a 2-dimensional labelled data-structure
Creating a Dataframe
Dataframe In-Built Functions
head()
shape() describe()
tail()
.iloc[]
.loc[]
Dropping Columns
Dropping Rows
Combining Data-Frames
Creating first dataframe
Creating second dataframe
Data-Frames Concatenation
Axis=0
Data-Frames Concatenation
Axis=0, Sort = True
Data-Frames Concatenation
Axis=1
Data-Frames Merge – Inner Join
Inner Join
Data-Frames Merge – Left Join
Left Join
Data-Frames Merge – Right Join
Right Join
Data-Frames Merge – Outer Join
Outer Join
More Pandas Functions
Mean Minimum
Median Maximum
More Pandas Functions
More Pandas Functions
Value_counts()
sort_values()
Pokemon Analysis
Understanding Data
Looking at Null Values
Imputing Null Values
Checking Frequency
Renaming Columns
Extracting Primary Types
Extracting Primary & Secondary Types
Extracting Specific Pokemons
Python Matplotlib
Matplotlib is a python library used for data visualization
Line Plot
Line Plot
Adding Title and Labels
Line Plot
Changing Line Aesthetics
Line Plot
Adding two lines in the same plot
Line Plot
Line Plot
Adding sub-plots
Bar Plot
Bar Plot
Adding Title and Labels
Horizontal Bar Plot
Horizontal Bar Plot
Scatter Plot
Creating a basic scatter-plot
Scatter Plot
Changing Mark Aesthetics
Scatter Plot
Adding two markers
in the same plot
Scatter Plot
Adding sub-plots
Histogram
Creating data
Making Histogram
Histogram
Changing Aesthetics
Histogram
Working with a dataset
Box-Plot
Creating data
Making Histogram
Making Plot
Violin-Plot
Creating data
Making Histogram
Making Plot
Pie-Chart
Creating data
Making Plot
Pie-Chart
Changing Aesthetics
DoughNut-Chart
Creating Data
Making Plot
SeaBorn Line Plot
SeaBorn Line Plot
Grouping data with ‘hue’
SeaBorn Line Plot
Adding Styles
SeaBorn Line Plot
Adding Markers
SeaBorn Bar Plot
SeaBorn Bar Plot
SeaBorn Bar Plot
SeaBorn Bar Plot
SeaBorn Bar Plot
SeaBorn Scatterplot
SeaBorn Scatterplot
SeaBorn Scatterplot
SeaBorn Histogram/Distplot
SeaBorn Histogram/Distplot
SeaBorn Histogram/Distplot
SeaBorn Histogram/Distplot
SeaBorn Histogram/Distplot
SeaBorn JointPlot
SeaBorn JointPlot
SeaBorn JointPlot
SeaBorn BoxPlot
SeaBorn BoxPlot
SeaBorn BoxPlot
SeaBorn BoxPlot
SeaBorn BoxPlot
SeaBorn BoxPlot
SeaBorn Pair Plot
Case Study
Case Study
We will have a case study on this census dataset
Case Study
Case Study
Case Study
Case Study
Renaming Columns
Case Study
Extracting Individual Columns
Case Study
Case Study
Case Study
Case Study
Case Study
Case Study
Case Study