Code With Python
39K subscribers
841 photos
24 videos
22 files
746 links
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
Topic: Python Classes and Objects — Basics of Object-Oriented Programming

Python supports object-oriented programming (OOP), allowing you to model real-world entities using classes and objects.

---

Defining a Class

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")


---

Creating Objects

person1 = Person("Alice", 30)
person1.greet() # Output: Hello, my name is Alice and I am 30 years old.


---

Key Concepts

Class: Blueprint for creating objects.

Object: Instance of a class.

__init__ method: Constructor that initializes object attributes.

self parameter: Refers to the current object instance.

---

Adding Methods

class Circle:
def __init__(self, radius):
self.radius = radius

def area(self):
return 3.1416 * self.radius ** 2

circle = Circle(5)
print(circle.area()) # Output: 78.54


---

Inheritance

• Allows a class to inherit attributes and methods from another class.

class Animal:
def speak(self):
print("Animal speaks")

class Dog(Animal):
def speak(self):
print("Woof!")

dog = Dog()
dog.speak() # Output: Woof!


---

Summary

Classes and objects are core to Python OOP.

• Use class keyword to define classes.

• Initialize attrinitith __init__ method.

• Objects are instances of classes.

• Inheritance enables code reuse and polymorphism.

---

#Python #OOP #Classes #Objects #ProgrammingConcepts
3
Forwarded from PyData Careers
Interview Question

What is the difference between __str__ and __repr__ methods in Python classes, and when would you implementstr
__str__ returns a human-readable string representation of an object (e.g., via print(obj)), making it user-friendly for displayrepr__repr__ aims for a more detailed, unambiguous string that's ideally executable as code (like repr(obj)), useful for debugging—imstr __str__ for end-user outrepr__repr__ for developer tools or str __str__ is defined.

tags: #interview #python #magicmethods #classes

➡️ @DataScienceQ 🤎
Please open Telegram to view this post
VIEW IN TELEGRAM
1