sunum33
sunum33
-WEEK 3
Object-Oriented Programming
CLASESS
While a class is
just a template,
an object is a real
entity derived
from that
template.
OBJECTS
Object-Oriented Programming
Attributes
class MyClass:
def __del__(self):
print("Object deleted")
class Person:
def __eq__(self, other):
return self.name == other.name and self.age == other.age
Constructor (__init__)
In Python, constructor is defined as __init__() method. __init__() is the first method of the class when creating
an object and provides initialization of the object.
2. The __init__() method is not called directly outside the class. In other words, it runs automatically when
the object is created. This method is used to set the initial data (attributes) of the object
Thanks to self, each object can have independent attributes. For example, within a Person class, each object can have a
different name and age value.
Constructor (__init__)
Advantages of Polymorphism:
import math
# Rounding operations
print("Floor (floor):", math.floor(7.8)) # Round 7.8 down
# Numerical operations print("Ceiling (ceil):", math.ceil(7.2)) # Round 7.2 up
print("Square root (sqrt ():", math.sqrt25)) # Square root of 25
print("Power (pow):", math.pow(2, 3)) # 2 to the power of 3
print("Factorial (factorial):", math.factorial(5)) # Factorial of 5 (5!)
# Trigonometric functions
angle_degrees = 60 # Constants
angle_radians = math.radians(angle_degrees) # Convert degrees to radians print("Pi:", math.pi) # Pi constant
print(f"{angle_degrees} degrees in radians:", angle_radians)
print("Sine (sin):", math.sin(angle_radians)) # Sine of 60 degrees
print("Cosine (cos):", math.cos(angle_radians)) # Cosine of 60 degrees
o Random (generating random numbers)
The random module is used to generate random numbers or make random selections.
import random
# Adding/subtracting days
future_date = now + datetime.timedelta(days=10) # Add 10 days
past_date = now - datetime.timedelta(days=10) # Subtract 10 days
print("Future date:", future_date)print("Past date:", past_date)
IMPORTING BUILT IN AND EXTERNAL MODULES
OUTPUT
BASICS OF INSTALLING PACKAGES USING PIP.
PIP is a package manager for Python packages, or modules if you like.
Pip is a tool that is installed automatically if you've installed python 3.4 or later versions,
if you do not have PIP installed, you can download and install it.
Example:
• Upgrade pip pip install --upgrade pandas
To ensure you have the latest version:
• Uninstall a Package
pip install --upgrade pip To remove a package:
pip uninstall <package_name>
• Install a Package
• View Installed Packages
To install a package, use: To list all installed packages:
pip install <package_name> pip list