Python Notes Part4
Python Notes Part4
Python provides a vast number of built-in modules that help developers perform various tasks
efficiently.
What is Matplotlib?
Matplotlib is a powerful library used for creating static, animated, and interactive visualizations in
Python. It is mainly used for:
Installation:
Basic Syntax:
# Data
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 50]
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:
2. Bar Chart
Output:
3. Scatter Plot
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [10, 15, 20, 25, 30, 18, 22, 28, 35, 40]
Output:
What is a Class?
A class is a user-defined blueprint that defines attributes (variables) and behaviors (methods) of an
object.
What is a Method?
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
# Creating objects
car1 = Car("Toyota", "Camry")
car2 = Car("Honda", "Civic")
Explanation
Instance Variables
Class Variables
class Student:
school_name = "XYZ School" # Class variable
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
Key Differences
class Person:
def __str__(self):
return f"Person(Name: {self.name}, Age: {self.age})"
p1 = Person("John", 25)
print(p1) # Calls __str__ method
Output:
class Book:
def __init__(self, title, pages):
self.title = title
self.pages = pages
def __len__(self):
return self.pages # Returning number of pages
250
class Laptop:
l1 = Laptop("Dell")
print(l1.brand) # Dell
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:
For example:
5 in binary → 101
10 in binary → 1010
A 4-bit number can represent values from 0 to 15 (since (2^4 = 16) possible values).
1 0001
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
3 → 0011
5 → 0101
0011 (3)
+ 0101 (5)
------------
1000 (8 in decimal)
Answer: 3 + 5 = 8
7 → 0111
6 → 0110
0111 (7)
+ 0110 (6)
------------
1101 (13 in decimal)
Answer: 7 + 6 = 13
9 → 1001
4 → 0100
1001 (9)
+ 0100 (4)
------------
1101 (13 in decimal)
Answer: 9 + 4 = 13
12 → 1100
3 → 0011
1100 (12)
+ 0011 (3)
------------
1111 (15 in decimal)
Answer: 12 + 3 = 15
An 8-bit number can represent values from 0 to 255 (since (2^8 = 256) possible
values).
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
25 → 00011001
37 → 00100101
00011001 (25)
+ 00100101 (37)
---------------
00111110 (62 in decimal)
Answer: 25 + 37 = 62
75 → 01001011
50 → 00110010
01001011 (75)
+ 00110010 (50)
---------------
01111101 (125 in decimal)
Answer: 75 + 50 = 125
200 → 11001000
55 → 00110111
11001000 (200)
+ 00110111 (55)
---------------
11111111 (255 in decimal)
Answer: 200 + 55 = 255