Single Inheritance in Python | Python Tutorial - Day #78

preview_player
Показать описание

python, C, C++, Java, JavaScript and Other Cheetsheets [++]:

►Learn in One Video[++]:

►Complete course [playlist]:

Follow Me On Social Media
Comment "#HarryBhai" if you read this 😉😉
Рекомендации по теме
Комментарии
Автор

The Exercise is :
class animal:

def __init__(self, name, color):
self.name = name
self.color = color

def details(self):
print(f"i have a {self.name} which is {self.color} in color")


class cat(animal):

def __init__(self, name, age):
animal.__init__(self, name, color="gray")
self.age = age

def details(self):
print(
f"i have a {self.name} which is {self.color} in color whoes age is {self.age}"
)


ani = animal('buffalow', 'black and white')
ani.details()

ani1 = cat('cat', 3)
ani1.details()

MuhammadIhsan-guks
Автор

class Animal:
def __init__(self, name, species):
self.name = name
self.species = species

def make_sound(self):
print("Sound made by the animal")


class Cat(Animal):
def __init__(self, name, catage, species):
super().__init__(name, species)
self.catage = catage

def make_sound(self):
print("meow")

def hi(self):
print("hi")

arnavanand
Автор

Sir after 100 days of python please make a playlist on data analytics

dhairyamehta
Автор

I have watched all of the first 77 lectures so far. And, I can tell you didn't waste my time in even one of them.

HardLessonsOfLife
Автор

quick quiz:
class Animal:
def __init__(self, name, color):
self.name=name
self.color=color
def __str__(self):
return f"Name is {self.name} and color is {self.color}."

class cat:
def __init__(self, name, age):
self.age=age
Animal.__init__(self, name, color="Black and white")
def __str__(self):
return f"Name is {self.name}, color is {self.color}, and age is {self.age}."

e1=Animal("Cow", "Ghost-white")
print(e1)

e2=cat("cat", 9)
print(e2)

Sujal_don
Автор

class Animal:
def __init__(self, name):
self.name = name
@property
def features(self):
print(f"{self.name} :-\n")
class Cat(Animal):
def __init__(self, name):
super().__init__(name)
@property
def sound(self):
print("Sound : meow\n")
@property
def family(self):
print("Family : Tiger\n")
@property
def type(self):
print("Type : Domestic")

a = Cat("cat")
a.features
a.sound
a.family
a.type

Syed-jh
Автор

#Day78 done. Answer to the quick quiz:

class Animal:
def __init__(self, name, species):
self.name = name
self.species = species

def make_sound(self):
print("Sound made by the animal")

class Dog(Animal):
def __init__(self, name, breed):
Animal.__init__(self, name, species="Dog")
self.breed = breed

def make_sound(self):
print("Bark!")

d = Dog("Dog", "Doggerman")
d.make_sound()

a = Animal("Dog", "Dog")
a.make_sound()

class Cat:
def __init__(self, name, breed):
Animal.__init__(self, name, species = "Robo Cat")
self.breed = breed

def make_sound(self):
print("Meow Meow")
def eat(self):
print("Likes to eat Doracake")
def m_sound(self):
print("Doraemon talks like a human")

c = Cat("Cat", "Doraemon")
c.make_sound()
c.eat()
c.m_sound()

mariamhasan
Автор

class Animal:
def __init__(self, name, species):
self.name = name
self.species = species

def make_sound(self):
print("Sound made by the animal")

class Cat(Animal):
def __init__(self, name, species, fur_colour, eye_colour):
self.name = name
self.species = species
self.fur_colour = fur_colour
self.eye_colour = eye_colour

def describe(self):
print(f'{self.name} is from {self.species} species and have a {self.fur_colour} fur with {self.eye_colour} eyes.')

def make_sound(self):
print("Meowww!")

def pet(self):
print(f'{self.name} does not like petting... \n\n{self.name}:

def fetch(self):
print(f'{self.name} does not respond!')

A = Cat('Bell', 'Sadak-Chhaap', 'Brown', 'Yellow' )
Cat.describe(A)
Cat.make_sound(A)
Cat.pet(A)
Cat.fetch(A)

Iamaizadkhan
Автор

Sir after 100 days of python.. Please make a playlist of DSA in C++🙏🙏🙏🙏

nikhilkalloli
Автор

9.11.2024 day 78 is done.
quick quiz answer:
class Animal:
def __init__(self, name, species):
self.name = name
self.species = species

def make_sound(self):
print("Sound made by the animal")

class Cat(Animal):
def __init__(self, name, breed):
Animal.__init__(self, name, species="Cat")
self.breed = breed

def make_sound(self):
print("Meow!")

def purr(self):
print("Purr...")

# Creating instances and testing the methods
c = Cat("Whiskers", "Siamese")
c.make_sound() # Outputs: Meow!
c.purr() # Outputs: Purr...

a = Animal("Generic Animal", "Unknown")
a.make_sound() # Outputs: Sound made by the animal

TasinIslam-mcdk
Автор

Present sir on #Day78. Can't believe the amazing experience I had while learning python! Hats off!

arpankarmakar
Автор

lass animal:
def __init__(self, name, gender):
self.name=name
self.gender=gender

class cat(animal):
def __init__(self, name, gender, breed):
animal.__init__(self, name, gender)
self.breed=breed

def show(self):
print(f'{self.name} and {self.gender} and {self.breed}')


a=cat('dog', 'male', 'fdfdf')
a.show()

rajdeepsahu
Автор

# Quick Quiz: Implement a Cat class by using the Animal class. Add some methods specific to cats.

class Animal:
def __init__(self, name, species):
self.name = name
self.species = species

def make_sound(self):
print("Sound made by animal")

class Cat(Animal):
def __init__(self, name, breed="Cat"):
Animal.__init__(self, name, species="Cat")
self.breed = breed

def meow(self) -> None:
print("Meow")

def wiggle_tail(self) -> None:
print("*wiggles tail*")

def angry(self) -> None:
print("MEOWW!!!")

def show_details(self) -> None:
print(f"The species is {self.species} and the breed is {self.breed}. The name of the {self.species} is {self.name}.")

def main() -> None:
cat1 = Cat("Catto", "Persian")
cat2 = Cat("Pikachu", "Siberian")
cat1.meow()
cat2.wiggle_tail()
cat1.angry()
cat2.angry()
cat1.show_details()
cat2.show_details()

if ___name___ == '__main__':
main()

bravcoder
Автор

# Quick Quiz:

class Animal:
def __init__(self, name, species):
self.name = name
self.species = species

def make_sound(self):
print("Sound made by the animal")


class Cat(Animal):
def __init__(self, color, eye):
self.color = color
self.eye = eye
super().__init__("Cat", "Felis catus")

def make_sound(self):
print("Meow meow!")

def species_name(self):
print(self.species)

def skin_color(self):
print(self.color)

def eyes_color(self):
print(self.eye)


cat = Cat("Black", "Green")
cat.make_sound()
cat.species_name()
cat.skin_color()
cat.eyes_color()

arpankarmakar
Автор

I made a little bit of changes in the code to make it simpler:

# Parent class
class Animal:
def __init__(self, name, species):
self.name = name
self.species = species

def make_sound(self):
print("Sound made by an animal")

# Child class inheriting from Animal
class Dog(Animal):
def __init__(self, breed):
super().__init__(name="Tom", species='Dog') # Call the parent class constructor
self.breed = breed # ^ and ^ are Default values

def wag_tail(self):
print("Dog is wagging its tail")

# Creating an instance of Dog
d = Dog("Golden Retriever")
print(d.name) # Accessing attribute from the parent class
print(d.species) # Accessing attribute from the parent class
print(d.breed) # Accessing attribute from the child class
d.make_sound() # Calling method from the parent class
d.wag_tail() # Calling method from the child class
print("\n")



#CAR SPECIFICATION
class Car:
def __init__(self, name, speed):
self.name = name
self.speed = speed


def model(self):
print("New Model 2024!")

class Specs(Car):
def __init__(self, engine, color):
super().__init__(name="BMW", speed = "300 MPH")
self.engine = engine
self.color = color

e = Specs("Turbo VTEC 90", "Black")
print(e.name)
print(e.color)
print(e.engine)
print(e.speed)

khuzaimaamir
Автор

class Animal:
def __init__(self, name, species):
self.name = name
self.species = species

def get_color(self):
print("Color of animal is not known")

class Cat(Animal):
def __init__(self, name, color):
Animal.__init__(self, name, species="cat")
self.color = color

def get_color(self):
print("White")

# Creating an instance of Cat
d = Cat("cat", "White")
d.get_color()

# Creating an instance of Animal
a = Animal("Dog", "Dog")
a.get_color()

divyanshisingh
Автор

#Quick Quiz:
class animal:
def __init__(self, name, species):
self.name = name
self.species = species
def make_sound(self):
print("sound made by animal")

class cat(animal):
def __init__(self, breed, colour, name):
self.breed = breed
self.colour = colour
self.name=name

def specification(self):
print(f"{self.name} is a {self.colour} in colours. It is a {self.breed} cat.")

a=animal("Tommy", "cat")
a.make_sound()
b=cat("persian", " blue", "Tommy")
b.specification()

pubpubji
Автор

present sir from Pakistan tharparkar more love and courage of you agha barhoo sir ham ap ka sath ha

Keepitshort
Автор

class Animal:
def __init__(self, name, color):
self.name = name
self.color = color

def make_sound(self):
print(f" I have a {self.name} and color is {self.color}")

class Cat(Animal):
def __init__(self, name, color, breed):
Animal.__init__(self, name, color = "White")
self.breed = breed

def make_sound(self):
print(f" I have a {self.name} and color is {self.color} and breed is {self.breed}")

a = Animal("Dog", "Black")
a.make_sound()

c = Cat("Cat", "Gray", "Persian")
c.make_sound()

AshishBakoliya-kg
Автор

Can't express my Thanks to you sir,
You really changed my whole life,
Thanks Harry Bhai!

jaideepawari