Python fRRA
Python fRRA
The continue statement is a control flow statement used in loops to skip the
remaining code inside the current loop iteration. It essentially tells the loop
to continue executing the next iteration without completing the current one.
Purpose of continue statement:
The primary purpose of the continue statement is to modify the flow of a
loop by skipping certain iterations. This can be useful in situations where
you want to skip specific conditions or processing steps within the loop.
Syntax of continue statement:
The continue statement is a simple keyword that doesn't require any
arguments. It is typically placed within the body of the loop, usually after a
conditional check.
for item in some_list:
if condition(item):
continue
Here are some common scenarios where the continue statement is
useful:
• Skipping specific conditions: When you want to skip certain iterations
based on specific conditions.
• Early loop termination: When you want to terminate the loop early if
a certain condition is met.
• Optimizing loop performance: When you want to avoid unnecessary
processing for certain iterations.
• Iterating over collections: When you want to skip elements in a
collection based on specific criteria.
• Handling exceptions: When you want to skip processing exceptions
and continue the loop.
Provide an example of a recursive function and explain its working.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
result = factorial(5)
print("Factorial of 5:", result)
This code defines a recursive function called factorial() that takes an integer n as input.
The function calculates the factorial of n, which is the product of all positive integers
from 1 to n.
The function works by using recursion, which means that it calls itself to solve the
problem. In this case, the factorial() function calls itself with a smaller value of n until
n reaches 0. When n is 0, the function returns 1, which is the base case of the
recursion.For example, to calculate the factorial of 5, the factorial() function is called
with n = 5. The function then calls itself with n = 4, and so on, until n reaches 0. At
each step, the function multiplies the current value of n by the result of the previous
recursive call.
Write the custom function to input any 3 numbers and find out the largest one.
def find_largest(num1, num2, num3):
largest = num1
if num2 > largest:
largest = num2
if num3 > largest:
largest = num3
return largest
largest_number = find_largest(10, 20, 30)
print("The largest number is:", largest_number)
This function takes three numbers as input and returns the largest one. It works by
comparing the three numbers and assigning the largest one to a variable. The variable
is then returned as the result of the function.
Define Object-Oriented Programming (OOP) and explain its key
features.
Object-Oriented Programming (OOP) is a programming paradigm that
organizes code around data or objects, rather than functions and logic. It
encapsulates data and code within a unit called an object, which can interact
with other objects to create complex systems.
Key Features of OOP in Python:
Classes and Objects: Classes are blueprints for creating objects, while
objects are instances of classes. Classes define the attributes (properties)
and methods (behaviors) that objects possess.
Encapsulation: Encapsulation hides the internal implementation details of
an object, exposing only necessary methods to interact with it. This
promotes modularity and protects data integrity.
Inheritance: Inheritance allows classes to inherit attributes and methods
from parent classes, enabling code reusability and creating hierarchies of
related objects.
Polymorphism: Polymorphism allows objects of different classes to
respond to the same method call in different ways, based on their specific
type. This promotes flexibility and dynamic behavior.
Abstraction: Abstraction focuses on the essential characteristics of an
object, hiding unnecessary details. This simplifies code and promotes code
reusability.
Example of OOP in Python:
class Employee:
def __init__(self, name, age, department):
self.name = name
self.age = age
self.department = department
def get_employee_details(self):
print(f"Name: {self.name}") print(f"Age: {self.age}")
print(f"Department: {self.department}")
Define a constructor in Python. Why is it used? Provide an example.