0% found this document useful (0 votes)
4 views15 pages

Python Notes Part4

The document provides training notes on Python, covering built-in modules, particularly Matplotlib for data visualization, and the creation of classes and methods. It explains various plotting techniques, including line plots, bar charts, and scatter plots, along with the fundamentals of object-oriented programming. Additionally, it discusses binary representation and addition, illustrating both 4-bit and 8-bit examples.

Uploaded by

chirurxc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Python Notes Part4

The document provides training notes on Python, covering built-in modules, particularly Matplotlib for data visualization, and the creation of classes and methods. It explains various plotting techniques, including line plots, bar charts, and scatter plots, along with the fundamentals of object-oriented programming. Additionally, it discusses binary representation and addition, illustrating both 4-bit and 8-bit examples.

Uploaded by

chirurxc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

PYTHON TRAINING NOTES DREAM A DREAM

Introduction to Built-in Modules

Python provides a vast number of built-in modules that help developers perform various tasks
efficiently.

Matplotlib: Data Visualization

What is Matplotlib?

Matplotlib is a powerful library used for creating static, animated, and interactive visualizations in
Python. It is mainly used for:

 Creating graphs and plots


 Customizing plots (changing labels, colors, line styles)
 Handling large data for visual representation

Installation:

pip install matplotlib

Basic Syntax:

import matplotlib.pyplot as plt # Import the module

# Data
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 50]

# Plot the data


plt.plot(x, y)

# Show the plot


plt.show()

PREPARED BY HARISH YADAV pg. 1


PYTHON TRAINING NOTES DREAM A DREAM

Basic Types of Plots in Matplotlib

1. Line Plot (Default Plot)

A line plot is useful for showing trends over time.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 50]

plt.plot(x, y, color='red', marker='o', linestyle='--') # Red line with circle markers and dashed line
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.title("Line Plot Example")
plt.grid(True) # Adding grid lines
plt.show()

Output:

PREPARED BY HARISH YADAV pg. 2


PYTHON TRAINING NOTES DREAM A DREAM

2. Bar Chart

A bar chart is used for comparing different categories.

import matplotlib.pyplot as plt

categories = ["A", "B", "C", "D"]


values = [10, 20, 15, 30]

plt.bar(categories, values, color=['blue', 'green', 'red', 'purple'])


plt.xlabel("Categories")
plt.ylabel("Values")
plt.title("Bar Chart Example")
plt.show()

Output:

PREPARED BY HARISH YADAV pg. 3


PYTHON TRAINING NOTES DREAM A DREAM

3. Scatter Plot

A scatter plot shows the relationship between two numerical variables.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [10, 15, 20, 25, 30, 18, 22, 28, 35, 40]

plt.scatter(x, y, color='green', marker='*')


plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.title("Scatter Plot Example")
plt.show()

Output:

PREPARED BY HARISH YADAV pg. 4


PYTHON TRAINING NOTES DREAM A DREAM

Understanding Classes and Methods

What is a Class?

A class is a user-defined blueprint that defines attributes (variables) and behaviors (methods) of an
object.

 A class groups related data and functions together.


 It allows reusability and modular programming.

What is a Method?

A method is a function inside a class that operates on objects.

 It performs operations like modifying object attributes or displaying information.

Why Use Classes and Methods?

 Organized Code - Keeps data and behavior together


 Reusability - Code can be reused in multiple programs
 Encapsulation - Data is protected from unintended modification

2 Creating Classes in Python

Defining a Class

To create a class, use the class keyword followed by the class name.

class Car:
def __init__(self, brand, model): # Constructor
self.brand = brand
self.model = model

PREPARED BY HARISH YADAV pg. 5


PYTHON TRAINING NOTES DREAM A DREAM

def display_info(self): # Method


print(f"Car Brand: {self.brand}, Model: {self.model}")

# Creating objects
car1 = Car("Toyota", "Camry")
car2 = Car("Honda", "Civic")

# Using the method


car1.display_info()
car2.display_info()

Explanation

 __init__ is a constructor method that initializes object attributes.


 self.brand and self.model store the values assigned during object creation.
 display_info() is a method that prints the object’s details.

3 Instance and Class Variables

Instance Variables

 Belong to a specific object.


 Defined using self.attribute_name.

Class Variables

 Shared among all instances of the class.


 Defined outside the __init__ method.

class Student:
school_name = "XYZ School" # Class variable

def __init__(self, name, grade):


self.name = name # Instance variable
self.grade = grade # Instance variable

PREPARED BY HARISH YADAV pg. 6


PYTHON TRAINING NOTES DREAM A DREAM

def show(self):
print(f"Student: {self.name}, Grade: {self.grade}, School: {Student.school_name}")

# Creating objects
s1 = Student("Alice", "A")
s2 = Student("Bob", "B")

s1.show()
s2.show()

Output

Student: Alice, Grade: A, School: XYZ School


Student: Bob, Grade: B, School: XYZ School

Key Differences

Instance Variables Class Variables

Unique to each object Shared among all objects

Defined inside __init__ Defined outside __init__

Accessed with self.attribute_name Accessed with ClassName.attribute_name

4 Using Built-in Functions with Classes

Python provides built-in functions that work with classes.

__str__ Method (String Representation of Objects)

This method returns a string representation of an object.

class Person:

PREPARED BY HARISH YADAV pg. 7


PYTHON TRAINING NOTES DREAM A DREAM

def __init__(self, name, age):


self.name = name
self.age = age

def __str__(self):
return f"Person(Name: {self.name}, Age: {self.age})"

p1 = Person("John", 25)
print(p1) # Calls __str__ method
Output:

Person(Name: John, Age: 25)

len() Function (Counting Attributes)

You can define __len__ to return the number of attributes in an object.

class Book:
def __init__(self, title, pages):
self.title = title
self.pages = pages

def __len__(self):
return self.pages # Returning number of pages

book1 = Book("Python Basics", 250)


print(len(book1)) # Calls __len__ method
Output:

250

del Keyword (Deleting Objects)

The del keyword removes an object from memory.

class Laptop:

PREPARED BY HARISH YADAV pg. 8


PYTHON TRAINING NOTES DREAM A DREAM

def __init__(self, brand):


self.brand = brand

l1 = Laptop("Dell")
print(l1.brand) # Dell

del l1 # Deletes the object


Trying to access l1.brand after deletion will give an error.

Bit Values

Understanding Numbers and Bit Values What are Bit Values? A bit (binary digit) is the smallest
unit of data in computing. It can have only two possible values:

0 (Off / Low), 1 (On / High)

Each number we use in daily life is stored as a binary number in computers.

For example:

5 in binary → 101

10 in binary → 1010

255 in binary → 11111111

4-Bit Binary Representation & Addition

1 Understanding 4-Bit Representation

A 4-bit number can represent values from 0 to 15 (since (2^4 = 16) possible values).

Decimal Binary (4-bit Representation)


0 0000

1 0001

PREPARED BY HARISH YADAV pg. 9


PYTHON TRAINING NOTES DREAM A DREAM

Decimal Binary (4-bit Representation)


2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

10 1010

11 1011

12 1100

13 1101

14 1110

15 1111

2 Binary Addition Rules


Binary addition follows these simple rules:

Binary Digits Sum Carry


0+0 0 0
0+1 1 0
1+0 1 0
1+1 10 (Carry 1)
1+1+1 11 (Carry 1)

PREPARED BY HARISH YADAV pg. 10


PYTHON TRAINING NOTES DREAM A DREAM

3 Examples of 4-Bit Addition

Example 1: Adding 3 and 5

Step 1: Convert to Binary

3 → 0011
5 → 0101

Step 2: Perform Bitwise Addition

0011 (3)
+ 0101 (5)
------------
1000 (8 in decimal)
Answer: 3 + 5 = 8

Example 2: Adding 7 and 6

Step 1: Convert to Binary

7 → 0111
6 → 0110

Step 2: Perform Bitwise Addition

0111 (7)
+ 0110 (6)
------------
1101 (13 in decimal)
Answer: 7 + 6 = 13

PREPARED BY HARISH YADAV pg. 11


PYTHON TRAINING NOTES DREAM A DREAM

Example 3: Adding 9 and 4

Step 1: Convert to Binary

9 → 1001
4 → 0100

Step 2: Perform Bitwise Addition

1001 (9)
+ 0100 (4)
------------
1101 (13 in decimal)
Answer: 9 + 4 = 13

Example 4: Adding 12 and 3

Step 1: Convert to Binary

12 → 1100
3 → 0011

Step 2: Perform Bitwise Addition

1100 (12)
+ 0011 (3)
------------
1111 (15 in decimal)
Answer: 12 + 3 = 15

PREPARED BY HARISH YADAV pg. 12


PYTHON TRAINING NOTES DREAM A DREAM

8-Bit Binary Representation & Addition

Understanding 8-Bit Representation

An 8-bit number can represent values from 0 to 255 (since (2^8 = 256) possible
values).

Decimal Binary (8-bit Representation)


0 00000000

1 00000001

2 00000010

3 00000011

4 00000100

5 00000101

6 00000110

7 00000111

8 00001000

9 00001001

10 00001010

15 00001111

20 00010100

50 00110010

100 01100100

127 01111111

128 10000000

200 11001000

255 11111111

PREPARED BY HARISH YADAV pg. 13


PYTHON TRAINING NOTES DREAM A DREAM

3 Examples of 8-Bit Addition

Example 1: Adding 25 and 37

Step 1: Convert to Binary

25 → 00011001
37 → 00100101

Step 2: Perform Bitwise Addition

00011001 (25)
+ 00100101 (37)
---------------
00111110 (62 in decimal)
Answer: 25 + 37 = 62

Example 2: Adding 75 and 50

Step 1: Convert to Binary

75 → 01001011
50 → 00110010

Step 2: Perform Bitwise Addition

01001011 (75)
+ 00110010 (50)
---------------
01111101 (125 in decimal)
Answer: 75 + 50 = 125

PREPARED BY HARISH YADAV pg. 14


PYTHON TRAINING NOTES DREAM A DREAM

Example 3: Adding 200 and 55

Step 1: Convert to Binary

200 → 11001000
55 → 00110111

Step 2: Perform Bitwise Addition

11001000 (200)
+ 00110111 (55)
---------------
11111111 (255 in decimal)
Answer: 200 + 55 = 255

PREPARED BY HARISH YADAV pg. 15

You might also like