Data Analytics
Fundamentals
Second Technical
Session
▪ Python for Data Science, AI & Development
▪ Python Basics
Agenda ▪ Python Programming Fundamentals
▪ Activity: Hands-On Python Lab
▪ Working with Data in Python
▪ Quiz
2
Python for Data Science, AI
& Development
3
▪ Open Source
▪ Easy to Understand
▪ OOP and Functional Programming
Why Python? ▪ Full-Stack
▪ Flexibility
▪ Pre-Built Models and Libraries
4
Python Basics
5
Python Basics
Numeric Data Types
▪ Python support three types of numeric data types - int and float.
▪ int is short for integer. Integers are whole numbers. They don’t
have any decimal part.
▪ When the numbers have a decimal part, as in 3.14 or 2.5, it is called
a float data type.
▪ Complex number is represented by a complex class. It is specified
as (real part) + (imaginary part)j. For example – 2+3j
Boolean Data Types
▪ Python also has a bool data type which stands for Boolean values. It
can take only two values. True or False.
String Data Types
▪ String is a collection of characters. In Python, it is referred to as
“str” data type.
6
Python Basics
Lists
▪ List is a collection of objects. The objects can be int, str, float, bool, or even one of the
collection objects. Each object can be of a different type.
▪ List is created with square brackets around the collection, with each element of the
collection separated by a comma.
▪ Lists are mutable. You can change them from the original form. You can do this by adding
elements to the list, deleting elements from the list and inserting element at a desired
position in the list.
▪ [1,”two”,3.5,True,1,”two”]
7
Python Basics
Tuples
▪ Tuple is also a collection of diverse type of objects. The objects can be int, str, float, bool
or even one of the collection objects.
▪ Tuple is created with rounded brackets around the collection, with each element of the
collection separated by a comma.
▪ Tuples are immutable. You cannot change them from the original form. Tuples are most
useful when you want to create a collection object which you don’t want to be changed
for the life of the application.
▪ (1,”two”,3.5,True,1,”two”)
8
Python Basics
Set
▪ Set is also a collection of diverse type of objects. The objects can be int, str, float, bool or
a tuple.
▪ A Set is created with curly brackets around the collection, with each element of the
collection separated by a comma.
▪ Sets are mutable. You can change them from the original form. You add elements, update
elements, remove elements from the set. But the elements in a set have to be
immutable. That is why you cannot have a list or set as an element inside a set.
▪ {1,”two”,3.5,True}
9
Python Basics
Dictionary
▪ Dict stands for dictionary. Dict is a special collection object which maintains a key-value
pairs. The key is a unique identifier and the value is an object. The objects can be int, str,
bool, float or any other objects. There can be more than one copy of the same object. But
the keys have to immutable objects and have to be unique. Usually, str objects are used
as keys. No two objects can have same keys.
▪ Dicts are mutable. You can change them from the original form. You add new key value
pairs, update the value of a key, remove a key value pair from the dict.
▪ {"a":"This is first object","b":1,"c":2.5,"d":True,"e":[1,2,3],"f":(4,5,6)}
10
Python Programing Fundamentals
11
Python Programing Fundamentals
12
Python Programing Fundamentals
13
Activity: Hands-On Python LAB
▪ Google Colab - https://colab.research.google.com/
14
Python Programing Fundamentals
'if' Statement
If the statement is true, you can enter the room, and your program
can run some predefined task.
If the statement is false, your program will skip the task within the
"if" body.
'if..else' Statement
If the statement is false, your program will skip the task within
the "if" body and perform whatever is in the "else" part of the
body.
The program will then continue to execute with rest of the
statements if any.
15
Python Programing Fundamentals
if…elif...else Statement
The elif statement, short for “else if,” allows us to check
additional conditions if the proceeding condition is false. If the
condition is true, the alternate expressions will be run.
Nested if Statement
We can have a if...elif...else statement inside another
if...elif...else statement. This is known as Nesting. Any number
of these statements can be nested inside one another.
Indentation is the only way to figure out the level of nesting.
Nested if statements must be avoided unless necessary.
16
Python Programing Fundamentals
for Loop
he for loop in Python is used to iterate over a sequence (list,
tuple, string) or other iterable objects. Iterating over a
sequence is called traversal
17
Python Programing Fundamentals
while Loop
The while loop in Python is used to iterate over a block of code
as long as the test expression (condition) is true.
18
Python Programing Fundamentals
break Statement
The break statement terminates
the loop containing it. Control of
the program flows to the
statement immediately after the
body of the loop. If the break
statement is inside a nested loop,
it will terminate the innermost
loop.
19
Python Programing Fundamentals
continue Statement
The continue statement is used to skip the rest of the code inside a loop for
the current iteration only. Loop does not terminate but continues with the
next iteration.
20
Python Programing Fundamentals
Functions
In Python, a function is a group of related statements
that performs a specific task and can be reused.
Functions help break our program into smaller and
modular chunks. As our program grows larger and
larger, functions make it more organized and
manageable.
If we define a function to do the task, we just need to
call the function.
Scope and Lifetime of Variables
Parameters and variables that are defined outside of
any function are said to be within the global scope,
meaning they can be accessed anywhere after they are
defined. Parameters and variables defined inside a
function are not visible from outside the function.
Hence, they have a local scope
21
Python Programing Fundamentals
Types of Functions
Built-in Functions: Functions that are built into
Python. These are readily available for use.
There are a number of built-in functions
available in Python.
User-defined Functions: Functions that are
defined by users for their use.
22
Python Programing Fundamentals
Objects and Class
Python supports The Object-Oriented Programming
(OOP) approach.
Almost everything in python is an object, with its
properties and methods.
An object has two characteristics:
a. attributes
b. an internal data representation (a blueprint)
A class is a blueprint for the object. To create a class,
we use the keyword class.
An object (instance) is an instantiation of a class. When
class is defined, only the description for the object is
defined. Therefore, no memory or storage is allocated.
Methods are functions defined inside the body of a
class. They are used to define the behaviours of an
object.
23
Working with Data in Python
Numpy stands for numeric python. It provides
many useful methods that can be used for
data processing specifically when the data is
in the form of a 1 or 2-dimensional numeric
array. This package is also installed by default.
But will have to be imported to use in your
python notebooks.
24
Working with Data in Python
Pandas is Python's main tabular data library. All data
that can be structured in a tabular format will be
using Pandas to process the data in Python. It
provides fast, flexible, and expressive data structures
designed to make working with structured (tabular,
multidimensional, potentially heterogeneous) and
time-series data both easy and intuitive.
Pandas package doesn't require installation. It is
installed by default with your Anaconda installation.
But pandas will have to be imported to use in a
notebook.
import pandas as pd
25
Working with Data in Python
The data that is loaded with pandas is
accessed through a dataframe. Pandas
offer read methods to read the data into a
dataframe object. Once the data is read into
a dataframe object, the data can be viewed,
explored and manipulated.
Like the other data types and objects, we
have seen in earlier sections of the course,
dataframes also allows user to invoke
methods on it.
• info()
• describe()
26
QUIZ
Q1. What will be the output of the
following Python code snippet?
x=5
y=3
print(x ** y)
A) 15
B) 125
C) 8
D) 53
28
Q2. Which of the following is the correct
way to create a list in Python?
A) list = list(1, 2, 3)
B) list = (1, 2, 3)
C) list = [1, 2, 3]
D) list = {1, 2, 3}
29
Q3. Consider the following code snippet.
What will be the output?
def multiply(a, b):
return a * b
print(multiply(2, 10))
A) 20
B) 2
C) 10
D) None of the above
30
Q4. What is the output of the following
code?
my_string = "Hello, World!"
print(my_string[7])
A) H
B) e
C) W
D) ,
31
Q5. What is the correct output of the
following code snippet?
list = [1, 2, 3, 4]
list.insert(2, 'a')
print(list)
A) [1, 2, 'a', 3, 4]
B) [1, 'a', 2, 3, 4]
C) [1, 2, 3, 'a', 4]
D) TypeError
32
Q6. What does the following code print?
for i in range(3):
print(i)
else:
print('Done')
A) 0 1 2 Done
B) 0 1 2
C) 1 2 3 Done
D) 1 2 3
33
Q7. What will be the output of the
following code?
a = [1, 2, 3]
b=a
b[0] = 'x'
print(a)
A) [1, 2, 3]
B) ['x', 2, 3]
C) [1, 'x', 3]
D) TypeError
34
▪ Hands-on Creating Basic Charts and
Graphs: Bar charts, line charts, scatter
plots
Next Session ▪ Hands-on Using Data Visualization Tools:
Creating visualizations using software tools
Agenda ▪ Hands-on Interpreting Visualizations:
Extracting insights from visual
representations of data
▪ Hands-on Exploratory Data Analysis (EDA):
Analyzing dataset distributions,
correlations, and outliers
35
Thank you!
For further queries, email: [email protected]