Data Analytics Using Python
Data Analytics Using Python
Data Analytics Using Python
History of Python
Python was developed by Guido van Rossum in the late eighties
and early nineties at the National Research Institute for Mathematics and Computer
scripting languages.
What is Python
With its interpreted nature, Python's syntax and dynamic typing make it an ideal
language for scripting and rapid application development.
Python has many third-party libraries that can be used to make its functionality
easier. These libraries cover many domains, for example, web development,
scientific computing, data analysis, and more.
In the above example, the statements that are the same level to the right belong to
the function. Generally, we can use four whitespaces to define indentation.
In Python, comments can be added using the '#' symbol. Any text written after the
'#' symbol is considered a comment and is ignored by the interpreter. This trick is
useful for adding notes to the code or temporarily disabling a code block. It also
helps in understanding the code better by some other developers.
'If', 'otherwise', 'for', 'while', 'try', 'except', and 'finally' are a few reserved keywords
in Python that cannot be used as variable names. These terms are used in the
language for particular reasons and have fixed meanings. If you use these
keywords, your code may include errors, or the interpreter may reject them as
potential new Variables.
Python Features
Easy-to-learn − Python has few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language quickly.
Easy-to-read − Python code is more clearly defined and visible to the eyes.
A broad standard library − Python's bulk of the library is very portable and
cross-platform compatible on UNIX, Windows, and Macintosh.
Interactive Mode − Python has support for an interactive mode which allows
interactive testing and debugging of snippets of code.
Portable − Python can run on a wide variety of hardware platforms and has the
same interface on all platforms.
Extendable − You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more
efficient.
GUI Programming − Python supports GUI applications that can be created and
ported to many system calls, libraries and windows systems, such as Windows
MFC, Macintosh, and the X Window system of Unix.
Scalable − Python provides a better structure and support for large programs than
shell scripting. It supports functional and structured programming methods as
well as OOP.
It can be used as a scripting language or can be compiled to byte-code for
building large applications
. It provides very high-level dynamic data types and supports dynamic type
checking.
Python has wide range of libraries and frameworks widely used in various fields
such as machine learning, artificial intelligence, web applications, etc. We define
some popular frameworks and libraries of Python as follows.
Python Identifiers
Python identifiers –
Class names start with an uppercase letter. All other identifiers start with a
lowercase letter.
Starting an identifier with a single leading underscore indicates that the identifier
is private.
Starting an identifier with two leading underscores indicates a strongly private
identifier.
If the identifier also ends with two trailing underscores, the identifier is a
language-defined special name. Reserved Words The following list shows the
Python keywords. These are reserved words and you cannot use them as constant
or variable or any other identifier names
Comments in Python
A hash sign (#) that is not inside a string literal begins a comment. All characters
after the
# and up to the end of the physical line are part of the comment and the Python
interpreter ignores them. #!/usr/bin/python
Python - Variable
Variables are nothing but reserved memory locations to store values. This means
that when you create a variable you reserve some space in memory. Based on the
data type of a variable, the interpreter allocates memory and decides what can be
stored in the reserved memory. Therefore, by assigning different data types to
variables, you can store integers, decimals or characters in these variables.
print (counter)
print (miles)
print (name)
KEYWORDS
The following list shows the Python keywords. These are reserved words and you
cannot use them as constant or variable or any other identifier names. All the
Python
INPUT Function:
To get input from the user you can use the input function. When the input function
is
called the program stops running the program, prompts the user to enter something
at the
keyboard by printing a string called the prompt to the screen, and then waits for the
user to
press the Enter key. The user types a string of characters and presses enter. Then
the input
function returns that string and Python continues running the program by executing
the next
statement after the input statement.
Python provides the function input(). input has an optional parameter, which is the
prompt string.
Indentation
Code blocks are identified by indentation rather than using symbols like curly
braces.
Without extra symbols, programs are easier to read. Also, indentation clearly
identifies which
block of code a statement belongs to. Of course, code blocks can consist of single
statements,
Here's a list of different types of Python operators that we will learn in this tutorial.
1. Arithmetic operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. Special Operators
1. Python Arithmetic Operators
Mathematical operations including addition, subtraction, multiplication, and
division are commonly carried out using Python arithmetic operators.
They are compatible with integers, variables, and expressions.
In addition to the standard arithmetic operators, there are operators for
modulus, exponentiation, and floor division.
Operator Name Example
+ Addition 10 + 20 = 30
- Subtraction 20 – 10 = 10
* Multiplication 10 * 20 = 200
/ Division 20 / 10 = 2
% Modulus 22 % 10 = 2
** Exponent 4**2 = 16
// Floor Division 9//2 = 4
a =int(input(“enter no “)
b = int(input(“enter no”)
print('Addition of two numbers:',a+b)
print('Subtraction of two numbers:',a-b)
print('Multiplication of two numbers:',a*b)
print('Division of two numbers:',a/b)
print('Reminder of two numbers:',a%b)
print('Exponent of two numbers:',a**b)
print('Floor division of two numbers:',a//b)
Comparison operator
Comparison operators mainly use for comparison purposes. Comparison operators
compare the values of the two operands and return a true or false Boolean value in
accordance. The example of comparison operators are ==, !=, <=, >=, >, <. In the
below table, we explain the works of the operators.
a =int(input(“enter no “)
b = int(input(“enter no”)
print('Two numbers are equal or not:',a==b)
print('Two numbers are not equal or not:',a!=b)
print('a is less than or equal to b:',a<=b)
print('a is greater than or equal to b:',a>=b)
print('a is greater b:',a>b)
print('a is less than b:',a<b)
Assignment Operators
Using the assignment operators, the right expression's value is assigned to the left
operand. There are some examples of assignment operators like =, +=, -=, *=, %=,
**=, //=. In the below table, we explain the works of the operators.
a =int(input(“enter no “)
b = int(input(“enter no”)
print('a=b:', a==b)
print('a+=b:', a+b)
print('a-=b:', a-b)
print('a*=b:', a*b)
print('a%=b:', a%b)
print('a**=b:', a**b)
print('a//=b:', a//b)
Bitwise Operators
The two operands' values are processed bit by bit by the bitwise operators. The
examples of Bitwise operators are bitwise OR (|), bitwise AND (&), bitwise XOR
(^), negation (~), Left shift (<<), and Right shift (>>). Consider the case below.
a =int(input(“enter no “)
b = int(input(“enter no”)
print('a&b:', a&b)
print('a|b:', a|b)
print('a^b:', a^b)
print('~a:', ~a)
print('a<<b:', a<<b)
print('a>>b:', a>>b)
Logical Operators
The assessment of expressions to make decisions typically uses logical operators.
The examples of logical operators are and, or, and not. In the case of logical AND,
if the first one is 0, it does not depend upon the second one. In the case of logical
OR, if the first one is 1, it does not depend on the second one. Python supports the
following logical operators. In the below table, we explain the works of the logical
operators.
Membership Operators
The membership of a value inside a Python data structure can be verified using
Python membership operators. The result is true if the value is in the data structure;
otherwise, it returns false.
x = ["Rose", "Lotus"]
print(' Is value Present?', "Rose" in x)
print(' Is value not Present?', "Riya" not in x)
If Statement
num = 5
if (num < 10):
print(“Num is smaller than 10”)
If-else Statement
As discussed above, the if statement executes the code block when the condition is
true. Similarly, the else statement works in conjuncture with the if statement to
execute a code block when the defined if condition is false.
num = 5
if(num > 10):
print(“number is greater than 10”)
else:
print(“number is less than 10”)
Elif statements
In Python, we have one more conditional statement called “elif” statements. “elif”
statement is used to check multiple conditions only if the given condition is false.
It’s similar to an “if-else” statement and the only difference is that in “else” we will
not check the condition but in “elif” we will check the condition.
“elif” statements are similar to “if-else” statements but “elif” statements evaluate
multiple conditions.
num = 10
if (num == 0):
print(“Number is Zero”)
else:
print(“Number is smaller than 5”)
LOOPING STATEMENTS
The following loops are available in Python to fulfil the looping needs. Python
offers 3 choices for running the loops. The basic functionality of all the techniques
is the same, although the syntax and the amount of time required for checking the
condition differ.
The following sorts of loops are available in the Python programming language.
EX : for I in range(0,20)
Print(i)
While Loop
While loops are used in Python to iterate until a specified condition is met.
However, the statement in the program that follows the while loop is executed
once the condition changes to false.
counter = 0
while counter < 10: # giving the condition
counter = counter + 3
print("Python Loops")
Break Statement
It stops the execution of the loop when the break statement is reached.
for string in "Python Loops":
if string == 'L':
break
print('Current Letter: ', string)
Pass Statement
Pass statements are used to create empty loops. Pass statement is also employed for
classes, functions, and empty control statements.
Continue Statement
It returns the control to the beginning of the loop.
match lang:
case "JavaScript":
print("You can become a web developer.")
case "Python":
print("You can become a Data Scientist")
case "PHP":
print("You can become a backend developer")
case "Solidity":
print("You can become a Blockchain developer")
case "Java":
print("You can become a mobile app developer")
case _:
print("The language doesn't matter, what matters is solving problems.")
Functions
In Python, standard library functions are the built-in functions that can be used
directly in our program. For example,
Pause We can stop a program from repeatedly using the same code block by
including functions.
o Once defined, Python functions can be called multiple times and from any
location in a program.
o Our Python program can be broken up into numerous, easy-to-follow
functions if it is significant.
o The ability to return as many outputs as we want using a variety of
arguments is one of Python's most significant achievements.
o Python programs have always incurred overhead when calling functions.
Built-in functions, such as help() to ask for help, min() to get the minimum
value, print() to print an object to the terminal,… You can find an overview with
more of these functions
Ex : len(), abs(),pi() sum(),type()
User-Defined Functions (UDFs), which are functions that users create to help
them out; And
Ex : def (x,y)
Return(x-y)
Print(sub(5,2))
Anonymous functions, which are also called lambda functions because they are
not declared with the standard def keyword.
lambda_ = lambda argument1, argument2: argument1 + argument2;