Aditya Tiwari Profile picture
Mar 2 13 tweets 4 min read
Inheritance in Python

It is a way of creating a new class by deriving it from an existing class.

With inheritance, the new class inherits all properties and behaviors of the existing class, allowing to reuse code and create classes that build on each other

A thread🧵
#Python
In Python, we can define a class that inherits from another class using the syntax class DerivedClass(BaseClass):

This means that the DerivedClass is inheriting from the BaseClass.

1/10
Here is an example of how to define a class that inherits from another class:

In this example, we have a Animal class with a speak method that prints "I am an animal". We also have a Dog class that inherits from Animal and adds a bark method that prints "Woof!".

2/10
Now, let's create an object of each class and call their methods:

As we can see, the Dog object inherited the speak method from the Animal class, and we were also able to call the bark method specific to the Dog class.

3/10
Inheritance allows us to create more specialized classes that inherit attributes and methods from a base class. This is useful when we want to reuse code and avoid duplication.

4/10
For example, we create a Cat class that is similar to the Dog class but has a different sound attribute and method. Instead of defining the speak method from scratch, I can inherit it from Animal class & only define the sound attribute and method specific to the Cat class

5/10
In above example, we have a Cat class that inherits from Animal. We define a sound attribute that is specific to the Cat class and a meow method that prints the sound attribute.

Now, let's create a Cat object and call its methods:

6/10
As we can see, the Cat object inherited the speak method from the Animal class and we were also able to call the meow method specific to the Cat class.

7/10
Inheritance is a powerful tool in object-oriented programming that allows us to create more specialized classes based on existing ones.

By reusing code and avoiding duplication, we can create more efficient and maintainable code.

But..
It's important to use inheritance judiciously and avoid deep inheritance hierarchies that can become difficult to understand and maintain. In general, it's a good idea to limit inheritance to a few levels and favor composition over inheritance when possible.
That's it for this thread on objects and classes in #Python I hope this was helpful!

Don't forget to follow me for more Python content:
@thegeekyb0y
Thanks for sticking with me through this Python twitter thread! If you're feeling extra generous and want to support my work, consider becoming a member and getting some sweet perks.

thegeekyboy.gumroad.com/l/memberships-…

Either way, thanks for being part of my community!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Aditya Tiwari

Aditya Tiwari Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @TheGeekyB0y

Feb 28
Objects and Classes in Python🐍

Objects are instances of a class, and classes are the blueprint for creating objects.

In this thread🧵, we'll explore the concepts of objects and classes in depth, along with some examples.

#Python Image
First, let's define a class in Python.

To create a class, we use the class keyword followed by the name of the class. Here's a simple example:

In this example, we've created a class called MyClass with nothing inside it. Let's add some attributes to the class.

1/10 Image
Attributes are variables that belong to a class. They are defined in the __init__ method of the class. Here's an example:

In this example, we've defined a class : Person with two attributes: name and age. The __init__ method is called when a new instance of the class is created. Image
Read 14 tweets
Feb 22
Strings and String Manipulation Methods in Python🐍

Strings in Python are a sequence of characters enclosed in single, double, or triple quotes.

Strings are immutable, which means you cannot change their values after they are created.

Detailed Thread👀🧵

#python #programming
Concatenation of Strings : You can concatenate two or more strings using the "+" operator.

1/16
Replication of Strings : You can also repeat a string multiple times using the "*" operator.

2/16
Read 18 tweets
Feb 21
Loops in Python ⚡🐍

Loops are an essential programming concept that allow you to execute a block of code repeatedly. There are two main types of loops in Python: for loops and while loops.

A thread 🧵#python #DataScience
For Loop :

A for loop allows you to iterate over a sequence of values, such as a list, tuple, or string.

Here's an example that prints each element of a lists

1/10
You can also use the range() function to iterate over a sequence of numbers. Here's an example that prints the numbers 0 through 9:

2/10
Read 17 tweets
Feb 20
F-strings in Python : The ultimate usage tips ⚡

F-strings in Python are a concise and powerful way to format strings. You can include any valid Python expression inside the curly braces in an f-string.

Thread 🧵

#Python #programming Image
To use an f-string, just prefix your string with the letter "f".

Here's an example that uses an f-string to format a variable:

#Python
1/16 Image
You can use f-strings to format numbers as well. Here's an example that formats a floating-point number with two decimal places:

2/16 #Python Image
Read 18 tweets
Jan 27
Internet is full of resources. Its good, but sometimes, people get stuck on which is best for them.

I have compiled a massive list of resources for PYTHON lang you can get started with.

#Python

A thread 🧵
Official SitePython.org
Documentation : docs.python.org/3/
Quick Start Guide : python.org/about/gettings…

#Python

1/9
Basic Roadmap (GFG)
geeksforgeeks.org/best-way-to-st…

Python Notes (goalkicker)
books.goalkicker.com/PythonBook/

#Python

2/9
Read 11 tweets
Jan 7
Optimize your Python code using these 6 ways.

A thread 🧵
1. Use the 'timeit' module to measure the performance of your code.

This will give you a baseline to work from and help you identify areas of your code that are particularly slow.
2. Use the 'cProfile' module to profile your code and identify bottlenecks.

This will give you a detailed breakdown of where your code is spending most of its time, so you can focus your optimization efforts on the most impactful areas.
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(