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

Py New U5

The document discusses different types of errors in Python including syntax errors, runtime errors, and semantic errors. It also covers exception handling in Python using try, except, and finally blocks and provides examples of catching built-in exceptions like ZeroDivisionError as well as user-defined exceptions.

Uploaded by

Charan Putla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Py New U5

The document discusses different types of errors in Python including syntax errors, runtime errors, and semantic errors. It also covers exception handling in Python using try, except, and finally blocks and provides examples of catching built-in exceptions like ZeroDivisionError as well as user-defined exceptions.

Uploaded by

Charan Putla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Unit-5

Error and Exception Handling


Error Handling
In Python, there are several types of errors that can occur during the execution of a program. These errors are
categorized into different types based on their causes. The most common types of errors in Python are:

1.Syntax Errors:

Syntax errors occur when the code violates the rules of the Python language. These errors are typically raised
during the parsing phase before the code is executed. Common causes of syntax errors include missing colons,
mismatched parentheses, incorrect indentation, etc. Examples of syntax errors:

# Example of a syntax error (missing closing parenthesis)

print("Hello, World!"

# Example of a syntax error (missing colon)

if x > 5

print("x is greater than 5")

2. Runtime Errors (Exceptions):

Runtime errors, also known as exceptions, occur during the execution of a program when something unexpected
happens. These errors are not caught during the parsing phase and are raised when a specific condition or
operation cannot be performed as expected. Common examples of runtime errors include division by zero,
accessing an index that is out of range, trying to open a file that does not exist, etc.

# Example of a runtime error (division by zero)

x = 10 / 0

# Example of a runtime error (index out of range)

my_list = [1, 2, 3]

print(my_list[5])

3. Semantic Errors (Logic Errors):

Semantic errors, also known as logic errors, do not raise any errors or exceptions, but they cause the program to
produce incorrect or unexpected results due to logical flaws in the code. These errors are challenging to identify
as they do not generate any error messages. They arise when the programmer makes a mistake in the algorithm
or the logical flow of the program.

# Example of a semantic error (incorrect logic in a loop)

sum = 0

for i in range(10):

sum = sum + i # Incorrect logic: should be sum = sum + i + 1

print(sum) # Output: 45 (instead of 55)


Exception Handling

● In Python, the exception is an abnormal situation during the execution.

● In general, when a Python program encounters a situation that it cannot cope with, it raises an
exception.

● When an exception has occurred, it must either handle the exception otherwise it terminates the
execution.

● The Python programming language provides three keywords to handle the exceptions.

✔ try

✔ except

✔ finally

try

● The try keyword is used to define a block of code which may cause an exception.

except

● The except keyword is used to define a block of code which handles the exception.

finally

● The finally keyword is used to define a block of code which is to execute, regardless of an exception
occurrence.

The Python allows us to use an optional else keyword that can be used with try block. The else is used to define
a block of code to be executed if no exception were raised.

The following is the general syntax of exception handling in the Python.

Syntax

try:

# the code that may raise an exception

except:

# the code that handles an exception

finally:

# the code that must be executed

Example

try:

a = a * 10

print(a)

except:

print('Exception: Please do define variable a before it is used!')


else:

print('Everything is going smooth!!')

finally:

print('This is executed compulsory!!!')

Output:

Exception: Please do define variable a before it is used!

This is executed compulsory!!!

Catching the specific Exception


● In Python, the except block may also catch a particular exception.

● In such a case, it handles only the particularized exception, if any other exception occurs it terminates the
execution.

Let's look at the following piece of code.

a = int(input('Enter a number: '))

b = int(input('Enter a number: '))

try:

c=a/b

print('Result: ', c)

except ZeroDivisionError:

print('Exception: value of b can not be zero!')

Output:

Enter a number: 10

Enter a number: 0

Exception: value of b cannot be zero!

Built In exception classes


ZeroDivisionError, ValueError, and TypeError are built-in exception classes in Python. They are part of the
standard Python language and represent specific types of exceptions that can occur in various situations.

1. ZeroDivisionError

try:

result = 10 / 0

except ZeroDivisionError as e:

print("Caught ZeroDivisionError:", e)

O/P:

Caught ZeroDivisionError: division by zero


2. ValueError

try:

num = int("abc")

except ValueError as e:

print("Caught ValueError:", e)

O/P:

Caught ValueError: invalid literal for int() with base 10: 'abc'

3. TypeError
try:
num = "123" + 456
except TypeError as e:
print("Caught TypeError:", e)

O/P:

Caught TypeError: can only concatenate str (not "int") to str

Catching Multiple Exceptions

●In Python, the except block may also catch multiple exceptions. In such a case, a single try block has multiple
except blocks.

●When an exception arises, it sent to the first except block if it can handle then rest of the except blocks are
ignored otherwise it forwarded to the next except block and so on.

●The above example code may generate two types of exceptions ZeroDivisionError and ValueError.

●Let's modify the above code to handle both the exception.

1.

try:

a = int(input('Enter a number: '))

b = int(input('Enter a number: '))

c=a/b

print('Result: ', c)

except ZeroDivisionError:

print('Exception_1: value of b can not be zero!')

except ValueError:

print('Exception_2: values of a and b must be numeric!!')

else:

print('No exception occurred!!!')


O/P:

Enter a number: a

Exception_2: values of a and b must be numeric!!

Multiple exceptions in a single block


def divide_numbers(a, b):

try:

result = a / b

return result

except (ZeroDivisionError, ValueError, TypeError) as e:

print("Caught a specific exception:", e)

except Exception as e:

print("Caught a generic exception:", e)

finally:

print("This block will always execute.")

# Test the function with different inputs

try:

result1 = divide_numbers(10, 2)

print("Result 1:", result1)

result2 = divide_numbers(10, 0)

print("Result 2:", result2)

result3 = divide_numbers(10, "2")

print("Result 3:", result3)

except Exception as e:

print("Outer exception caught:", e)

O/P:

This block will always execute.

Result 1: 5.0

Caught a specific exception: division by zero

This block will always execute.

Result 2: None

Caught a specific exception: unsupported operand type(s) for /: 'int' and 'str'

This block will always execute.


Result 3: None

Built-in and User Defined Exceptions


1. Built-in Exception Example
def convert_to_integer(input_str):

try:

number = int(input_str)

return number

except ValueError as e:

print(f"Error: {e}. Invalid input value!")

# Test the function with different inputs

try:

result1 = convert_to_integer("123")

print("Result 1:", result1)

result2 = convert_to_integer("abc")

print("Result 2:", result2)

except Exception as e:

print("Outer exception caught:", e)

O/P:

Result 1: 123

Error: invalid literal for int() with base 10: 'abc'. Invalid input value!

Result 2: None

2. User-defined Exception Example

class MyCustomException(Exception):

pass

def check_value(value):

if value < 0:

raise MyCustomException("Value cannot be negative.")

else:

print('its a positive value')

try:

check_value(10)

check_value(-5)
except MyCustomException as e:

print("Custom exception caught:", e)

O/P

its a positive value

Custom exception caught: Value cannot be negative.

In this example, we define a custom exception class MyCustomException, which inherits from the built-in
Exception class. We also create a function check_value that takes a value as input and raises MyCustomException
if the value is negative.

When we call check_value(10), no exception is raised because the value is positive. However, when we call
check_value(-5), the MyCustomException is raised, and the exception is caught by the except
MyCustomException block, which then prints "Custom exception caught: Value cannot be negative."

Lab-Program Week 10
1.Write a program which repeatedly reads numbers until the user enters ‘done’. Once ‘done’ is entered, print out
the total, count and average of the numbers. If the user enters anything other than a number, detect the Error
using try and except blocks, and print an error message, skip to the next number.

total = 0

count = 0

while True:

user_input = input("Enter a number (or 'done' to finish): ")

if user_input.lower() == 'done':

break

else:

try:

number = float(user_input)

total += number

count += 1

except ValueError:

print("Invalid input! Please enter a valid number or 'done' to finish.")

if count > 0:

average = total / count

print(f"Total: {total}")

print(f"Count: {count}")

print(f"Average: {average}")

else:
pass

O/P:

Enter a number (or 'done' to finish): 1

Enter a number (or 'done' to finish): 2

Enter a number (or 'done' to finish): 3

Enter a number (or 'done' to finish): 4

Enter a number (or 'done' to finish): 5

Enter a number (or 'done' to finish): done

Total: 15.0

Count: 5

Average: 3.0

Files
●Files are named locations on disk to store related information. They are used to permanently store data in a
non-volatile memory (e.g. hard disk).

●Since Random Access Memory (RAM) is volatile (which loses its data when the computer is turned off), we use
files for future use of the data by permanently storing them.

●When we want to read from or write to a file, we need to open it first. When we are done, it needs to be closed
so that the resources that are tied with the file are freed.

Hence, in Python, a file operation takes place in the following order:

✔ Open a file

✔ Read or write (perform operation)

✔ Close the file

Opening Files in Python:

●Python has a built-in open() function to open a file. This function returns a file object, also called a handle, as
it is used to read 0or modify the file accordingly.

Example

f = open("test.txt")#open file in current directory

f = open("D:/python/cse-files/test.txt") # specifying full path

●We can specify the mode while opening a file. In mode, we specify whether we want to read r, write w or
append a to the file.

●We can also specify if we want to open the file in text mode or binary mode.

●The default is reading in text mode. In this mode, we get strings when reading from the file.

●On the other hand, binary mode returns bytes and this is the mode to be used when dealing with non-text files
like images or executable files.
Mode Description

r Opens a file for reading. (default)

w Opens a file for writing. Creates a new file if it does not exist or truncates the file if it exists.

x Opens a file for exclusive creation. If the file already exists, the operation fails.

Opens a file for appending at the end of the file without truncating it. Creates a new file if it
a
does not exist.

t Opens in text mode. (default)

b Opens in binary mode.

+ Opens a file for updating (reading and writing)

●When working w

ith files in text mode, it is highly recommended to specify the encoding type.

Example

f = open("D:/python/cse-files/test.txt", mode='r', encoding='utf-8')

Closing Files in Python


●When we are done with performing operations on the file, we need to properly close the file.

●Closing a file will free up the resources that were tied with the file. It is done using the close() method available
in Python.

●Python has a garbage collector to clean up unreferenced objects but we must not rely on it to close the file.

f.close()
●This method is not entirely safe. If an exception occurs when we are performing some operation
with the file, the code exits without closing the file.
●A safer way is to use a try...finally block.
Example
try:
f = open("test.txt", encoding = 'utf-8')
# perform file operations
finally:
f.close()
●The best way to close a file is by using the with statement. This ensures that the file is closed when
the block inside the with statement is exited.
●We don't need to explicitly call the close() method. It is done internally.
Example
with open("test.txt", encoding = 'utf-8') as f:

pass

Writing to Files
●In order to write into a file in Python, we need to open it in write w, append a or exclusive creation x mode.

●We need to be careful with the w mode, as it will overwrite into the file if it already exists. Due to this, all the
previous data are erased.

●Writing a string or sequence of bytes (for binary files) is done using the write() method. This method returns
the number of characters written to the file.

Example

with open("D:/python/cse-files/test.txt",'w',encoding = 'utf-8') as f:

f.write("my first file\n")

f.write("This file\n\n")

f.write("contains three lines\n")

●This program will create a new file named test.txt in the current directory if it does not exist. If it does exist, it
is overwritten.

●We must include the newline characters ourselves to distinguish the different lines.

Reading Files
●To read a file in Python, we must open the file in reading r mode.
●There are various methods available for this purpose. We can use the read(size) method to read in the size
amount of data. If the size parameter is not specified, it reads and returns up to the end of the file.
●We can read the text.txt file we wrote in the above section in the following way:
Example
f = open("D:/python/cse-files/test.txt",'r',encoding = 'utf-8')

print(f.read(4)) # read the first 4 data

print(f.read(4)) # read the next 4 data

print(f.read()) # read in the rest till end of file

print(f.read())#further reading returns empty sting

O/P:

my f
irst

file

This file

contains three lines

●We can see that the read() method returns a newline as '\n'. Once the end of the file is reached, we get an
empty string on further reading.

●We can change our current file cursor (position) using the seek() method. Similarly, the tell() method returns
our current position (in number of bytes).

Example

f = open("D:/python/cse-files/test.txt",'r',encoding = 'utf-8')

print(f.read(4))# read the first 4 data

print(f.read(4))# read the next 4 data

print(f.read()) # read in the rest till end of file

print(f.read())#further reading returns empty sting

print(f.tell())# get the current file position

print(f.seek(0))#bring file cursor to initial position

print(f.read()) # read the entire file

O/P:

my f

irst

file

This file

contains three lines

50

my first file

This file

contains three lines

●We can read a file line-by-line using a for loop. This is both efficient and fast.
f = open("D:/python/cse-files/test.txt",'r',encoding = 'utf-8')

for line in f:

print(line, end = '')

O/P:

my first file

This file

contains three lines

●In this program, the lines in the file itself include a newline character \n. So, we use the end parameter of the
print() function to avoid two newlines when printing.

●Alternatively, we can use the readline() method to read individual lines of a file. This method reads a file till the
newline, including the newline character.

readline()
The readline() method returns one line from the file.

You can also specify how many bytes from the line to return, by using the size parameter.

Syntax

file.readline(size)
f = open("D:/python/cse-files/test.txt", "r")

print(f.readline())

print(f.readline())

O/P

my first file

This file contains three lines

File Positions
seek() method
In Python, seek() function is used to change the position of the File Handle to a given specific position. File handle
is like a cursor, which defines from where the data has to be read or written in the file.

Syntax: f.seek(offset, from_what), where f is file pointer

Parameters:

Offset: Number of positions to move forward

from_what: It defines point of reference.

Returns: Return the new absolute position.

The reference point is selected by the from_what argument. It accepts three values:
0: sets the reference point at the beginning of the file

1: sets the reference point at the current file position

2: sets the reference point at the end of the file

By default from_what argument is set to 0.

Note: Reference point at current position / end of file cannot be set in text mode except when offset is equal to
0.

Example1

f = open("D:/python/cse-files/test.txt", "r")

# Second parameter is by default 0

# sets Reference point to twentieth

# index position from the beginning

f.seek(20)

# prints current position

print(f.tell())

print(f.readline())

f.close()

O/P:

20

When you have to explain it, it is bad."

Example2

f = open("D:/python/cse-files/test1.txt", "rb")

# sets Reference point to tenth

# position to the left from end

f.seek(-10, 2)

# prints current position

print(f.tell())

# Converting binary to string and

# printing

print(f.readline().decode('utf-8'))

f.close()

O/P:

50
its bad.'

Lab Program week-10

2. Write a python script to perform file operations (creating, open, read and write-use exception

handling)

def create_file(file_name):

try:

with open(file_name, 'x'): # 'x' mode opens the file for exclusive creation, raises FileExistsError if the file
already exists

print(f"File '{file_name}' created successfully.")

except FileExistsError:

print(f"File '{file_name}' already exists.")

def write_to_file(file_name, data):

try:

with open(file_name, 'a') as file:

file.write(data + "\n")

print("Data written to the file successfully.")

except FileNotFoundError:

print(f"File '{file_name}' not found.")

except Exception as e:

print(f"Error occurred while writing to the file: {e}")

def read_file(file_name):

try:

with open(file_name, 'r') as file:

content = file.read()

print("File content:")

print(content)

except FileNotFoundError:

print(f"File '{file_name}' not found.")

except Exception as e:

print(f"Error occurred while reading the file: {e}")


def main():

file_name = input("Enter the name of the file: ")

create_file(file_name)

data_to_write = input("Enter data to write to the file: ")

write_to_file(file_name, data_to_write)

print("\nReading the file:")

read_file(file_name)

if __name__ == "__main__":

main()

O/P:

Enter the name of the file: code.txt

File 'code.txt' created successfully.

Enter data to write to the file: coding is interesting but difficult to explain

Data written to the file successfully.

Reading the file:

File content:

coding is interesting but difficult to explain

Python Libraries and Packages


PIP
What is a PIP?

PIP, short for "Python Package Index," is a package manager used to install and manage Python packages. It
allows you to easily install, upgrade, and uninstall Python libraries and dependencies from the Python Package
Index or other repositories.

Here's an example of how to use PIP to install a package:

Step 1: Open a command-line interface (terminal or command prompt).

Step 2: Check if PIP is installed on your system. Type the following command and press Enter:

pip –version

O/P: pip 23.1.2 from C:\Python311\Lib\site-packages\pip (python 3.11)

This command will display the version of PIP installed on your system if it is available. If PIP is not installed, you'll
need to install it first. However, in most cases, if you're using a recent version of Python, PIP comes bundled by
default.

If PIP is not installed or you are using an older version of Python, you can follow these steps to install PIP:
First Way

Step 1: Download get-pip.py: Open your web browser and visit the official "get-pip.py" script link:
https://bootstrap.pypa.io/get-pip.py. Right-click on the page and choose "Save Page As" or "Save Link As" to
download the file. Save it in a location you can easily access, such as your Downloads folder.

Step2:

For Python 3.x:

python3 get-pip.py

Second Way

If you do not have PIP installed, you can download and install it from this page: https://pypi.org/project/pip/

What is a Package?

A package contains all the files you need for a module.

Modules are Python code libraries you can include in your project.

my_package/

__init__.py

module1.py

module2.py

Installation of package using pip

Let's say you want to install the requests library, which is commonly used for making HTTP requests in Python.
To install it, use the following command

pip install requests

pip show requests

Working With a package

import camelcase
c = camelcase.CamelCase()
txt = "hello world"
print(c.hump(txt))
O/P:

Hello World

Remove a Package
uninstall <package-name>

List Packages
pip list
Numpy
NumPy is a Python library. NumPy is used for working with arrays. NumPy is short for "Numerical Python". It also
has functions for working in domain of linear algebra, Fourier transform, and matrices.

NumPy was created in 2005 by Travis Oliphant. It is an open-source project and you can use it freely.

Why Use NumPy?


In Python we have lists that serve the purpose of arrays, but they are slow to process.

NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.

The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with
ndarray very easy.

Arrays are very frequently used in data science, where speed and resources are very important.

Why is NumPy Faster Than Lists?


NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate
them very efficiently.

This behavior is called locality of reference in computer science.

This is the main reason why NumPy is faster than lists. Also, it is optimized to work with latest CPU architectures.

Which Language is NumPy written in?

NumPy is a Python library and is written partially in Python, but most of the parts that require fast
computation are written in C or C++.

Where is the NumPy Codebase?


The source code for NumPy is located at this github repository https://github.com/numpy/numpy

github: enables many people to work on the same codebase.

Installation of NumPy
If you have Python and PIP already installed on a system, then installation of NumPy is very easy.

Install it using this command:

C:\Users\Your Name>pip install numpy

If this command fails, then use a python distribution that already has NumPy installed like, Anaconda, Spyder
etc.

Import NumPy
Once NumPy is installed, import it in your applications by adding the import keyword:

import numpy

Now NumPy is imported and ready to use.

Example
import numpy

arr = numpy.array([1, 2, 3, 4, 5])

print(arr)

O/P:

[1 2 3 4 5]

NumPy as np
NumPy is usually imported under the np alias.

alias: In Python alias are an alternate name for referring to the same thing.

Create an alias with the as keyword while importing:

import numpy as np

Now the NumPy package can be referred to as np instead of numpy.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)

O/P:

[1 2 3 4 5]

Creating Arrays Using Numpy


Example1:
import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)

print(type(arr))

Output:

[1,2,3,4,5]

<class ‘numpy.ndarray’>

Example2:

import numpy as np

arr = np.array((1, 2, 3, 4, 5))


print(arr)

Output:

[1,2,3,4,5]

Dimensions in Arrays

0-D Arrays

0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.

Example
Create a 0-D array with value 42

import numpy as np
arr = np.array(42)
print(arr)
Output:
42

1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
These are the most common and basic arrays.
Example
Create a 1-D array containing the values 1,2,3,4,5:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Output:
[1, 2, 3, 4, 5])
2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.

These are often used to represent matrix or 2nd order tensors.

NumPy has a whole sub module dedicated towards matrix operations called numpy.mat

Example

Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr)
Output:

[[1,2,3]

[4,5,6]]

3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D array.

These are often used to represent a 3rd order tensor.

Example

Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6:

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(arr)

Output:

[[[1,2,3]

[4,5,6]]

[[1,2,3]

4,5,6]]]

Check Number of Dimensions


NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array
have.

import numpy as np

a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
O/P:

3
Slicing arrays
Slicing in python means taking elements from one given index to another given index.

We pass slice instead of index like this: [start:end].

We can also define the step, like this: [start:end:step].

If we don't pass start its considered 0

If we don't pass end its considered length of array in that dimension

If we don't pass step its considered 1

Example1

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5])
O/P:
[2 3 4 5]
Example2

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[4:])
O/P:
[5 6 7]
Example3
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[:4])
O/P:
[1 2 3 4]
Example4
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])


print(arr[1:5:2])
O/P:
[2 4]

Negative Slicing
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[-3:-1])
O/P:
[5 6]

Data Types in NumPy


NumPy has some extra data types, and refer to data types with one character, like i for integers, u for unsigned
integers etc.

Below is a list of all data types in NumPy and the characters used to represent them.

• i - integer

• b - boolean

• u - unsigned integer

• f - float

• c - complex float

• m - timedelta

• M - datetime

• O - object

• S - string

• U - unicode string

• V - fixed chunk of memory for other type ( void )

Checking the Data Type of an Array


Example1:

import numpy as np

arr = np.array([1, 2, 3, 4])

print(arr.dtype)
O/P:

int32

Example2

import numpy as np

arr = np.array(['apple', 'banana', 'cherry'])

print(arr.dtype)

O/P:

<U6

Joining NumPy Arrays


We pass a sequence of arrays that we want to join to the concatenate() function, along with the axis.
If axis is not explicitly passed, it is taken as 0.
import numpy as np

arr1 = np.array([1, 2, 3])

arr2 = np.array([4, 5, 6])

arr = np.concatenate((arr1, arr2))

print(arr)

O/P:

[1 2 3 4 5 6]

Join two 2-D arrays along rows (axis=1)

import numpy as np

arr1 = np.array([[1, 2], [3, 4]])

arr2 = np.array([[5, 6], [7, 8]])

arr = np.concatenate((arr1, arr2), axis=1)

print(arr)

O/P:

[[1 2 5 6]

[3 4 7 8]]

import numpy as np

arr1 = np.array([[1, 2], [3, 4]])

arr2 = np.array([[5, 6], [7, 8]])


arr = np.concatenate((arr1, arr2), axis=0)

print(arr)

O/P:

[[1 2]

[3 4]

[5 6]

[7 8]]

Splitting NumPy Arrays


We use array_split() for splitting arrays, we pass it the array we want to split and the number of splits.

Example1:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

newarr = np.array_split(arr, 3)

print(newarr)

O/P:

[array([1, 2]), array([3, 4]), array([5, 6])]

Example2:

import numpy as np

arr = np.array([1, 2, 3, 4])

newarr = np.array_split(arr, 3)

print(newarr)

O/P:

[array([1, 2]), array([3]), array([4])]

Example3:

import numpy as np

arr = np.array([1, 2,3])

newarr = np.array_split(arr, 2)

print(newarr)

O/P:

[array([1, 2]), array([3])]

Searching Arrays
To search an array, use the where() method.
Find the indexes where the value is 4:
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 4, 4])

x = np.where(arr == 4)

print(x)

O/P:

(array([3, 5, 6], dtype=int64),)

Sorting Arrays
The NumPy ndarray object has a function called sort(), that will sort a specified array.

import numpy as np

arr = np.array([3, 2, 0, 1])

print(np.sort(arr))

O/P:

[0 1 2 3]

Iterating Arrays
import numpy as np

arr = np.array([1, 2, 3])

for x in arr:

print(x)

O/P:

3
The Difference Between Copy and View

The main difference between a copy and a view of an array is that the copy is a new array, and the view is just a
view of the original array.

The copy owns the data and any changes made to the copy will not affect original array, and any changes made
to the original array will not affect the copy.

The view does not own the data and any changes made to the view will affect the original array, and any changes
made to the original array will affect the view.

COPY

import numpy as np

arr = np.array([1, 2, 3, 4, 5])


x = arr.copy()

arr[0= = 42

print(arr)

print(x)

O/P:

[42 2 3 4 5]

[1 2 3 4 5]

VIEW

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

x = arr.view()

arr[0] = 42

print(arr)

print(x)

O/P:

[42 2 3 4 5]

[42 2 3 4 5]

Shape of an Array
NumPy arrays have an attribute called shape that returns a tuple with each index having the number of
corresponding elements.

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

print(arr.shape)

O/P:

(2, 4)

Reshape From 1-D to 2-D


import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

newarr = arr.reshape(4, 3)

print(newarr)

O/P:
[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

Shuffling Arrays
from numpy import random
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
random.shuffle(arr)
print(arr)

Output:

[2 1 5 4 3]

What is a Random Number?


Random number does NOT mean a different number every time. Random means something that can not be
predicted logically.

Pseudo Random and True Random.

Computers work on programs, and programs are definitive set of instructions. So it means there must be some
algorithm to generate a random number as well.

If there is a program to generate random number it can be predicted, thus it is not truly random.

Random numbers generated through a generation algorithm are called pseudo random.

Can we make truly random numbers?

Yes. In order to generate a truly random number on our computers we need to get the random data from some
outside source. This outside source is generally our keystrokes, mouse movements, data on network etc.

We do not need truly random numbers, unless its related to security (e.g. encryption keys) or the basis of
application is the randomness (e.g. Digital roulette wheels).

In this tutorial we will be using pseudo random numbers.

Generate Random Number


NumPy offers the random module to work with random numbers.

Example

Generate a random integer from 0 to 100:

from numpy import random

x = random.randint(100)

print(x)

Output:

28
Matrix Addition
import numpy as np

# Define two 2x2 matrices

matrix1 = np.array([[1, 2], [3, 4]])

matrix2 = np.array([[5, 6], [7, 8]])

# Perform matrix addition

result = matrix1 + matrix2

print("Matrix 1:")

print(matrix1)

print("\nMatrix 2:")

print(matrix2)

print("\nResult of Matrix Addition:")

print(result)

O/P:

Matrix 1:

[[1 2]

[3 4]]

atrix 2:

[[5 6]

[7 8]]

Result of Matrix Addition:

[[ 6 8]

[10 12]]

Matrix Multiplication
import numpy as np

# Define two matrices

matrix1 = np.array([[1, 2], [3, 4]])

matrix2 = np.array([[5, 6], [7, 8]])

# Perform matrix multiplication

result_dot = np.dot(matrix1, matrix2) # using dot function

result_operator = matrix1 @ matrix2 # using @ operator

print("Matrix 1:")
print(matrix1)

print("\nMatrix 2:")

print(matrix2)

print("\nResult of Matrix Multiplication (dot function):")

print(result_dot)

print("\nResult of Matrix Multiplication (@ operator):")

print(result_operator)

O/P:

Matrix 1:

[[1 2]

[3 4]]

Matrix 2:

[[5 6]

[7 8]]

Result of Matrix Multiplication (dot function):

[[19 22]

[43 50]]

Result of Matrix Multiplication (@ operator):

[[19 22]

[43 50]]

Pandas
What is Pandas?
Pandas is a Python library used for working with data sets. It has functions for analyzing, cleaning, exploring, and
manipulating data.The name "Pandas" has a reference to both "Panel Data", and "Python Data Analysis" and was
created by Wes McKinney in 2008.

Use of Pandas
Pandas allows us to analyze big data and make conclusions based on statistical theories.

Pandas can clean messy data sets, and make them readable and relevant.

Relevant data is very important in data science.

What Can Pandas Do?


Pandas gives you answers about the data. Like:
• Is there a correlation between two or more columns?

• What is average value?

• Max value?

• Min value?

Pandas are also able to delete rows that are not relevant, or contains wrong values, like empty or NULL values.
This is called cleaning the data.

Where is the Pandas Codebase?


The source code for Pandas is located at this github repository https://github.com/pandas-dev/pandas

github: enables many people to work on the same codebase.

Installation of Pandas
If you have Python and PIP already installed on a system, then installation of Pandas is very easy.

Install it using this command:

C:\Users\Your Name>pip install pandas

If this command fails, then use a python distribution that already has Pandas installed like, Anaconda, Spyder
etc.

Import Pandas

Once Pandas is installed, import it in your applications by adding the import keyword:

import pandas

Now Pandas is imported and ready to use.

Example

1:

import pandas

mydataset = {

'cars': ["BMW", "Volvo", "Ford"],

'passings': [3, 7, 2]

myvar = pandas.DataFrame(mydataset)

print(myvar)

O/P:
cars passings

0 BMW 3

1 Volvo 7

2 Ford 2

What is a DataFrame
A Pandas DataFrame is a 2-dimensional data structure, like a 2-dimensional array, or a table with rows and
columns.

Example

import pandas as pd

data = {

"calories": [420, 380, 390],

"duration": [50, 40, 45]

#load data into a DataFrame object:

df = pd.DataFrame(data)

print(df)

O/P:

calories duration

0 420 50

1 380 40

2 390 45

Pandas Series
What is a Series?
A Pandas Series is like a column in a table.It is a one-dimensional array holding data of any type.

Example

import pandas as pd

a = [1, 7, 2]

myvar = pd.Series(a)
print(myvar)

O/P:

0 1

1 7

2 2

dtype: int64

Create Labels
With the index argument, you can name your own labels.

import pandas as pd

a = [1, 7, 2]

myvar = pd.Series(a, index = ["x", "y", "z"])

print(myvar)

O/P:

x 1

y 7

z 2

dtype: int64

Read CSV Files


A simple way to store big data sets is to use CSV files (comma separated files). CSV files contains plain text and
is a well know format that can be read by everyone including Pandas. In our examples we will be using a CSV
file called 'data.csv'.

Example

import pandas as pd

df = pd.read_csv('D:/python/cse-files/venev/data1.csv')

# For example, you can print the first few rows using the 'head()' method:

print(df.head())

O/P:

rowid pl_name ... disc_telescope disc_instrument

0 1 11 Com b ... 2.16 m Telescope Coude Echelle Spectrograph


1 2 11 UMi b ... 2.0 m Alfred Jensch Telescope Coude Echelle Spectrograph

2 3 14 And b ... 1.88 m Telescope HIDES Echelle Spectrograph

3 4 14 Her b ... 10 m Keck I Telescope HIRES Spectrometer

4 5 16 Cyg B b ... Multiple Telescopes Multiple Instruments

[5 rows x 20 columns]

Viewing the Data


One of the most used method for getting a quick overview of the DataFrame, is the head() method.

The head() method returns the headers and a specified number of rows, starting from the top.

Example

import pandas as pd

df = pd.read_csv('D:/python/cse-files/venev/data1.csv')

print(df.head(10))

O/P:

rowid ... disc_instrument

0 1 ... Coude Echelle Spectrograph

1 2 ... Coude Echelle Spectrograph

2 3 ... HIDES Echelle Spectrograph

3 4 ... HIRES Spectrometer

4 5 ... Multiple Instruments

5 6 ... Hamilton Echelle Spectrograph

6 7 ... HIDES Echelle Spectrograph

7 8 ... NIRI Camera

8 9 ... HIDES Echelle Spectrograph

9 10 ... Hamilton Echelle Spectrograph

[10 rows x 20 columns]

There is also a tail() method for viewing the last rows of the DataFrame.

The tail() method returns the headers and a specified number of rows, starting from the bottom.

Example

import pandas as pd

df = pd.read_csv('D:/python/cse-files/venev/data1.csv')

print(df.tail(5))
O/P:

rowid ... disc_instrument

18 19 ... NIRC2 Camera

19 20 ... Coude Echelle Spectrograph

20 21 ... Coude Echelle Spectrograph

21 22 ... Coude Echelle Spectrograph

22 23 ... Hamilton Echelle Spectrograph

[5 rows x 20 columns]

SciPy
SciPy is a scientific computation library that uses NumPy underneath.

SciPy stands for Scientific Python.

It provides more utility functions for optimization, stats and signal processing.

Like NumPy, SciPy is open source so we can use it freely.

SciPy was created by NumPy's creator Travis Olliphant.

Why Use SciPy?


If SciPy uses NumPy underneath, why can we not just use NumPy?

SciPy has optimized and added functions that are frequently used in NumPy and Data Science.

Which Language is SciPy Written in?


SciPy is predominantly written in Python, but a few segments are written in C.

Where is the SciPy Codebase?


The source code for SciPy is located at this github repository https://github.com/scipy/scipy

github: enables many people to work on the same codebase.

Installation of SciPy
If you have Python and PIP already installed on a system, then installation of SciPy is very easy.

Install it using this command:

C:\Users\Your Name>pip install scipy

If this command fails, then use a Python distribution that already has SciPy installed like, Anaconda, Spyder etc.

Checking SciPy Version

import scipy

print(scipy.__version__)
Import SciPy
Once SciPy is installed, import the SciPy module(s) you want to use in your applications by adding the from scipy
import module statement.

from scipy import constants

Now we have imported the constants module from SciPy, and the application is ready to use it

Constants

As SciPy is more focused on scientific implementations, it provides many built-in scientific constants.

These constants can be helpful when you are working with Data Science.

Example1

from scipy import constants

print(constants.liter)

O/P:

0.001

constants: SciPy offers a set of mathematical constants, one of them is liter which returns 1 liter as cubic
meters.

Example2

from scipy import constants

print(constants.pi)

O/P:

3.141592653589793

Time:

Return the specified unit in seconds (e.g. hour returns 3600.0)

from scipy import constants

print(constants.minute) #60.0

print(constants.hour) #3600.0

print(constants.day) #86400.0

print(constants.week) #604800.0

print(constants.year) #31536000.0

print(constants.Julian_year) #31557600.0

Scipy Optimizers

Optimizers are a set of procedures defined in SciPy that either find the minimum value of a function, or the root
of an equation.

Roots of an Equation

NumPy is capable of finding roots for polynomials and linear equations, but it cannot find roots for nonlinear-
equations, like this one:

x + cos(x)

For that you can use SciPy's optimze.root function.

This function takes two required arguments:

fun - a function representing an equation.

x0 - an initial guess for the root.

The function returns an object with information regarding the solution.

The actual solution is given under attribute x of the returned object:

from scipy.optimize import root

from math import cos

def eqn(x):

return x + cos(x)

myroot = root(eqn, 0)

print(myroot.x)

O/P:

[-0.73908513]

Finding Minima

We can use scipy.optimize.minimize() function to minimize the function.

The minimize() function takes the following arguments:

fun - a function representing an equation.

x0 - an initial guess for the root.

method - name of the method to use. Legal values:

'CG'

'BFGS'

'Newton-CG'

'L-BFGS-B'

'TNC'

'COBYLA'

'SLSQP'

callback - function called after each iteration of optimization.

from scipy.optimize import minimize


def eqn(x):

return x**2 + x + 2

mymin = minimize(eqn, 0, method='BFGS')

print(mymin)

O/P:

x: array([-0.50000001]

Note: BFGS-Broyden–Fletcher–Goldfarb–Shanno algorithm

Matplotlib
Matplotlib is a low-level graph plotting library in python that serves as a visualization utility.

Matplotlib was created by John D. Hunter.

Matplotlib is open source and we can use it freely.

Matplotlib is mostly written in python, a few segments are written in C, Objective-C and Javascript for Platform
compatibility.

Where is the Matplotlib Codebase?


The source code for Matplotlib is located at this github repository https://github.com/matplotlib/matplotlib

Installation of Matplotlib
If you have Python and PIP already installed on a system, then installation of Matplotlib is very easy.

Install it using this command:

C:\Users\Your Name>pip install matplotlib

If this command fails, then use a python distribution that already has Matplotlib installed, like Anaconda, Spyder
etc.

Import Matplotlib
Once Matplotlib is installed, import it in your applications by adding the import module statement:

import matplotlib

Now Matplotlib is imported and ready to use:

Checking Matplotlib Version


import matplotlib

print(matplotlib.__version__)

O/P:

3.5.3

Pyplot
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under the plt alias:
import matplotlib.pyplot as plt

Now the Pyplot package can be referred to as plt.

Example1

import matplotlib.pyplot as plt

import numpy as np

xpoints = np.array([0, 6])

ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)

plt.show()

O/P:

Example2

import matplotlib.pyplot as plt

import numpy as np

xpoints = np.array([1, 8])

ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)

plt.show()

O/P:
Markers

You can use the keyword argument marker to emphasize each point with a specified marker:

Example

Mark each point with a circle:

import matplotlib.pyplot as plt

import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o')

plt.show()

Result:

Create Labels for a Plot

With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.

Example

import numpy as np

import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])

y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.plot(x, y)
plt.xlabel("Average Pulse")

plt.ylabel("Calorie Burnage")

plt.show()

Result:

Add Grid Lines to a Plot

With Pyplot, you can use the grid() function to add grid lines to the plot.

Example

import numpy as np

import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])

y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data")

plt.xlabel("Average Pulse")

plt.ylabel("Calorie Burnage")

plt.plot(x, y)

plt.grid()

plt.show()

Result:
Display Multiple Plots

With the subplots() function you can draw multiple plots in one figure:

Example

Draw 2 plots:

import matplotlib.pyplot as plt

import numpy as np

#plot 1:

x = np.array([0, 1, 2, 3])

y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)

plt.plot(x,y)

#plot 2:

x = np.array([0, 1, 2, 3])

y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)

plt.plot(x,y)

plt.show()

Result:

Creating Scatter Plots

With Pyplot, you can use the scatter() function to draw a scatter plot.

The scatter() function plots one dot for each observation. It needs two arrays of the same length, one for the
values of the x-axis, and one for values on the y-axis:

Example

A simple scatter plot:

import matplotlib.pyplot as plt


import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])

y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])

plt.scatter(x, y)

plt.show()

Result:

Creating Bars

With Pyplot, you can use the bar() function to draw bar graphs:

Example

Draw 4 bars:

import matplotlib.pyplot as plt

import numpy as np

x = np.array(["A", "B", "C", "D"])

y = np.array([3, 8, 1, 10])

plt.bar(x,y)

plt.show()

Result:
Histogram

A histogram is a graph showing frequency distributions.

It is a graph showing the number of observations within each given interval.

Example

import matplotlib.pyplot as plt

import numpy as np

# Generating 1000 random data points from a normal distribution

data = np.random.normal(loc=0, scale=1, size=1000)

# Plotting the histogram

plt.hist(data, bins=30, alpha=0.7, color='blue', edgecolor='black')

# Set labels and title

plt.xlabel('Values')

plt.ylabel('Frequency')

plt.title('Histogram Example')

# Show the plot

plt.show()

PIE CHART
import matplotlib.pyplot as plt

import numpy as np

y = np.array([35, 25, 25, 15])

plt.pie(y)

plt.show()

O/P

Labels

Add labels to the pie chart with the label parameter.

The label parameter must be an array with one label for each wedge

import matplotlib.pyplot as plt

import numpy as np

y = np.array([35, 25, 25, 15])

mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)

plt.show()

You might also like