Functions and Classes in Python
Defining a function
def
keyword, followed by the function name and parentheses ()
. Any parameters go inside the parentheses.
def function_name(parameters):
"""
Docstring: Brief description of the function
"""
# Function body
# Code block
return value
Calling a function
function name followed by parentheses ()
function_name(arguments)
Parameters and Arguments
The values you pass into a function when calling it are called arguments. The variables listed in the function definition are called parameters.
def greet(name, greeting="Hello"): # name is a parameter, greeting is a parameter with a default value
print(f"{greeting}, {name}!")
greet("Alice") # Alice is the argument
greet("Bob", "Hi") # Bob is the argument, Hi is the argument
\Arbitrary Arguments (args)*
If you don't know how many arguments will be passed to a function, you can use the *args
syntax to collect them into a tuple.
def print_numbers(*args):
for arg in args:
print(arg)
print_numbers(1, 2, 3) # Output: 1 2 3
\*Keyword Arguments (kwargs)*
You can also collect arbitrary keyword arguments into a dictionary using the **kwargs
syntax.
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name="Alice", age=25) # Output: name: Alice, age: 25
Lambda Functions
Python supports the creation of anonymous functions (lambda functions). These are small, one-line functions.
square = lambda x: x ** 2
print(square(5))
Python Classes/Objects
Python is an object oriented programming language.
Almost everything in Python is an object, with its properties and methods.
A Class is like an object constructor, or a "blueprint" for creating objects.
Define Python Class
We use the class
keyword to create a class in Python. For example,
class ClassName:
# class definition
Here, we have created a class named ClassNam
e
.
Let's see an example,
class Room:
length = 0
breadth = 0
def area(self):
return self.length * self.breadth
Room
- the name of the classlength and breadth
are the attributes of the classarea
is the method of the class
Understanding init method
def __init__(self, length=0, breadth=0):
self.length = length
self.breadth = breadth
It is the constructor of the class Room. Meaning it is initialized everytime when the class is called(when object is created)
class Room:
# Constructor
def __init__(self, length=0, breadth=0):
self.length = length
self.breadth = breadth
# Instance methods
def area(self):
return self.length * self.breadth
def perimeter(self):
return 2 * (self.length + self.breadth)
def print_details(self):
print(f"Room dimensions: {self.length} x {self.breadth} meters")
print(f"Area: {self.area()} square meters")
print(f"Perimeter: {self.perimeter()} meters")
# Creating instances (objects) of the Room class
room1 = Room(5, 4)
room2 = Room(3, 3)
# Calling instance methods
room1.print_details()
print()
room2.print_details()
print()
Inheritance:
class Room:
# Class attribute
num_rooms = 0
# Constructor
def __init__(self, length=0, breadth=0):
self.length = length
self.breadth = breadth
Room.num_rooms += 1
# Instance methods
def area(self):
return self.length * self.breadth
def perimeter(self):
return 2 * (self.length + self.breadth)
def print_details(self):
print(f"Room dimensions: {self.length} x {self.breadth} meters")
print(f"Area: {self.area()} square meters")
print(f"Perimeter: {self.perimeter()} meters")
# Child class inheriting from Room
class Bedroom(Room):
def __init__(self, length, breadth, num_beds):
super().__init__(length, breadth)
self.num_beds = num_beds
def print_details(self):
super().print_details()
print(f"Number of beds: {self.num_beds}")
# Creating instances of the classes
room1 = Room(5, 4)
bedroom1 = Bedroom(4, 3, 2)
# Calling instance methods
room1.print_details()
print()
bedroom1.print_details()