Posts

Showing posts with the label Init Method

Featured Post

Python Set Operations Explained: From Theory to Real-Time Applications

Image
A  set  in Python is an unordered collection of unique elements. It is useful when storing distinct values and performing operations like union, intersection, or difference. Real-Time Example: Removing Duplicate Customer Emails in a Marketing Campaign Imagine you are working on an email marketing campaign for your company. You have a list of customer emails, but some are duplicated. Using a set , you can remove duplicates efficiently before sending emails. Code Example: # List of customer emails (some duplicates) customer_emails = [ "alice@example.com" , "bob@example.com" , "charlie@example.com" , "alice@example.com" , "david@example.com" , "bob@example.com" ] # Convert list to a set to remove duplicates unique_emails = set (customer_emails) # Convert back to a list (if needed) unique_email_list = list (unique_emails) # Print the unique emails print ( "Unique customer emails:" , unique_email_list) Ou...

How to Initialize Class Variables? The Purpose of Init Method in Python

Image
The init method is like a constructor in Java. But the usage is different in Python.  Here's the best example to write Class in Python using init method.  The init executes by default and it will initialize all the variable. The dog_1 is an instantiation of dog class. When you print the dog_1, it prints as "I am in the init method", which is from the "init" method. The self means this class. It is mandatory. Not only self, but you can also give any name in that place. Instead of "self" you can give as "This_is_Python_Self". It also will work. How to use init method class dog:    def __init__(self):          print("I am in init method") Now, instantiating the class dog_1=dog() print(dog_1) The output from the above display is as below: I am in init method <__main__.dog object at 0x7fe7b5875be0> Related posts The best Python Interview Questions