0% found this document useful (0 votes)
11 views10 pages

Built-In Functions in Python

The document outlines built-in functions and class attributes in Python, highlighting their definitions, purposes, and advantages. It also covers inheritance in Python, detailing its types, syntax, and key terms, along with examples of single, multilevel, multiple, hierarchical, and hybrid inheritance. Additionally, it discusses method overriding and the use of the super() function for calling parent class methods.

Uploaded by

shruthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Built-In Functions in Python

The document outlines built-in functions and class attributes in Python, highlighting their definitions, purposes, and advantages. It also covers inheritance in Python, detailing its types, syntax, and key terms, along with examples of single, multilevel, multiple, hierarchical, and hybrid inheritance. Additionally, it discusses method overriding and the use of the super() function for calling parent class methods.

Uploaded by

shruthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BUILT-IN FUNCTIONS IN PYTHON

Built-in functions are functions that are already available in Python.​


You can use them without importing any module.
Definition
Built-in functions are functions that are already defined in Python and perform commonly
required operations such as input, output, type conversion, mathematical calculations, and object
handling.
Characteristics
●​ No need to import any module

●​ Always available in Python


●​ Increase code efficiency and readability
●​ Reduce program length
●​ Written in optimized C code (fast execution)
Purpose of Built-in Functions
●​ To simplify programming tasks

●​ To avoid writing repetitive code


●​ To perform common operations easily
●​ To provide standard functionality across programs
Categories of Built-in Functions
1.​ Input/Output functions – input(), print()

2.​ Type conversion functions – int(), float(), str()

3.​ Mathematical functions – abs(), round(), pow()

4.​ Sequence functions – len(), max(), min(), sum()

5.​ Object & OOP functions – type(), isinstance(), hasattr()

6.​ Iteration functions – range(), enumerate()

Advantages
●​ Save development time

●​ Improve program reliability


●​ Easy to understand and use
●​ Reduce logical errors
Built-in class attributes are special attributes automatically created by Python for every
class.​
They store metadata (information) about the class itself, not about individual objects.
Definition

Built-in class attributes are predefined attributes provided by Python that give information
about a class such as its name, documentation, module, and base classes.
Common Built-in Class Attributes
__name__
●​ Stores the name of the class

class Student:
pass

print(Student.__name__) # Student

__doc__
●​ Stores the documentation string (docstring) of the class

class Student:
"""This class represents a student"""
pass

print(Student.__doc__)

__module__
●​ Stores the module name in which the class is defined

print(Student.__module__) # __main__

__dict__
●​ Stores a dictionary of the class namespace

●​ Contains class variables and methods


print(Student.__dict__)

__bases__
●​ Stores a tuple of base (parent) classes
class Person:
pass

class Student(Person):
pass

print(Student.__bases__) # (<class '__main__.Person'>,)


Purpose of Built-in Class Attributes
●​ Provide information about class structure
●​ Support introspection (examining objects at runtime)
●​ Help in debugging and documentation
●​ Used internally by Python and frameworks
Table

Attribute Description

__name__ Class name

__doc__ Class documentation

__module__ Module name

__dict__ Class namespace

__bases__ Parent classes

Class Method:
A class method is a method that operates on class variables and uses cls as its first parameter.
Static Method:
A static method is a method that does not operate on instance or class variables and does not
require self or cls.
INHERITING CLASSES
Inheritance allows one class to acquire the properties and methods of another class.​
The class that is inherited from is called the parent (base) class, and the class that inherits is
called the child (derived) class.
Inheritance is one of the core concepts of Object-Oriented Programming (OOP).​
It allows a new class to reuse the properties and methods of an existing class.
●​ The existing class is called the Parent / Base / Super class
●​ The new class is called the Child / Derived / Sub class

Basic Syntax
class Parent:
pass

class Child(Parent):
pass

Purpose of Inheritance
●​ Promotes code reusability
●​ Reduces redundancy
●​ Improves maintainability
●​ Supports polymorphism
●​ Represents real-world relationships
Types of Inheritance in Python

Single Inheritance
Definition:​
A child class inherits from one parent class.
Structure:​
Parent → Child
Use case:​
Simple parent-child relationship.
Multilevel Inheritance
Definition:​
A class is derived from another derived class, forming a chain of inheritance.
Structure:​
Grandparent → Parent → Child
Use case:​
Represents inheritance across multiple levels.

Multiple Inheritance
Definition:​
A child class inherits from more than one parent class.
Structure:​
Parent1 + Parent2 → Child
Use case:​
When a class needs features from multiple classes.

Hierarchical Inheritance
Definition:​
Multiple child classes inherit from the same parent class.
Structure:​
Parent → Child1, Child2, Child3
Use case:​
Common base class shared by different subclasses.

Hybrid Inheritance
Definition:​
Combination of two or more types of inheritance.
Structure:​
Mix of single, multiple, multilevel, or hierarchical inheritance.
Use case:​
Complex real-world systems.
Key Terms Related to Inheritance
●​ Superclass – class being inherited from
●​ Subclass – class that inherits
●​ Method overriding – redefining parent method in child
●​ super() – calls parent class methods
●​ MRO (Method Resolution Order) – order in which methods are searched

Advantages of Inheritance
●​ Faster development
●​ Better organization of code
●​ Easy extension of existing classes
●​ Enhances readability

Disadvantages (Theory Point)


●​ Tight coupling between classes
●​ Can increase complexity if overused
Single Inheritance
One child inherits from one parent.
class Person:
def __init__(self, name, age, city):
[Link] = name
[Link] = age
[Link] = city

class Student(Person):
def display(self):
print([Link], [Link], [Link])

s = Student("Asha", 20, "Delhi")


[Link]()

Multilevel Inheritance

Inheritance across multiple levels.

class Person:
def speak(self):
print("I can speak")

class Student(Person):
def study(self):
print("I can study")

class Graduate(Student):
def research(self):
print("I do research")

g = Graduate()
[Link]()
[Link]()
[Link]()

Multiple Inheritance

A class inherits from more than one parent.

class Sports:
def play(self):
print("I play sports")

class Academics:
def study(self):
print("I study")

class Student(Sports, Academics):


pass

s = Student()
[Link]()
[Link]()

Hierarchical Inheritance

Multiple child classes inherit from the same parent.

class Person:
def speak(self):
print("I can speak")

class Student(Person):
pass

class Teacher(Person):
pass
Hybrid Inheritance

Combination of two or more types of inheritance.

class A:
pass

class B(A):
pass

class C(A):
pass

class D(B, C):


pass

Method Overriding

Child class provides its own implementation of a parent method.

class Parent:
def show(self):
print("Parent method")

class Child(Parent):
def show(self):
print("Child method")

super() Function

Used to call parent class methods.

class Parent:
def __init__(self):
print("Parent constructor")

class Child(Parent):
def __init__(self):
super().__init__()
print("Child constructor")

Key Advantages

●​ Code reusability
●​ Easy maintenance
●​ Extensibility
●​ Supports polymorphism

You might also like