0% found this document useful (0 votes)
2 views

Python notes part5

The document provides comprehensive training notes on Python, covering key concepts such as recursion, pattern printing, and object-oriented programming (OOP). It explains recursion with examples like factorial and Fibonacci series, discusses various patterns that can be printed using loops, and outlines OOP principles including encapsulation, abstraction, inheritance, and polymorphism. Each section includes syntax examples and practical applications to enhance understanding.

Uploaded by

chirurxc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python notes part5

The document provides comprehensive training notes on Python, covering key concepts such as recursion, pattern printing, and object-oriented programming (OOP). It explains recursion with examples like factorial and Fibonacci series, discusses various patterns that can be printed using loops, and outlines OOP principles including encapsulation, abstraction, inheritance, and polymorphism. Each section includes syntax examples and practical applications to enhance understanding.

Uploaded by

chirurxc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PYTHON TRAINING NOTES DREAM A DREAM

Recursion

1 What is Recursion?

Recursion is a programming technique where a function calls itself to solve a problem. Instead of using
loops, recursion breaks down a problem into smaller subproblems, solving each one step by step.

Key Characteristics of Recursion:

✔ A recursive function calls itself.


✔ It must have a base condition (stopping condition).
✔ It breaks the problem into smaller instances of itself.
✔ Used when problems can be divided into similar subproblems.

2 Syntax of Recursion in Python

def recursive_function(parameters):
if base_condition: # Stopping condition (prevents infinite recursion)
return value
else:
return recursive_function(modified_parameters) # Function calls itself

3 How Recursion Works?

Recursion follows the call stack mechanism, which stores function calls in memory until the base
condition is met.

Example: Calculating Factorial of a Number (n!)

Mathematical formula:
[ n! = n \times (n-1) \times (n-2) \times ... \times 1 ] or
[ n! = n \times (n-1)! ] with the base condition 1! = 1.

Factorial Example Using Recursion

def factorial(n):
if n == 1: # Base condition
return 1
else:

PREPARED BY HARISH YADAV pg. 1


PYTHON TRAINING NOTES DREAM A DREAM

return n * factorial(n - 1) # Recursive call

print(factorial(5)) # Output: 120


How it works?
factorial(5) → 5 * factorial(4)
factorial(4) → 4 * factorial(3)
factorial(3) → 3 * factorial(2)
factorial(2) → 2 * factorial(1)
factorial(1) → 1 (base condition met, function starts returning values)
Final computation: 5 × 4 × 3 × 2 × 1 = 120

4 Recursion vs Iteration (Loop)

Feature Recursion Iteration (Loop)

Memory
High (stores function calls in stack) Low (uses a loop variable)
Usage

Faster (executes in a single


Speed Slower due to repeated function calls
block)

Code Length Shorter & more intuitive Longer but efficient

Best for problems that break into Best for straightforward


Use Case
subproblems tasks

When to Use Recursion?


✔ When a problem can be broken down into similar subproblems.
✔ When writing tree-based or divide-and-conquer algorithms (e.g., QuickSort, MergeSort).
✔ When iteration is complex (e.g., Tower of Hanoi, Fibonacci).

When NOT to Use Recursion?


✖ When memory efficiency is critical (large input sizes may cause a stack overflow).
✖ When a loop-based approach is more straightforward.

PREPARED BY HARISH YADAV pg. 2


PYTHON TRAINING NOTES DREAM A DREAM

6 More Recursion Examples

Example 1: Fibonacci Series using Recursion

Fibonacci series:
[ F(n) = F(n-1) + F(n-2) ] with base conditions F(0) = 0, F(1) = 1.

def fibonacci(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)

print(fibonacci(6)) # Output: 8
Recursive calls:
fibonacci(6) → fibonacci(5) + fibonacci(4)
fibonacci(5) → fibonacci(4) + fibonacci(3)
...
fibonacci(2) → fibonacci(1) + fibonacci(0)

PATTERN PRINTING IN PYTHON


1 What is Pattern Printing?

Pattern printing in Python refers to displaying characters, numbers, or symbols in a structured format
using loops. These patterns are often used for problem-solving, logic-building, and interview
preparation.

Key Features of Pattern Printing:


✔ Uses nested loops (outer loop for rows, inner loop for columns).
✔ Can use numbers, alphabets, or symbols (*, #, etc.).
✔ Patterns can be triangular, rectangular, diamond-shaped, etc.

2 Syntax for Pattern Printing

for i in range(1, n+1): # Outer loop (controls rows)


for j in range(1, i+1): # Inner loop (controls columns)

PREPARED BY HARISH YADAV pg. 3


PYTHON TRAINING NOTES DREAM A DREAM

print("*", end=" ") # Print symbol or number


print() # Move to the next line after inner loop finishes
Outer loop runs from 1 to n (controls the number of rows).
Inner loop runs from 1 to i+1 (controls columns in each row).
print("*", end=" ") prints the star without moving to a new line.
print() moves to the next row.

3 Basic Patterns

Example 1: Right-Angled Triangle

Pattern:

*
**
***
****
*****
n=5
for i in range(1, n+1):
for j in range(1, i+1):
print("*", end=" ")
print()

Example 2: Inverted Right-Angled Triangle

Pattern:

*****
****
***
**
*
n=5
for i in range(n, 0, -1):
for j in range(i):
print("*", end=" ")
print()

PREPARED BY HARISH YADAV pg. 4


PYTHON TRAINING NOTES DREAM A DREAM

Example 3: Pyramid Pattern

Pattern:

*
**
***
****
*****
n=5
for i in range(n):
print(" " * (n-i-1), end="")
print("* " * (i+1))
Spaces are printed first to shift stars towards the center.
Number of stars increases with each row.

4 Number-Based Patterns

Example 4: Number Triangle

Pattern:

1
12
123
1234
12345
n=5
for i in range(1, n+1):
for j in range(1, i+1):
print(j, end=" ")
print()

Example 5: Inverted Number Triangle

Pattern:

12345
1234
123

PREPARED BY HARISH YADAV pg. 5


PYTHON TRAINING NOTES DREAM A DREAM

12
1
n=5
for i in range(n, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print()

6 Advanced Patterns

Example 7: Diamond Pattern

Pattern:

*
***
*****
*******
*********
*******
*****
***
*
n=5
for i in range(1, n+1, 2):
print(" " * ((n - i) // 2) + "*" * i)
for i in range(n-2, 0, -2):
print(" " * ((n - i) // 2) + "*" * i)
First loop prints the upper triangle.
Second loop prints the inverted lower triangle.

7 Application of Pattern Printing in Real Life

✔ Printing patterns in user interfaces (UI).


✔ Problem-solving in competitive programming.
✔ Understanding loops, nesting, and logic building.

PREPARED BY HARISH YADAV pg. 6


PYTHON TRAINING NOTES DREAM A DREAM

Object-Oriented Programming (OOP) in Python


OOP is a programming paradigm that organizes code into objects that contain data
(attributes) and functions (methods).

Key Features of OOP:

✔ Encapsulation – Hiding implementation details.


✔ Abstraction – Exposing only essential features.
✔ Inheritance – Reusing code from parent classes.
✔ Polymorphism – Different behaviors for the same method.

1 Class and Object

What is a Class?

A class is a blueprint for creating objects. It defines the structure and behavior (attributes and methods)
of objects.

What is an Object?

An object is an instance of a class with real data stored in memory.

Syntax of Class and Object:

class Car:
def __init__(self, brand, model, year): # Constructor
self.brand = brand
self.model = model
self.year = year

def display_info(self):
print(f"Car: {self.brand} {self.model}, Year: {self.year}")

# Creating an object (instance)


car1 = Car("Toyota", "Camry", 2022)
car1.display_info() # Output: Car: Toyota Camry, Year: 2022

PREPARED BY HARISH YADAV pg. 7


PYTHON TRAINING NOTES DREAM A DREAM

2 Encapsulation

Definition:

Encapsulation is data hiding using private and protected attributes.

Syntax of Encapsulation:

class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute

def deposit(self, amount):


self.__balance += amount
print(f"Deposited: {amount}, New Balance: {self.__balance}")

def get_balance(self):
return self.__balance

acc = BankAccount(1000)
acc.deposit(500) # Deposited: 500, New Balance: 1500
print(acc.get_balance()) # 1500
✔ Private attributes (__balance) cannot be accessed directly.
✔ Use getter methods (get_balance()) to access private data.

3 Abstraction

Definition:

Abstraction hides unnecessary details and exposes only the essential functionalities. It is implemented
using abstract classes.

Syntax of Abstraction:

from abc import ABC, abstractmethod

class Shape(ABC): # Abstract class


@abstractmethod
def area(self): # Abstract method
pass

PREPARED BY HARISH YADAV pg. 8


PYTHON TRAINING NOTES DREAM A DREAM

class Circle(Shape):
def __init__(self, radius):
self.radius = radius

def area(self): # Implementing abstract method


return 3.14 * self.radius * self.radius

circle = Circle(5)
print("Area:", circle.area()) # Output: Area: 78.5
✔ Abstract classes cannot be instantiated.
✔ Subclasses must implement all abstract methods.

4 Inheritance

Definition:

Inheritance allows one class (child class) to reuse the attributes and methods of another class (parent
class).

Types of Inheritance:

1) Single Inheritance
2) Multiple Inheritance
3) Multilevel Inheritance
4) Hierarchical Inheritance
5) Hybrid Inheritance

Single Inheritance

A child class inherits from a single parent class.

class Animal:
def sound(self):
print("Animals make sounds")

class Dog(Animal):
def bark(self):
print("Dog barks")

d = Dog()

PREPARED BY HARISH YADAV pg. 9


PYTHON TRAINING NOTES DREAM A DREAM

d.sound() # Output: Animals make sounds


d.bark() # Output: Dog barks

Multiple Inheritance

A child class inherits from multiple parent classes.

class A:
def methodA(self):
print("Method from A")

class B:
def methodB(self):
print("Method from B")

class C(A, B):


pass

obj = C()
obj.methodA() # Output: Method from A
obj.methodB() # Output: Method from B

Multilevel Inheritance

A class inherits from another class that is already a child of a class.

class Grandparent:
def grandparent_method(self):
print("Grandparent class")

class Parent(Grandparent):
def parent_method(self):
print("Parent class")

class Child(Parent):
def child_method(self):
print("Child class")

obj = Child()
obj.grandparent_method() # Output: Grandparent class
obj.parent_method() # Output: Parent class

PREPARED BY HARISH YADAV pg. 10


PYTHON TRAINING NOTES DREAM A DREAM

obj.child_method() # Output: Child class

Hierarchical Inheritance

Multiple child classes inherit from the same parent class.

class Parent:
def common_method(self):
print("This is a parent method")

class Child1(Parent):
pass

class Child2(Parent):
pass

obj1 = Child1()
obj2 = Child2()

obj1.common_method() # Output: This is a parent method


obj2.common_method() # Output: This is a parent method

5 Polymorphism

Definition:

Polymorphism allows different classes to have the same method name but different implementations.

Method Overriding

Child class overrides a method from the parent class.

class Animal:
def make_sound(self):
print("Animal makes sound")

class Dog(Animal):
def make_sound(self): # Overriding
print("Dog barks")

d = Dog()

PREPARED BY HARISH YADAV pg. 11


PYTHON TRAINING NOTES DREAM A DREAM

d.make_sound() # Output: Dog barks

Method Overloading (Achieved using default arguments)

Python does not support true method overloading, but we can simulate it using default parameters.

class Math:
def add(self, a, b, c=0):
return a + b + c

m = Math()
print(m.add(2, 3)) # Output: 5
print(m.add(2, 3, 4)) # Output: 9

PREPARED BY HARISH YADAV pg. 12

You might also like