0% found this document useful (0 votes)
2 views

Ppt1 Variable Strings Functions

The document outlines a Data Science Workshop conducted by Ashis Kumar Pati at SOA University, covering essential topics such as Python programming, data analysis using libraries like NumPy and Pandas, and data visualization techniques. It emphasizes the role of data scientists in extracting insights from data and includes a grading pattern and reference materials. The workshop aims to equip participants with foundational skills in data science and Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Ppt1 Variable Strings Functions

The document outlines a Data Science Workshop conducted by Ashis Kumar Pati at SOA University, covering essential topics such as Python programming, data analysis using libraries like NumPy and Pandas, and data visualization techniques. It emphasizes the role of data scientists in extracting insights from data and includes a grading pattern and reference materials. The workshop aims to equip participants with foundational skills in data science and Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

Data Science Workshop-1

ITER, SOA University

Ashis Kumar Pati

Centre for Data Science


SOA University
[email protected]

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 1 / 69


Course details

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 2 / 69


Course Details

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 3 / 69


Course Details

Grading Pattern

Reference Book: Python for Data Analysis

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 4 / 69


Contents

Introduction
Basics of Python
NumPy Library
Pandas Library
Data Visualisation

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 5 / 69


Introduction
What is data science?
Amazon
Data science is the study of data to extract meaningful insights. Data
scientists to ask and answer questions like what happened, why it
happened, what will happen, and what can be done with the results.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 6 / 69


Introduction
What is data science?
Amazon
Data science is the study of data to extract meaningful insights. Data
scientists to ask and answer questions like what happened, why it
happened, what will happen, and what can be done with the results.

IBM
Data science combines math, statistics, and specialized programming
to uncover hidden insights that can be used to guide decision making and
strategic planning.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 6 / 69


Introduction
What is data science?
Amazon
Data science is the study of data to extract meaningful insights. Data
scientists to ask and answer questions like what happened, why it
happened, what will happen, and what can be done with the results.

IBM
Data science combines math, statistics, and specialized programming
to uncover hidden insights that can be used to guide decision making and
strategic planning.

Wikipedia
Data science is an interdisciplinary academic field that uses statistics,
scientific computing, scientific methods, processes, algorithms and systems
to extract or extrapolate knowledge and insights from noisy, structured,
and unstructured data.
Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 6 / 69
Introduction

Data science is all about getting relevant information about the data.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 7 / 69


Introduction

Data science is all about getting relevant information about the data.
Traditional Programming language vs Data science

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 7 / 69


Introduction

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 8 / 69


Introduction

Data Scientist or Data Analyst


Data Science - is a combination of various tools, machine learning
principles, and algorithm with the aim to find the patterns from the raw
data. Make predictions by identifying the trends (SQL,Machine
Learning,Visualisation,Storytelling).
Data Analyst - Here data sets are Cleaned(dirty data), Getting basic
information, examined to draw conclusions about the information they
contain. Information is extricated and classified to identify and analyze
conduct information, and different techniques are there according to
organizational requirements. We also called it data analysis.(Data
Wrangling, Data Mining,SQL,Visualization)

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 9 / 69


Introduction

Data vs Big data

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 10 / 69


Introduction

How much Data?

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 11 / 69


Introduction

Why Data science?

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 12 / 69


Introduction

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 13 / 69


Introduction

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 14 / 69


Introduction

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 15 / 69


Introduction

What does a data scientist do?


Problem Understanding: Understanding the domain and the problem
at hand is vital for framing the right questions and selecting
appropriate methodologies.
Data Gathering: The initial phase involves collecting relevant data
from various sources, such as databases, APIs, or web scraping.
Data Cleaning: Often, data comes in messy, incomplete, or
inconsistent formats. Data scientists spend a significant portion of
their time cleaning and preprocessing the data to ensure its quality.
Feature Engineering: Crafting meaningful features from raw data is a
critical step that significantly impacts the performance of machine
learning models.
Maintenance: Data science is an iterative process, and models need
continuous monitoring, updating, and improvement.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 16 / 69


Introduction

Python skills required


Different Data types(numeric, Boolean) and operators(Arithmetic,
logical, relational)
Functions
Control structures(if-else, for, while)
Built-in data types.(Strings, lists, dictionaries, tuples and sets).

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 17 / 69


Introduction

NumPy
NumPy(Numerical Python): Used for Numerical Computing.
Contains multidimensional array object called ndarray
Used for Linear algebra operations, Fourier transform etc
More efficient for storing and manipulating data

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 18 / 69


Introduction

pandas
stands for panel data
dataframe: with rows and columns
series: one dimensional data
Computing ideas of NumPy+Data manipulation techniques found in
data bases
Helps in reshape, slice and dice, reshape, aggregation

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 19 / 69


Introduction
Visualization
For producing plots and other two-dimensional data visualization
Jupyter Notebook
For interactive Python for writing, testing, debugging in one platform

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 20 / 69


Python
What is Python?
Started by Guido Van Rossum, 1991, Labs of National Institute of
research in Math and Computer science
The name was inspired by ’Monty Pythons flying circus’

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 21 / 69


Python
What is Python?
Started by Guido Van Rossum, 1991, Labs of National Institute of
research in Math and Computer science
The name was inspired by ’Monty Pythons flying circus’

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 21 / 69


Python

Why Python?

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 22 / 69


Python

Why Python?
Simple Easy to read, write and understand and Easy to code.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 22 / 69


Python

Why Python?
Simple Easy to read, write and understand and Easy to code.
General Purpose: Different range of applications

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 22 / 69


Python

Why Python?
Simple Easy to read, write and understand and Easy to code.
General Purpose: Different range of applications
Dynamically Typed: Variable types are determined at run time.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 22 / 69


Python

Why Python?
Simple Easy to read, write and understand and Easy to code.
General Purpose: Different range of applications
Dynamically Typed: Variable types are determined at run time.
Extensive Libraries: Around 1,37,000 libraries and supports external
libraries like NumPy and pandas.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 22 / 69


Python

Why Python?
Simple Easy to read, write and understand and Easy to code.
General Purpose: Different range of applications
Dynamically Typed: Variable types are determined at run time.
Extensive Libraries: Around 1,37,000 libraries and supports external
libraries like NumPy and pandas.
Object Oriented Language: Every thing including variables, functions
are objects

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 22 / 69


Python

Why Python?
Simple Easy to read, write and understand and Easy to code.
General Purpose: Different range of applications
Dynamically Typed: Variable types are determined at run time.
Extensive Libraries: Around 1,37,000 libraries and supports external
libraries like NumPy and pandas.
Object Oriented Language: Every thing including variables, functions
are objects
Interpretd vs Compiled: Runs line by line

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 22 / 69


Python

Why Python?
Simple Easy to read, write and understand and Easy to code.
General Purpose: Different range of applications
Dynamically Typed: Variable types are determined at run time.
Extensive Libraries: Around 1,37,000 libraries and supports external
libraries like NumPy and pandas.
Object Oriented Language: Every thing including variables, functions
are objects
Interpretd vs Compiled: Runs line by line
Interactive and script mode Write a line execute it before writing next
line.(Immediate feedback for each statement)

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 22 / 69


Python

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 23 / 69


Python

Interpreted vs Compiled Let You want to make a chicken biriyani, and


the recipe is written in odia, and you don’t understand odia. So there are
two ways to proceed. The first is if someone had already translated it into
English for you. You could read the English version of the recipe and make
Biriyani. Think of this translated recipe as the compiled version.
The second way is if you have a friend who knows odia. When you’re ready
to make biriyani, your odia friend sits next to you and translates the recipe
into English as you go, line by line, and you proceed after each step. In this
case, your friend is the interpreter for the interpreted version of the recipe.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 24 / 69


Python

Comments
# for single line comments
Triple quotes ”’multiline”’ for multi line comments.

Indentation
The spaces at the beginning of a code line. The way of telling a Python
interpreter that the group of statements belongs to a particular block of
code.

variable assignment
Name given to a memory location(the objects, so that can be used later.
As Python is dynamically typed, so we don’t need to specify the type of
the variable.
>>>x=10

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 25 / 69


Python

Python data types


Numeric Types: int, float, complex
Sequence Data type: (The ordered collection of similar or different
data types) String, list, tuple.
Boolean True and False
Set type Set
Mapping Type Dictionary

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 26 / 69


Python

Type casting: It is the method to convert the Python variable of one


datatype into another data type.
type() to check the type of an object
Python Implicit Type Conversion( Python converts the datatype into
another datatype automatically. Users don’t have to involve in this
process)
Python Explicit Type Conversion
int() function take float or string as an argument and returns int type
object.
float() function take int or string as an argument and return float type
object.
str() function takes float or int as an argument and returns string type
object.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 27 / 69


Python

Example of Python type casting

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 28 / 69


Python

Python operators
A symbol that performs a specific operation between two
operands(python variables).
Arithmetic Operators
+ Addition >>>x + y
- Subtraction >>>x - y
* Multiplication>>>x * y
/ Division >>>x / y
// Floor division >>>x // y
% Modulus (remainder)>>>x % y
** >>>Exponentiation x ** y
Comparison operators return a true or false Boolean value

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 29 / 69


Python

Python operators
Arithmetic operator
Comparison operator
== If the value of two operands is equal, then the condition becomes
true.
!= If the value of two operands is not equal, then the condition
becomes true.
<= The condition is met if the first operand is smaller than or equal to
the second operand.
>= The condition is met if the first operand is greater than or equal to
the second operand.
<If the first operand is less than the second operand, then the
condition becomes true.
>If the first operand is greater than the second operand, then the
condition becomes true.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 30 / 69


Python

Python operators
Arithmetic operator
Comparison operator
Logical Operators
and: True if both the operands are true x and y
or: True if either of the operands is true x or y
not True if operand is false
Assignment Operator
= Assign value of right side of expression to left side operand
+= Add and Assign: Add right side operand with left side operand and
then assign to left operand
-= Subtract AND: Subtract right operand from left operand and then
assign to left operand

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 31 / 69


Python

Python Operators
Arithmetic Operators
Comparison Operators
Logical operators
Assignment operators
Membership operators
in: If the first operand can be found in the second operand(list, tuple,
or dictionary), it is evaluated to be true.
not in: If the first operand is not present in the second operand, the
evaluation is true.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 32 / 69


Python

Python Operators
Arithmetic, Comparison, logical, Assignment, Membership Operator
Bitwise operators
& Bitwise AND: 1 if both are 1 otherwise 0
| Bitwise OR: 0 if both are 0 otherwise 1
∼ Bitwise NOT: inverts individual bits
∧ Bitwise XOR: 1 if any bit is 1 but not both, otherwise 0
>> Bitwise right shift: The left operand’s value is moved toward right
by the number of bits specified by the right operand.
<< Bitwise left shift: The left operand’s value is moved toward left by
the number of bits specified by the right operand

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 33 / 69


Python

Precedence of Operators

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 34 / 69


Python

Escape sequence
Escape sequence is a sequence of characters that, when used inside a
character or string,
does not represent itself but is converted into another character or
series of characters
that may be difficult or impossible to express directly
\’ Single quote
\\’ Double quote
Frequently used escape characters \\ Backslash
\n New line
\t Tab character
To ignore all the escape sequences in the string, we have to make a
string as a raw string using ’r’ or ’R’.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 35 / 69


Python

Python Strings
Using single quotes or double quotes or even triple quotes.
indexing:The indexing of the Python strings starts from 0

String is an immutable data type, meaning that once you have


created a string, you cannot change it

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 36 / 69


Python

Strings slicing
Syntax for slicing is [start : stop : step]
start is the starting index from where to start slicing.
stop is the ending index or where to sop. End index is stop-1.
step is the number of steps to jump.
Default value for start is 0 or -length(String), stop is length(String),
step is 1.
Slicing can be done on strings, arrays, lists, and tuples.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 37 / 69


Python

Strings slicing example

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 38 / 69


Python

Join(concatenate) Two or More Strings using the ’+’ operator.


>>>str1=’HELLO’
>>>str2=’PYTHON’
>>>str1+str2(Concatenate operation)
>>>’HELLO PYTHON’(This is the output)
Membership Test Check if a substring exists within a string or not,
using the keyword in.
>>>’He’ in ’Hello”
>>>True (Output)
String Repetition: Using * operator
>>>”Hi”*5
>>>’HiHiHiHiHi’

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 39 / 69


Python

In built functions for Python


len() method to find the length of a string.
upper() converts the string to uppercase[Similarly lower(),
capitalize(), title(), swapcase()]
partition() returns a tuple
replace() replaces substring inside
find() returns the index of first occurrence of substring[Similarly
rfind()]
strip() removes trailing characters[rstrip(),lstrip()]
split() splits string in to a list based on delimiter.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 40 / 69


Python

In built functions for python


startswith() checks if string starts with the specified string
isnumeric() checks numeric characters[islower(),isupper(),istitle()]
index() returns index of substring
count() Number of occurrences of a character

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 41 / 69


Python Strings

Examples

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 42 / 69


Python Strings

Examples

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 43 / 69


Python Strings
Format string
String formatting in Python allows you to create dynamic strings by
combining variables and values
There are five different ways to perform string formatting in Python
Formatting with % Operator.
Formatting with format() string method.
Other methods: Formatting with string literals(f-strings), string
Template Class, center() string method
It tells Python how to format a value using a format specifier. The
specifier is a sequence of characters that describe a variety of
formatting details
%d or %i – integer %f – float %s – string
The minimum number of characters to use is placed before the d, i, f
or s
4.7f indicates that a value should be formatted as a floating-point
number using a minimum of 4 characters, including the decimal point
and the seven digits to its right.
Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 44 / 69
Python Strings

Format string

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 45 / 69


Python

Python Functions
function is a piece of code written to carry out a specific task
Sometimes we bundle a set of instructions that we want to use
repeatedly or sometimes because of their complexity, we make a
sub-program and call when needed.Readability, and Reusability
Three types of functions in Python:
Built-in functions, written by python developers
User-Defined Functions (UDFs), which are functions that users create
to help them out
Anonymous functions, which are also called lambda functions.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 46 / 69


Builtin Functions
built-in functions
print() Prints to the standard output device
input() Allowing user input
abs() Returns the absolute value of a number
eval() Evaluates and executes an expression
type() gives type of an object.
float(), int(), str() Returns a floating point number,integer, string
respectively[similarly set(), list(), tuple()]
id() Returns the id of an object
range() Returns a sequence of numbers, starting from 0 and
increments by 1 by default.
min() and max() Return minimum, maximum among all elements
open() open file and read corresponding file object.
ord() return an integer representing the Unicode code point of that
character, inverse of the char()
map(), filter(), reduce() takes a function and iterable.
Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 47 / 69
All Builtin-functions

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 48 / 69


User defined functions

The four steps of defining functions


Use the keyword def to declare the function and follow this up with
the function name.
Add parameters(at most 255) to the function: they should be within
the parentheses of the function. End your line with a colon.
Add statements that the functions should execute. The bodies of
functions are indented.
End your function with a return statement if the function should
output something. Without the return statement, your function will
return an object None.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 49 / 69


User defined functions

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 50 / 69


User Defined functions

return statement
If you want to continue to work with the result of your function and
try out some operations on it, you will need to use the return
statement
Using return statement we can return multiple values, we have to use
tuples.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 51 / 69


User defined functions

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 52 / 69


User defined functions
Function arguments are of four types
Default arguments are values that are provided while defining
functions. So they become optional during the function calls. If we
provide a value to the default arguments during function calls, it
overrides the default value.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 53 / 69


User defined functions
Function arguments are of four types
Default arguments are values that are provided while defining
functions. So they become optional during the function calls. If we
provide a value to the default arguments during function calls, it
overrides the default value.
Required arguments that have to be in there. These arguments need
to be passed during the function call and in precisely the right order

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 53 / 69


User defined functions
Function arguments are of four types
Default arguments are values that are provided while defining
functions. So they become optional during the function calls. If we
provide a value to the default arguments during function calls, it
overrides the default value.
Required arguments that have to be in there. These arguments need
to be passed during the function call and in precisely the right order
Keyword arguments: During a function call, we use these to identify
the arguments by their parameter name. Values passed through
arguments don’t need to be in the order of parameters in the function
definition.
Variable number of arguments In cases where you don’t know the
exact number of arguments that you want to pass to a function, you
can use *args(Arbitrary positional arguments), **kwargs(arbitrary
keyword arguments).
Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 53 / 69
Things to remember about functions

arguments
In function definition Default arguments should come after
non-default(positional or required) arguments.
While calling the function keyword arguments should come after positional
arguments.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 54 / 69


Things to remember about functions

Summary:
default arguments should follow non-default arguments

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 55 / 69


Things to remember about functions

Summary:
default arguments should follow non-default arguments
keyword arguments should follow positional arguments

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 55 / 69


Things to remember about functions

Summary:
default arguments should follow non-default arguments
keyword arguments should follow positional arguments
All the keyword arguments passed must match one of the arguments
accepted by the function and their order is not important.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 55 / 69


Things to remember about functions

Summary:
default arguments should follow non-default arguments
keyword arguments should follow positional arguments
All the keyword arguments passed must match one of the arguments
accepted by the function and their order is not important.
No arguments should receive a value more than once.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 55 / 69


Things to remember about functions

Summary:
default arguments should follow non-default arguments
keyword arguments should follow positional arguments
All the keyword arguments passed must match one of the arguments
accepted by the function and their order is not important.
No arguments should receive a value more than once.
Default arguments are optional arguments.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 55 / 69


Things to remember about functions

Summary:
default arguments should follow non-default arguments
keyword arguments should follow positional arguments
All the keyword arguments passed must match one of the arguments
accepted by the function and their order is not important.
No arguments should receive a value more than once.
Default arguments are optional arguments.
Orders of Positional arguments is important.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 55 / 69


*args

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 56 / 69


**kwargs

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 57 / 69


Lambda(Anonymous) functions

Lambda(Anonymous) functions
Anonymous functions are also called lambda functions in Python
because instead of declaring them with the standard def keyword, you
use the lambda keyword.
A lambda function can take any number of arguments, but can only
have one expression.

syntax
lambda arguments: expression

Example
square = lambda x: x * x
sum = lambda x,y: x+y
Max = lambda x, y : x if(x>y) else y

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 58 / 69


User defined functions

name ==’ main ’


Functions from an old Python program can be imported into a new
one using the import keyword, followed by the name of the Python
file that contains the functions of interest (without the .py extension).
This allows the new program to call all of the functions in the old file,
but it also causes the program in the old file
While this may be desirable in some situations, we often want access
to the old program’s functions without actually running the program.
This is nor mally accomplished by creating a function named main

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 59 / 69


Date time module

A date in Python is not a data type of its own, but we can import a
module named datetime to work with dates as date objects.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 60 / 69


Date time module

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 61 / 69


random module

used to generate random numbers in Python


randint() Returns a random integer within the range
random() Generate random floating numbers
randrange((start, stop, step) Returns a random number within the
range(step has to be an integer)
shuffle(seq) Shuffle the sequence
choice(seq) Returns a random item from a sequence
sample(sequence, k) Return a k length list of unique elements chosen
from the sequence.
seed(): If the seeding value is same, the sequence will be the same
normalvariate() Return a random floating point number with normal
distribution

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 62 / 69


Random Module

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 63 / 69


random module

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 64 / 69


file handling

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 65 / 69


file handling

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 66 / 69


try..except..

Error in Python can be of two types i.e. Syntax errors and Exceptions.
Some of the common Exception Errors are :
IOError: if the file can’t be opened
KeyboardInterrupt: when an unrequired key is pressed by the user
ValueError: when the built-in function receives a wrong argument
EOFError: if End-Of-File is hit without reading any data
ImportError: if it is unable to find the module
NameError: If a variable is used, which is not defined before
Try and Except statement is used to handle these errors within our
code in Python
The try block lets you test a block of code for errors.
The except block lets you handle the error.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 67 / 69


try-except example

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 68 / 69


try-except
The else block lets you execute code when there is no error.
The finally block lets you execute code, regardless of the result of the
try- and except blocks.

Ashis Kumar Pati (SOA) Data Science Workshop-1 2023 69 / 69

You might also like