Fundamentals of Data Analytics: DR Parveen Siwach

Download as pdf or txt
Download as pdf or txt
You are on page 1of 84

Fundamentals of Data

Analytics
Dr Parveen Siwach
Unit-1 Basics of Python
• Python: Overview, Installation, and Importance in Data Analysis, Download and
install Python, Execute Python program from command prompt and using
IDLE., Data types and variables; Operators and operator precedence, Data
type conversions, Data input, Comments
Unit-2 – Python flow control, Sequences and functions
• Python flow control: If statement, If.. elif.. else statement, While loop, For loop,
Break & continue, Else clause; Python Sequences: String, List, Tuple,
Dictionary, Set; Python functions: Define function, Pass Arguments,
Mathematical functions, Recursive functions
Unit-3- Python classes, pandas and data processing

Python classes and objects: Class definition, Creating objects,
Constructors & Destructors; Pandas: Series, Dataframe, Importing csv,
Exporting csv, Groupby, Describe, Info, Iloc, loc, Filtering, Slicing; Data
Processing: Data Cleaning, Data Integration, Data Reduction
Unit-4- Data Visualizations

Plotting Financial Data: Line Plots, Scatter Plots, and Bar Charts;
Customizations: Axes, Labels, Titles, Legends, and Annotations; Multiple Plots
and Subplots; Distribution Plots: Histograms, Kernel Density Plots, and Joint
Plots; Categorical Plots: Box Plots, Violin Plots, Swarm Plots; Pair Plots and
Heatmaps; Candlestick Charts using Plotly
What is Python?
• Python is a general-purpose, high-level, interpreted, object-oriented
programming language used for various applications.
• Python was developed by Guido Van Rossum in 1989 while working at
National Research Institute in the Netherlands. But officially, Python
was made available to the public in 1991.
• Python is simple and easy to learn. Syntax emphasizes readability and
therefore reduces the cost of program maintenance. In addition, it
supports modules and packages, which encourages program
modularity and code reuse.
Install Python
• It may be possible that some PCs and Macs will have Python already installed. You
can check which version of Python is installed before proceeding to the
installation.
• Open the command line or terminal and type the below command.
python -–version
• If you find Python is not installed, install it using the following instructions.
• Installing or updating Python on your computer is the first step to programming
in Python. There are multiple installation methods, such as installing Python using
an installer or a source code (.zip file)
• Download the latest version of Python from python.org. Once you’ve
downloaded the installer as per the operating system, Next run an installer by
double-clicking on the downloaded file and following the steps
Create and Run Your First Python Program
We can run Python by using the following three ways
• Run Python using IDLE
• Run Python interactively using the command line in immediate mode
• Executing Python File
We will see each one by one but before that, let’s see how to write your first
Python program.
Let’s write a simple statement in Python to print the ‘hello world’ on a
screen.
• Use the print() function and write a message in its opening and closing
brackets shown below.
• A message is a string that is a sequence of characters. In Python, strings are
enclosed inside single quotes, double quotes, or triple quotes.
Quick Summary of Symbols in python
• print("Hello, World!") # Double quotes
• print('Hello, World!') # Single quotes

• print("It's a beautiful day!") # Double quotes used to avoid escaping single quote
• print('He said, "Hello!"') # Single quotes used to avoid escaping double quotes

• print('It\'s a beautiful day!') # Using backslash to escape single quote within a single-
quoted string
• print("He said, \"Hello!\"") # Using backslash to escape double quotes within a double-
quoted string
End-of-Line to Terminate a Statement
• In Python end of the line terminate the statement. So you don’t need to
write any symbol to mark the end of the line to indicate the statement
termination. For example, in other programming languages like Java and C,
the statement must end with a semicolon (;).
• Python statement ends with the token NEWLINE character (\n). But we can
extend the statement over multiple lines using line continuation character
(\). This is known as an explicit continuation.
• Semicolumn to Separate Multiple Statements
• In Python, we can add multiple statements on a single line separated
by semicolons, as follows:
Indentation
• Indentation in Python is crucial because it defines the structure and
flow of code, particularly for defining blocks such as loops, functions,
and conditionals.
• Python uses indentation (usually 4 spaces or a tab) to indicate a block
of code rather than curly braces or other delimiters like in some other
languages.
Indentation
• Python indentation tells a Python interpreter that the group of statements
belongs to a particular block of code. The indentation makes the code look
neat, clean, and more readable.
• A block is a combination of all multiple statements. Inside a code block, we
group multiple statements for a specific purpose.
• In other programming languages like C or Java, use curly braces { } to define
a block of code. Python uses indentation to denote the code block.
• Whitespace is used for indentation in Python to define the indentation
level. Ideally, we should use 4 spaces per indentation level. In Python,
indented code blocks are always preceded by a colon (:) on the previous
line
• Take the example of the if-else statement in Python:
Indentation
Indentation
• If one code block is nested in another, the child code block should
separate by 4 spaces from the parent code block.
• If a block has to be more deeply nested, it is simply indented further
to the right. You can understand it better by looking at the following
lines of code.

• The overall purpose of the code is to determine if a number is both


greater than 100 and even, and if so, it prints a specific message.
Syntax in Python
• Syntax is the structure of language or a set of rules that defines how a
Python program will be written and interpreted.
• Understanding Python's syntax is essential for writing code that runs
correctly. Here are some key aspects:
• Statements: In Python, a statement is a line of code that performs an
action. Examples include assignments, function calls, and control flow
statements:
Syntax in Python
• Comments: Comments are used to explain and document code. In
Python, comments start with a # symbol and continue to the end of the
line:

• Strings: Strings can be defined using single quotes (' ') or double
quotes (" "). Triple quotes (''' ''' or """ """) are used for multi-line
strings:
Syntax in Python
• Variables and Data Types: Variables are used to store data, and
Python supports several data types, including integers, floats, strings,
and lists:

• Control Flow: Python uses keywords like if, else, elif, for, and while
to control the flow of execution:
Syntax in Python
• Functions: Functions are defined using the def keyword and are called
by their name followed by parentheses:
Indentation in Python
• Indentation in Python is not just for readability; it defines the structure
of the code and the grouping of statements. Proper indentation is
crucial for code to run correctly.
1. Indentation Rules: Python uses indentation to denote blocks of code.
For example, in conditional statements, loops, and function
definitions:
Indentation in Python
• Consistent Indentation: Python requires consistent use of either
spaces or tabs for indentation. Mixing spaces and tabs can lead to
errors. The Python style guide (PEP 8) recommends using 4 spaces per
indentation level:
Indentation in Python
• Nested Blocks: When blocks are nested, each new level of nesting
should be indented further:

• Error Handling: Incorrect indentation will result in IndentationError


or SyntaxError:
What is keyword in Python?
• Python keywords are reserved words that have a special
meaning associated with them and can’t be used for anything but
those specific purposes. Each keyword is designed to achieve specific
functionality.
• Python keywords are case-sensitive.
1.All keywords contain only letters (no special symbols)
2.Except for three keywords (True, False, None), all keywords have
lower case letters
Get the List of Keywords
• As of Python 3.9.6, there are 36 keywords available. This number can vary
slightly over time.
• We can use the following two ways to get the list of keywords in Python
• keyword module: The keyword is the built-in module to get the list of
keywords. Also, this module allows a Python program to determine if a string
is a keyword.
• help() function: Apart from a keyword module, we can use
the help() function to get the list of keywords
• Example: keyword module
import keyword
print(keyword.kwlist)
Get the List of Keywords

Output:

All the keywords except, True, False, and None, must be written in a lowercase alphabet symbol.

Note:
You cannot use any of the above keywords as identifiers in your programs. If you try to do so, you will get an
error. An identifier is a name given to an entity, For example, variables name, functions name, or class name.
The help() function

Output:
Understand Any keyword
• The python help() function is used to display the documentation of
modules, functions, classes, keywords.
• Pass the keyword name to the help() function to get to know how to
use it. The help() function returns the description of a keyword along
with an example.
• Let’s understand how to use the if keyword.
Understand Any keyword

Output:
How to Identify Python Keywords
• Another way is if you are getting a syntax error for any identifier
declaration, then you may be using a keyword as an identifier in your
program.
Keyword Module
• Python keyword module allows a Python program to determine if a
string is a keyword.
• iskeyword(s): Returns True if s is a keyword

Output:
As you can see in the output, it returned True because ‘if’ is the keyword, and it returned False because the range
is not a keyword (it is a built-in function).
Types of Keywords
• All 36 keywords can be divided into the following seven categories.
• Value Keywords: True, False, None.
• True and False are used to represent truth values, know as boolean
values. It is used with a conditional statement to determine which
block of code to execute. When executed, the condition evaluates
to True or False.
Types of Keywords
• Operator Keywords: and, or, not, in, is
• The logical and keyword returns True if both expressions are True.
Otherwise, it will return. False.
• The logical or keyword returns a boolean True if one expression is
true, and it returns False if both values are false.
• The logical not keyword returns boolean True if the expression is false
• See Logical operators in Python
Types of Keywords
Types of Keywords

The is keyword returns return True if the memory address first value is equal to the second
value. Read Identity operators in Python.
Types of Keywords
• The in keyword returns True if it finds a given object in the sequence
(such as list, string). Read membership operators in Python
Types of Keywords
• Conditional Keywords: if, elif, else
• In Python, condition keywords act depending on whether a given
condition is true or false. You can execute different blocks of codes
depending on the outcome of a condition. See: Control flow in
Python
Types of Keywords
• Iterative and Transfer Keywords: for, while, break, continue, else
• Iterative keywords allow us to execute a block of code repeatedly. We
also call it a loop statements.
• while: The while loop repeatedly executes a code block while a
particular condition is true.
• for: Using for loop, we can iterate any sequence or iterable variable.
The sequence can be string, list, dictionary, set, or tuple.
• break, continue, and pass: In Python, transfer statements are used to
alter the program’s way of execution in a certain manner. Read break
and continue in Python.
Types of Keywords
• Iterative and Transfer Keywords: for, while, break, continue, else
Types of Keywords
• Structure Keywords: def, class, with, as, pass, lambda
• The def keyword is used to define user-defined funtion or methods of
a class. Example: def keyword
Types of Keywords
• Structure Keywords: def, class, with, as, pass, lambda
• The pass keyword is used to define as a placeholder when
a statement is required syntactically.
• Example: pass keyword
Types of Keywords
• Structure Keywords: def, class, with, as, pass, lambda
• class keyword is sued to define class in Python. Object-oriented
programming (OOP) is a programming paradigm based on the concept of
“objects“. An object-oriented paradigm is to design the program using classes and
objects. Example: class keyword
Types of Keywords
• Structure Keywords: def, class, with, as, pass, lambda
• with keyword is used when working with unmanaged resources (like file streams). It allows you to
ensure that a resource is “cleaned up” when the code that uses it finishes running, even if
exceptions are thrown.
• Example: Open a file in Python using the with statement
Types of Keywords
• Import Keywords: import, from, as
• In Python, the import statement is used to import the whole module.
• Example: Import Python datetime module

Also, we can import specific classes and functions from a module. Example:
Types of Keywords
• Returning Keywords: return, yield
• In Python, to return value from the function, a return statement is used.
• yield is a keyword that is used like return, except the function will return a generator. See yield
keyword Example:
Exception Description

AssertionError Raised when an assert statement fails.

Types of Keywords AttributeError Raised when attribute assignment or reference fails.

EOFError Raised when the input() function hits the end-of-file condition.

FloatingPointError Raised when a floating-point operation fails.

• Exception-Handling GeneratorExit Raise when a generator’s close() method is called.


Keywords: try, except, raise, finally, else,
assert ImportError Raised when the imported module is not found.

• An exception is an event that occurs IndexError Raised when the index of a sequence is out of range.

during the execution of programs that KeyError Raised when a key is not found in a dictionary.
disrupt the normal flow of execution
(e.g., KeyError Raised when a key is not KeyboardInterrupt Raised when the user hits the interrupt key (Ctrl+C or Delete)

found in a dictionary.) An exception is a MemoryError Raised when an operation runs out of memory.
Python object that represents an error.
NameError Raised when a variable is not found in the local or global scope.

OSError Raised when system operation causes system related error.

Raised when a weak reference proxy is used to access a garbage


ReferenceError
collected referent.
Types of Keywords
• Variable Handling Keywords: del, global, nonlocal
• The del keyword is used to delete the object.
• The global keyword is used to declare the global variable. A global variable is a variable that is defined outside of
the method (block of code). That is accessible anywhere in the code file.
• Nonlocal variables are used in nested functions whose local scope is not defined. This means that the variable
can be neither in the local nor the global scope.
Types of Keywords
• Asynchronous Programming Keywords: async, await
• The async keyword is used with def to define an asynchronous function, or coroutine.

• Also, you can make a function asynchronous by adding the async keyword before the function’s regular
definition.
• The await Keyword
• Python’s await keyword is used in asynchronous functions to specify a point in the function where control is
given back to the event loop for other functions to run. You can use it by placing the await keyword in front
of a call to any async function:
Python Variables
• A variable is a reserved memory area (memory address) to store value.
For example, we want to store an employee’s salary. In such a case, we can
create a variable and store salary using it. Using that variable name, you
can read or modify the salary amount.
• In other words, a variable is a value that varies according to the condition
or input pass to the program. Everything in Python is treated as an object
so every variable is nothing but an object in Python.
• A variable can be either mutable or immutable. If the variable’s value can
change, the object is called mutable, while if the value cannot change, the
object is called immutable. We will learn the difference between mutable
and immutable types in the later section of this article
Creating a variable
• Python programming language is dynamically typed, so there is no
need to declare a variable before using it or declare the data type of
variable like in other programming languages. The declaration
happens automatically when we assign a value to the variable.
• Creating a variable and assigning a value
• We can assign a value to the variable at that time variable is created.
We can use the assignment operator = to assign a value to a variable.
Creating a variable
• In the given example, “John”, 25, 25800.60 are values that are
assigned to name, age, and salary respectively.
Changing the value of a variable
• Many programming languages are statically typed languages where
the variable is initially declared with a specific type, and during its
lifetime, it must always have that type.
• But in Python, variables are dynamically typed and not subject to
the data type restriction. A variable may be assigned to a value of one
type, and then later, we can also re-assigned a value of a different
type. Let’s see the example.
Changing the value of a variable
Create Number, String, List variables
• We can create different types of variables as per our requirements.
Let’s see each one by one.
• Number
• A number is a data type to store numeric values. The object for the
number will be created when we assign a value to the variable. In
Python3, we can use the following three data types to store numeric
values.
1.Int
2.float
3.complex
Create Number, String, List variables
• We can create different types of variables as per our requirements.
Let’s see each one by one.
• Number
• A number is a data type to store numeric values. The object for the
number will be created when we assign a value to the variable. In
Python3, we can use the following three data types to store numeric
values.
1.Int
2.float
3.complex
Create Number, String, List variables
• Integer variable
• The int is a data type that returns integer type values (signed
integers); they are also called ints or integers. The integer value can
be positive or negative without a decimal point.
• Example

Note: We used the built-in Python method type() to check the variable type.
Create Number, String, List variables
• Float variable
• Floats are the values with the decimal point dividing the integer and
the fractional parts of the number. Use float data type to store
decimal values.

In the above example, the variable salary assigned to value 10800.55, which is a float value.
Create Number, String, List variables
• Complex type
• The complex is the numbers that come with the real and imaginary
part. A complex number is in the form of a+bj, where a and b contain
integers or floating-point values.
Create Number, String, List variables
• String variable
• In Python, a string is a set of characters represented in quotation
marks. Python allows us to define a string in either pair
of single or double quotation marks. For example, to store a person’s
name we can use a string type.
• To retrieve a piece of string from a given string, we can use to slice
operator [] or [:]. Slicing provides us the subset of a string with an
index starting from index 0 to index end-1.
• To concatenate the string, we can use the addition(+) operator.
Create Number, String, List variables
Create Number, String, List variables
• List type variable:
• If we want to represent a group of elements (or value) as a single entity, we should go for the list variable
type. For example, we can use them to store student names. In the list, the insertion order of elements is
preserved. That means, in which order elements are inserted in the list, the order will be intact.
• Read: Complete Guide on Python lists
The list can be accessed in two ways, either positive or negative index. The list has the following
characteristics:
1. In the list insertion order of elements is preserved.
2. Heterogeneous (all types of data types int, float, string) elements are allowed.
3. Duplicates elements are permitted.
4. The list is mutable(can change).
5. Growable in nature means based on our requirement, we can increase or decrease the list’s size.
6. List elements should be enclosed within square brackets [].
Create Number, String, List variables
Get the data type of variable
• No matter what is stored in a variable (object), a variable can be any type
like int, float, str, list, tuple, dict, etc. There is a built-in function called type() to
get the data type of any variable.
• The type() function has a simple and straightforward syntax.
• Syntax of type() :
Get the data type of variable
• example
Get the data type of variable
• No matter what is stored in a variable (object), a variable can be any type
like int, float, str, list, tuple, dict, etc. There is a built-in function called type() to
get the data type of any variable.
• The type() function has a simple and straightforward syntax.
• Syntax of type() :
Delete a variable
• Use the del keyword to delete the variable. Once we delete the
variable, it will not be longer accessible and eligible for the garbage
collector.

• Now, let’s delete var1 and try to access it again.


Variable’s case-sensitive
• Python is a case-sensitive language. If we define a variable with
names a = 100 and A =200 then, Python differentiates
between a and A. These variables are treated as two different
variables (or objects).
Constant
• Constant is a variable or value that does not change, which means it
remains the same and cannot be modified. But in the case of Python,
the constant concept is not applicable.
• By convention, we can use only uppercase characters to define the
constant variable if we don’t want to change it.
Rules and naming convention for variables
and constants
• A name in a Python program is called an identifier. An identifier can
be a variable name, class name, function name, and module name.
• In Python, there are some conventions and rules to define variables
and constants that should follow.
• Rule 1: The name of the variable and constant should have a
combination of letters, digits, and underscore symbols.
• Alphabet/letters i.e., lowercase (a to z) or uppercase (A to Z)
• Digits(0 to 9)
• Underscore symbol (_)
Rules and naming convention for variables
and constants
• Rule 2: The variable name and constant name should make sense.
• Note: we should always create a meaningful variable name so it will
be easy to understand. That is, it should be meaningful.
• Rule 3: Don’t’ use special symbols in a variable name
• For declaring variable and constant, we cannot use special symbols
like $, #, @, %, !~, etc. If we try to declare names with a special
symbol, Python generates an error
Rules and naming convention for variables
and constants
• Rule 4: Variable and constant should not start with digit letters.
• You will receive an error if you start a variable name with a digit. Let’s verify this using a simple
example.

• Here Python will generate a syntax error at 1studnet. instead of this, you can declare a variable
like studnet_1 = "Jessa”
• Rule 5: Identifiers are case sensitive.
Rules and naming convention for variables
and constants
• Rule 4: Variable and constant should not start with digit letters.
• You will receive an error if you start a variable name with a digit. Let’s verify this using a simple
example.

• Here Python will generate a syntax error at 1studnet. instead of this, you can declare a variable
like studnet_1 = "Jessa”
• Rule 5: Identifiers are case sensitive.
Rules and naming convention for variables
and constants
• Rule 6: Use an underscore symbol for separating the words in a variable name
• If we want to declare variable and constant names having two words, use an underscore symbol
for separating the words.
Multiple assignments
• In Python, there is no restriction to declare a variable before using it
in the program. Python allows us to create a variable as and when
required.
• We can do multiple assignments in two ways, either by
assigning a single value to multiple variables or assigning multiple
values to multiple variables.
Multiple assignments
• Assigning a single value to multiple variables
• we can assign a single value to multiple variables simultaneously
using the assignment operator =.
• Now, let’s create an example to assign the single value 10 to all three
variables a, b, and c.
Assigning multiple values to multiple
variables
• In the given example, two integer values 10 and 70 are assigned to
variables roll_no and marks, respectively, and string literal, “Jessa,” is
assigned to the variable name

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy