UNIT 5 notes
UNIT 5 notes
UNIT 5 notes
Example:
import os
f = open('demofile.txt', 'r')
OUTPUT:
This text is an example.
text is an example.
Tell():
The seek() function is used to set the position of the file cursor, whereas the tell() function returns
the position where the cursor is set to begin reading.
Syntax:
tell()
The tell() function takes no parameter.
Example:
# Open a file
fo = open("demo.txt", "w")
print("Name of the file: ", fo.name)
# Write into the file using write() method
fo.write("This is a demo file")
# Get the current position of the file.
pos = fo.tell()
print("Current Position:", pos)
# Close opened file
fo.close()
Output:
Name of the file: demo.txt
Current Position: 19
Handling Binary Data:
Alright, lets get this out of the way! The basics are pretty standard:
1. There are 8 bits in a byte
2. Bits either consist of a 0 or a 1
3. A byte can be interpreted in different ways, like binary octal or hexadecimal
Note: These are not character encodings, those come later. This is just a way to look at a set of 1’s and 0’s
and see it in three different ways(or number systems).
Examples:
Input: 10011011
Output:
1001 1011 ---- 9B (in hex)
1001 1011 ---- 155 (in decimal)
1001 1011 ---- 233 (in octal)
Now that we know what a byte is and what it looks like, let us see how it is interpreted, mainly
in strings.
Character Encodings are a way to assign values to bytes or sets of bytes that represent a certain
character in that scheme.
Some encodings are ASCII(probably the oldest), Latin, and UTF-8(most widely used as of today. In a
sense encodings are a way for computers to represent, send and interpret human readable
characters.
This means that a sentence in one encoding might become completely incomprehensible in another
encoding.
Python and Bytes
From a developer’s point of view, the largest change in Python 3 is the handling of strings.
In Python 2, the str type was used for two different kinds of values – text and bytes, whereas in
Python 3, these are separate and incompatible types.
This means that before Python3 we could treat a set of bytes as a string and work from there, this
is not the case now, now we have a separate data type, called bytes.
This data type can be briefly explained as a string of bytes, which essentially means, once the
bytes data type is initialized it is immutable.
Example:
bytesArr = bytearray(b'\x00\x0F')
# Bytearray allows modification
bytesArr[0] = 255
bytesArr.append(255)
print(bytesArr)
Output:
bytearray(b'\xff\x0f\xff')
Bitwise Operations
In Python, bitwise operators are used to perform bitwise calculations on integers. The integers are
first converted into binary and then operations are performed on bit by bit, hence the name bitwise
operators. The standard bitwise operations are demonstrated below.
Example:
# Code to demonstrate bitwise operations
# Some bytes to play with
byte1 = int('11110000', 2) # 240
byte2 = int('00001111', 2) # 15
byte3 = int('01010101', 2) # 85
# Ones Complement (Flip the bits)
print(~byte1)
# AND
print(byte1 & byte2)
# OR
print(byte1 | byte2)
# XOR
print(byte1 ^ byte3)
# Shifting right will lose the
# right-most bit
print(byte2 >> 3)
# Shifting left will add a 0 bit
# on the right side
print(byte2 << 1)
# See if a single bit is set
bit_mask = int('00000001', 2) # Bit 1
# Is bit set in byte1?
print(bit_mask & byte1)
# Is bit set in byte2?
print(bit_mask & byte2)
Output:
-241
0
255
165
1
30
0
1
Handling CSV Files:
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a
spreadsheet or database.
A CSV file stores tabular data (numbers and text) in plain text.
Each line of the file is a data record. Each record consists of one or more fields, separated by
commas.
The use of the comma as a field separator is the source of the name for this file format. For working
CSV files in Python, there is an inbuilt module called CSV.
Working with CSV files in Python:
Reading a CSV file
Reading CSV Files Into a Dictionary With csv
Writing to a CSV file
Writing a dictionary to a CSV file
Reading CSV Files With Pandas
Writing CSV Files With Pandas
Storing email in CSV file
Reading a CSV file:
Reading from a CSV file is done using the reader object. The CSV file is opened as a text file with
Python’s built-in open() function, which returns a file object.
In this example, we first open the CSV file in READ mode, file object is converted to csv.reader object
and further operation takes place. Code and detailed explanation is given below.
# importing csv module
import csv
# csv file name
filename = "aapl.csv"
# initializing the titles and rows list
fields = []
rows = []
# reading csv file
with open(filename, 'r') as csvfile:
# creating a csv reader object
csvreader = csv.reader(csvfile)
# extracting field names through first row
fields = next(csvreader)
# extracting each data row one by one
for row in csvreader:
rows.append(row)
# get total number of rows
print("Total no. of rows: %d" % (csvreader.line_num))
name,department,birthday_month
John Smith,HR,July
Alice Johnson,IT,October
Bob Williams,Finance,January
Writing to a CSV file:
To write to a CSV file, we first open the CSV file in WRITE mode. The file object is converted to
csv.writer object and further operations takes place. Code and detailed explanation is given below.
# importing the csv module
import csv
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']
# data rows of csv file
rows = [['Nikhil', 'COE', '2', '9.0'],
['Sanchit', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]
# name of csv file
filename = "university_records.csv"
# writing to csv file
with open(filename, 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(rows)
Let us try to understand the above code in pieces.
fields and rows have been already defined. fields is a list containing all the field names. rows is a list
of lists. Each row is a list containing the field values of that row.
with open(filename, 'w') as csvfile:
csvwriter = csv.writer(csvfile)
Here, we first open the CSV file in WRITE mode. The file object is named as csvfile . The file object is
converted to csv.writer object. We save the csv.writer object as csvwriter .
csvwriter.writerow(fields)
Now we use writerow method to write the first row which is nothing but the field names.
csvwriter.writerows(rows)
We use writerows method to write multiple rows at once.
Modules:
A Python module is a file containing Python definitions and statements. A module can define functions,
classes, and variables. A module can also include runnable code.
Create a Python Module:
# A simple module, calc.py
def add(x, y):
return (x+y)
def subtract(x, y):
return (x-y)
Import module in Python:
Import the functions, and classes defined in a module to another module using the import
statement in some other Python source file.
When the interpreter encounters an import statement, it imports the module if the module is present in the
search path.
Syntax to Import Module in Python
import module
Importing modules in Python Example:
# importing module calc.py
import calc
print(calc.add(10, 2))
Python Import From Module:
# importing sqrt() and factorial from the
# module math
from math import sqrt, factorial
# if we simply do "import math", then
# math.sqrt(16) and math.factorial()
# are required.
print(sqrt(16))
print(factorial(6))
Output:
4.0
720
Import all Names:
The * symbol used with the import statement is used to import all the names from a module to a current
namespace.
Syntax:
from module_name import *
Example:
# importing sqrt() and factorial from the
# module math
from math import *
# if we simply do "import math", then
# math.sqrt(16) and math.factorial()
# are required.
print(sqrt(16))
print(factorial(6))
Output:
4.0
720
Locating Python Modules:
First, it searches for the module in the current directory.
If the module isn’t found in the current directory, Python then searches each directory in the shell
variable PYTHONPATH. The PYTHONPATH is an environment variable, consisting of a list of
directories.
If that also fails python checks the installation-dependent list of directories configured at the time
Python is installed.
# Sine of 2 radians
print(math.sin(2))
# 1 * 2 * 3 * 4 = 24
print(math.factorial(4))
Packages:
Python Packages are a way to organize and structure your Python code into reusable components.
Think of it like a folder that contains related Python files (modules) that work together to provide
certain functionality.
Packages help keep your code organized, make it easier to manage and maintain, and allow you to
share your code with others.
They’re like a toolbox where you can store and organize your tools (functions and classes) for
easy access and reuse in different projects.
How to Create Package in Python?
Creating packages in Python allows you to organize your code into reusable and manageable modules.
Here’s a brief overview of how to create packages:
Create a Directory: Start by creating a directory (folder) for your package. This directory will
serve as the root of your package structure.
Add Modules: Within the package directory, you can add Python files (modules) containing your
code. Each module should represent a distinct functionality or component of your package.
Init File: Include an __init__.py file in the package directory. This file can be empty or can contain
an initialization code for your package. It signals to Python that the directory should be treated as
a package.
Subpackages: You can create sub-packages within your package by adding additional directories
containing modules, along with their own __init__.py files.
Importing: To use modules from your package, import them into your Python scripts using dot
notation. For example, if you have a module named module1.py inside a package named
mypackage, you would import its function like this: from mypackage.module1 import greet.
Distribution: If you want to distribute your package for others to use, you can create a setup.py
file using Python’s setuptools library. This file defines metadata about your package and specifies
how it should be installed.
Code Example
Here’s a basic code sample demonstrating how to create a simple Python package:
1. Create a directory named mypackage.
2. Inside mypackage, create two Python files: module1.py and module2.py.
3. Create an __init__.py file inside mypackage (it can be empty).
4. Add some code to the modules.
5. Finally, demonstrate how to import and use the modules from the package.
mypackage/
│
├── __init__.py
├── module1.py
└── module2.py
Python Numpy:
Numpy is a general-purpose array-processing package. It provides a high-performance
multidimensional array object, and tools for working with these arrays. It is the fundamental package for
scientific computing with Python.
Arrays in Numpy
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of
positive integers. In Numpy, number of dimensions of the array is called rank of the array.A tuple of
integers giving the size of the array along each dimension is known as shape of the array. An array class
in Numpy is called as ndarray. Elements in Numpy arrays are accessed by using square brackets and can
be initialized by using nested Python Lists.
Creating a Numpy Array
Arrays in Numpy can be created by multiple ways, with various number of Ranks, defining the size of the
Array. Arrays can also be created with the use of various data types such as lists, tuples, etc. The type of
the resultant array is deduced from the type of the elements in the sequences.
Example:
# Python program for
# Creation of Arrays
import numpy as np
Pandas:
Pandas is a Python library used for working with data sets.
It has functions for analyzing, cleaning, exploring, and manipulating data.
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.
Example Program:
import pandas as pd
df = pd.read_csv('data.csv')
print(df.to_string())
Output:
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.