PythonYOYO for BCA
PythonYOYO for BCA
message = greet("Alice")
print(message) # Output: Hello, Alice
Parameters and Arguments
Parameters are variables that accept values within
a function. Arguments are the values passed to
the function.
python
Copy code
def add(a, b):
return a + b
result = add(3, 5) # 3 and 5 are arguments
print(result) # Output: 8
Importing Functions
You can import specific functions from a module.
python
Copy code
from math import sqrt, pow
if x > 5:
if y > 15:
print("x is greater than 5 and y is greater than
15")
Example Programs
python
Copy code
# Program to check if a number is positive,
negative, or zero
num = float(input("Enter a number: "))
if num > 0:
print("The number is positive")
elif num == 0:
print("The number is zero")
else:
print("The number is negative")
Iteration
while Loop
Repeats a block of code as long as the condition is
True.
python
Copy code
count = 0
while count < 5:
print(count)
count += 1
for Loop
Iterates over a sequence (e.g., list, string, range).
python
Copy code
# Iterating over a list
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
# Using range
for i in range(5):
print(i)
break Statement
Exits the loop prematurely when a condition is
met.
python
Copy code
for i in range(10):
if i == 5:
break
print(i)
continue Statement
Skips the current iteration and proceeds to the
next one.
python
Copy code
for i in range(10):
if i % 2 == 0:
continue
print(i) # prints only odd numbers
Nested Loops
Loops inside other loops.
python
Copy code
for i in range(3):
for j in range(3):
print(f"i = {i}, j = {j}")
Example Programs
python
Copy code
# Program to find the sum of all numbers up to a
given number
n = int(input("Enter a number: "))
sum = 0
for i in range(1, n+1):
sum += i
print(f"The sum of all numbers from 1 to {n} is
{sum}")
Recursion
What is Recursion?
Recursion is a programming technique where a
function calls itself in order to solve smaller
instances of the same problem. This continues
until it reaches a base case, which is a condition
that stops the recursion.
How Recursion Works
1. Base Case: This is the condition that stops the
recursion. Without a base case, the function
would call itself indefinitely, leading to a stack
overflow.
2. Recursive Case: This is where the function
calls itself with a smaller or simpler input,
gradually approaching the base case.
Example of a Recursive Function
• Factorial Calculation: The factorial of a
number n (denoted as n!) is the product of all
positive integers less than or equal to n.
python
Copy code
def factorial(n):
if n == 0:
return 1 # Base case
else:
return n * factorial(n - 1) # Recursive case
# Instance method
def description(self):
return f"{self.name} is {self.age} years old"
def __del__(self):
print(f"An instance with value {self.value} is
being destroyed")
obj = MyClass(10)
del obj # Explicitly deletes the object
Inheritance
Inheritance allows a class to inherit attributes and
methods from another class.
python
Copy code
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("Subclass must
implement this method")
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow"
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak()) # Output: Buddy says Woof
print(cat.speak()) # Output: Whiskers says Meow
Method Overriding
Method overriding allows a subclass to provide a
specific implementation of a method that is
already defined in its superclass.
python
Copy code
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return "Woof"
dog = Dog()
print(dog.speak()) # Output: Woof
Overloading Methods
Python does not support method overloading by
default. However, you can achieve similar
functionality using default arguments.
python
Copy code
class MathOperations:
def add(self, a, b, c=0):
return a + b + c
math_op = MathOperations()
print(math_op.add(2, 3)) # Output: 5
print(math_op.add(2, 3, 4)) # Output: 9
Overloading Operators
Operator overloading allows you to define how
operators behave with your custom objects.
python
Copy code
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"Point({self.x}, {self.y})"
p1 = Point(1, 2)
p2 = Point(3, 4)
p3 = p1 + p2
def get_hidden_value(self):
return self.__hidden_value
obj = MyClass(10)
print(obj.get_hidden_value()) # Output: 10
# print(obj.__hidden_value) # This will raise an
AttributeError
Example Program
python
Copy code
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.__balance = balance
def get_balance(self):
return self.__balance
# Creating an account
account = BankAccount("Alice", 100)