Skip to main content

Posts

Showing posts with the label methods

Super Keyword in Python

Learning Sections          show super Keyword in Python The super keyword in Python is used to call a method from the parent class. It is commonly used in the context of inheritance to invoke the superclass's methods. Using super with __init__ Method When a subclass overrides the __init__ method, the super function can be used to call the __init__ method of the parent class. class Animal : def __init__ ( self , name ): self . name = name class Dog ( Animal ): def __init__ ( self , name , breed ): super (). __init__ ( name ) self . breed = breed # Create an instance of Dog dog = Dog ( 'Buddy' , 'Golden Retriever' ) print ( dog . name ) # Output: Buddy print ( dog . breed ) # Output: Golden Retriever Using super with Other Methods In addition to __init__ , the super function can be used to call any method from the parent class. class Shape : def are...

dir, __dict__ and help method in Python

  Learning Sections          show dir, __dict__, and help Method in Python Python provides several built-in functions and attributes that are useful for introspection and understanding objects and their attributes. Three such tools are dir() , __dict__ , and help() . 1. dir() The dir() function returns a list of the attributes and methods of an object. It can be used on any object to see its properties. # Using dir() on a list example_list = [ 1 , 2 , 3 ] print ( dir ( example_list )) Output: ['__add__', '__class__', '__contains__', ... , 'append', 'clear', 'copy', ...] 2. __dict__ The __dict__ attribute is a dictionary representation of an object's attributes. It shows the object's attributes and their values. # Define a simple class class Person : def __init__ ( self , name , age ): self . name = name self . age = age # Create an inst...

Class Methods in Python

  Learning Sections          show @classmethod In Python, class methods are methods that are bound to the class itself rather than to instances of the class. They can be called using the class name or an instance of the class. Class methods are defined using the @classmethod decorator. Example: class Person : # Class variable total_people = 0 # Class method to initialize total_people @classmethod def increment_total_people ( cls ): cls . total_people += 1 # Call class method using class name Person . increment_total_people () # Access class variable using class name print ( Person . total_people ) # Output: 1 # Create instance of the Person class person1 = Person () # Call class method using instance person1 . increment_total_people () # Access class variable using instance print ( person1 . total_people ) # Output: 2 Class methods are often used for factory methods that...

seek(), tell() and other functions

  Learning Sections          show seek(), tell() and Other Functions in Python Python provides several functions to manipulate the file pointer while reading or writing files. Here are some commonly used functions: 1. seek() The seek() method sets the file's current position at the offset. The position is calculated from the beginning of the file, unless the optional whence parameter is provided. The whence parameter can take the following values: 0 : default, the beginning of the file 1 : current file position 2 : end of the file # Open the file in read mode with open ( 'example.txt' , 'r' ) as file : # Move the file pointer to the beginning of the file file . seek ( 0 ) # Read the first 10 characters content = file . read ( 10 ) print ( content ) 2. tell() The tell() method returns the current position of the file pointer within the file...

read(), readlines() and other methods in Python

Learning Sections          show read(), readlines() and Other Methods in Python Python provides several methods to read from and manipulate files. Here are some common methods: 1. read() The read() method reads the entire content of a file and returns it as a string. # Open the file in read mode with open ( 'example.txt' , 'r' ) as file : # Read the entire content of the file content = file . read () print ( content ) 2. readlines() The readlines() method reads all the lines of a file and returns a list where each element is a line in the file. # Open the file in read mode with open ( 'example.txt' , 'r' ) as file : # Read all lines of the file lines = file . readlines () for line in lines : print ( line . strip ()) # strip() removes the newline character 3. readline() The readline() method reads one line from the file and returns it as a...

Set Methods in Python

  Learning Sections          show Set Methods in Python A set is an unordered collection of unique elements. Python provides several built-in methods for manipulating sets: 1. add() Adds an element to the set if it is not already present. # Define a set fruits = { 'apple' , 'banana' , 'cherry' } # Add an element to the set fruits . add ( 'orange' ) print ( fruits ) # Output: {'banana', 'cherry', 'orange', 'apple'} 2. remove() Removes the specified element from the set. Raises a KeyError if the element is not present. # Remove an element from the set fruits . remove ( 'banana' ) print ( fruits ) # Output: {'apple', 'cherry', 'orange'} 3. discard() Removes the specified element from the set if it is present. Does not raise an error if the element is not present. # Discard an element from the set fruits...

List Methods in Python

  Learning Sections          show List Methods in Python Python lists come with a variety of built-in methods that allow you to perform common operations. Here are some of the most frequently used list methods: 1. append() Adds an item to the end of the list. # Example of append() my_list = [ 1 , 2 , 3 ] my_list . append ( 4 ) print ( my_list ) # Output: [1, 2, 3, 4] 2. extend() Extends the list by appending all the items from another list. # Example of extend() list1 = [ 1 , 2 , 3 ] list2 = [ 4 , 5 ] list1 . extend ( list2 ) print ( list1 ) # Output: [1, 2, 3, 4, 5] 3. insert() Inserts an item at a specified position. # Example of insert() my_list = [ 1 , 2 , 4 ] my_list . insert ( 2 , 3 ) print ( my_list ) # Output: [1, 2, 3, 4] 4. remove() Removes the first occurrence of a ...

String Methods in Python

Learning Sections      show Python String Methods 1. `upper()` and `lower()` s = "Hello, World!" print ( s . upper ()) # Output: HELLO, WORLD! print ( s . lower ()) # Output: hello, world! 2. `strip()`, `lstrip()`, and `rstrip()` s = " Hello, World! " print ( s . strip ()) # Output: "Hello, World!" print ( s . lstrip ()) # Output: "Hello, World! " print ( s . rstrip ()) # Output: " Hello, World!" 3. `split()` and `join()` s = "Hello, World!" words = s . split ( ", " ) print ( words ) # Output: ['Hello', 'World!'] print ( " - " . join ( words )) # Output: Hello - World! 4. `find()`, `rfind()`, `index()`, and `rindex()` s = "Hello, World!" print ( s . find ( "o" )) # Output: 4 print ( s . rfind ( "o" )) # Output: 8 print ( s . index ( "World" )) # Output: 7 5. `replace()` s = "Hello, Wor...