Repository navigation
Python Cheat Sheet
Python virtual environments are self contained environments for a project. This is done for portability.
This command makes the environment, replace venv_name with the name of your environment
python3 -m venv venv_name
This command will activate the venv
source venv_name/bin/activate
This command will install packages needed in the venv
pip install package_name
This command will deactivate the environment
deactivate
In Python, classes and objects are fundamental concepts that are used to implement object-oriented programming (OOP). Object-oriented programming is a programming paradigm that organizes code into reusable and modular units called classes, which define the structure and behavior of objects. Objects are instances of classes, and they represent specific instances or entities in your program.
Classes:
A class is a blueprint or template for creating objects. It defines the attributes (data) and methods (functions) that the objects of the class will have. Classes are defined using the class keyword followed by the class name, and they are typically named using CamelCase conventions. Example of a simple class definition:
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self):
return f"{self.name} says Woof!" In the above example, we defined a Dog class with attributes name and breed, as well as a method bark.
Objects:
Objects are instances of classes. They are created from a class and inherit the attributes and methods defined in that class. You can create multiple objects from the same class, each with its own set of attribute values. Example of creating objects from the Dog class:
dog1 = Dog("Buddy", "Golden Retriever")
dog2 = Dog("Max", "German Shepherd")
print(dog1.name) # Output: Buddy
print(dog2.breed) # Output: German Shepherd
print(dog1.bark()) # Output: Buddy says Woof!In this example, dog1 and dog2 are two different objects of the Dog class.
Constructor (init):
The init method is a special method (also called a constructor) that is called when an object of the class is created. It is used to initialize the attributes of the object. The self parameter refers to the instance of the object being created and is used to access and set object attributes. Methods:
Methods are functions defined within a class, and they operate on the attributes of the class. They can be called on objects to perform specific actions. In the Dog class example, bark is a method that returns a string representing the dog's bark.
Python supports various OOP principles such as encapsulation, inheritance, and polymorphism, which allow you to create complex and modular code by organizing data and behavior into classes and objects.
Here's a brief overview of some important OOP concepts in Python:
Inheritance: It allows a class to inherit attributes and methods from another class. This promotes code reusability.
Encapsulation: It refers to restricting access to certain parts of an object and only exposing the necessary interfaces. This is often achieved using private and public attributes and methods.
Polymorphism: It allows objects of different classes to be treated as objects of a common superclass, enabling more flexible and abstract code.
Abstraction: It involves simplifying complex reality by modeling classes based on the essential properties and behaviors, while hiding unnecessary details.
For more see here: https://www.geeksforgeeks.org/python-classes-and-objects/