Ppt1 Variable Strings Functions
Ppt1 Variable Strings Functions
Grading Pattern
Introduction
Basics of Python
NumPy Library
Pandas Library
Data Visualisation
IBM
Data science combines math, statistics, and specialized programming
to uncover hidden insights that can be used to guide decision making and
strategic planning.
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.
Data science is all about getting relevant information about the data.
Traditional Programming language vs Data science
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
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
Why Python?
Why Python?
Simple Easy to read, write and understand and Easy to code.
Why Python?
Simple Easy to read, write and understand and Easy to code.
General Purpose: Different range of applications
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.
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.
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
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
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)
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
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
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.
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
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.
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
Precedence of Operators
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’.
Python Strings
Using single quotes or double quotes or even triple quotes.
indexing:The indexing of the Python strings starts from 0
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.
Examples
Examples
Format string
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.
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.
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.
Summary:
default arguments should follow non-default arguments
Summary:
default arguments should follow non-default arguments
keyword arguments should follow positional arguments
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.
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.
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.
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.
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
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.
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.