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

UNIT 3 Solution Python Programming QUESTION BANK 2023-24

Uploaded by

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

UNIT 3 Solution Python Programming QUESTION BANK 2023-24

Uploaded by

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

Python programming (BCC302)

UNIT-3
QUESTION BANK SOLUTIONS

Q1: Explain the concept of string indexing & slicing and provide an example in Python.

Ans:

There are two type of Indexing:

1: Positive Indexing:

❑ Positive indexing start from Zero (0) and from left hand side i.e. first character store at index 0,
second at index 1 and so on.

❑ Every character of string has some index value.

2: Negative Indexing:

❑ Negative Indexing starts from negative indexing start from -1 and from right-hand side.

❑ Last character store at index -1 and as we move towards the left, it keeps increasing like -2, -3,
and so on.

Slicing: String slicing is the way of selection of substring.


String 'Like' is substring in given 'I Like Python' string. we can access string characters by giving indexes if
we use colon inside square bracket like [:] it becomes a slicing operator in Python.
➢ Start index: Index from which slicing starts.

➢ End Index: Index up to which slicing end.

➢ Step value is optional.

Example:

Q2: Differentiate between Mutable and Immutable objects in python languages with
example?
Ans:

Mutable Objects: Immutable Objects:


• Mutable objects are • Immutable objects are
those whose state or those whose state or
value can be modified value cannot be
after creation. modified after creation.
• Lists, dictionaries, and • Strings, tuples, and
sets are examples of integers are examples
mutable objects in of immutable objects in
Python. Python.
• When you modify a • When you perform an
mutable object, you are operation that appears
actually changing the to modify an
object in place. immutable object, you
are actually creating a
new object with the
modified value.

Example of Mutable Object: Examples of Immutable


Objects:
python
# Mutable object: List python
my_list = [1, 2, 3, 4] # Immutable objects:
String, Tuple, Integer
# Modifying the list in my_string = "Hello"
place my_tuple = (1, 2, 3)
my_list[2] = 10 my_integer = 5
print(my_list) # Output:
[1, 2, 10, 4] # Operations that create
new objects
In the example above, the list new_string = my_string +
", World!"
my_list can be modified by
new_tuple = my_tuple +
changing the value at a specific (4, 5)
index. new_integer = my_integer
* 2

# Original objects remain


unchanged
print(my_string) #
Output: 'Hello'
print(my_tuple) #
Output: (1, 2, 3)
print(my_integer) #
Output: 5

# New objects with


modified values
print(new_string) #
Output: 'Hello, World!'
print(new_tuple) #
Output: (1, 2, 3, 4, 5)
print(new_integer) #
Output: 10

Q3:
Explain the difference between function arguments and function
parameters. Discuss the concept of variable scope within
functions in Python

Ans: In Python, the terms "function parameters" and "function


arguments" are often used interchangeably, but they have distinct
meanings:

1. Function Parameters:
o Parameters are the names used in the function
definition to represent the input values that a
function expects.
o They act as placeholders for the actual values
(arguments) that will be passed into the function
when it is called.
o Parameters are defined in the function signature and
serve as a way to declare what kind of values a
function should accept.

python
• def add(x, y): # x and y are parameters
result = x + y
return result

• Function Arguments:

• Arguments are the actual values or expressions passed to a


function when it is called.
• They correspond to the parameters of the function and
provide the actual data that the function will operate on.
• Arguments are passed within the parentheses when calling
a function.

python
2. # 2 and 3 are arguments
3. sum_result = add(2, 3)
4.

1. Local Scope:
o Variables defined within a function have a local
scope, meaning they are only accessible within that
function.
o Once the function completes execution, the local
variables are destroyed, and their values are not
accessible from outside the function.

python
• def example_function():
local_variable = "I am local"
print(local_variable)

example_function()
# This would cause an error because local_variable is
not defined here:
# print(local_variable)

• Global Scope:

• Variables defined outside of any function have a global


scope, making them accessible throughout the entire
program.
• Global variables can be accessed and modified both inside
and outside functions.

python
• global_variable = "I am global"

def example_function():
print(global_variable)

example_function()
print(global_variable)

• Scope Hierarchy:

• When a variable is referenced within a function, Python


first looks for it in the local scope. If not found, it searches
in the enclosing (if any) function's scope, and so on, until it
reaches the global scope.

python
x = 10 # Global variable

def outer_function():
y = 20 # Enclosed variable

def inner_function():
z = 30 # Local variable
print(x, y, z)
inner_function()

outer_function()

In the example above, inner_function can access both the global


variable x and the enclosed variable y, but outer_function cannot
directly access z because it's local to inner_function.

Q4: Write a Python program to add 'ing' at the end of a given string
(length should be at least 3). If the given string already ends with 'ing',
add 'ly' instead. If the string length of the given string is less than 3,
leave it unchanged.
Ans:

def modify_string(input_string):

if len(input_string) >= 3:

if input_string.endswith('ing'):

result_string = input_string + 'ly'

else:

result_string = input_string + 'ing'

else:

result_string = input_string

return result_string

# Example usage:

input_str1 = "play"

input_str2 = "swimming"

input_str3 = "go"

result1 = modify_string(input_str1)

result2 = modify_string(input_str2)

result3 = modify_string(input_str3)

print(f"Original: {input_str1}, Modified: {result1}")


print(f"Original: {input_str2}, Modified: {result2}")

print(f"Original: {input_str3}, Modified: {result3}")

Q5:
When to Use Python Lists and when to Use Tuples, Dictionaries
or Sets.
ANS:

In Python, lists, tuples, dictionaries, and sets are all data structures that serve different purposes.
The choice of which one to use depends on the specific requirements of your program. Here are
some guidelines on when to use each:

1. Lists:
o Use when:
▪ You need an ordered collection of items.
▪ You need a collection that allows duplicate elements.
▪ You need to modify the elements (add, remove, or modify) after creation.
o Example:

python

• •
• my_list = [1, 2, 3, 'hello', 2.5]

• Tuples:

• Use when:
o You need an ordered, immutable collection of items.
o You want to create a collection that should not be changed during the program's
execution.
o You want to use the collection as a key in a dictionary (since keys must be
immutable).
• Example:

python

• •
• my_tuple = (1, 2, 'world', 3.14)

• Dictionaries:

• Use when:
o You need an unordered collection of key-value pairs.
o You want to look up values based on some unique identifier (the key).
o You need to associate values with labels or names.
• Example:
python

• •
• my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}

• Sets:

• Use when:
o You need an unordered collection of unique elements.
o You want to perform set operations like union, intersection, and difference.
• Example:

python

4.
o my_set = {1, 2, 3, 4, 5}
o

Note:

• Use lists when you need an ordered collection with the ability to modify elements.
• Use tuples when you need an ordered and immutable collection.
• Use dictionaries when you need a collection of key-value pairs for quick lookups.
• Use sets when you need an unordered collection of unique elements and want to perform
set operations.

Ultimately, the choice between lists, tuples, dictionaries, and sets depends on the specific
requirements of your program and the characteristics of each data structure.

Q6:
Explain Tuples and Unpacking Sequences in Python Data
Structure
ANS:

Tuples in Python:

A tuple is a data structure in Python that is similar to a list but is immutable, meaning its elements
cannot be changed after creation. Tuples are defined using parentheses () and can contain elements
of different data types. Tuples are often used to represent fixed collections of items.

Creating a Tuple:

python
my_tuple = (1, 'hello', 3.14)

Accessing Elements:
python
first_element = my_tuple[0] # Accessing the first element

Immutable Nature:

python
# This will result in an error because tuples are immutable
my_tuple[1] = 'world'

Unpacking Sequences in Python:

Unpacking sequences is a feature in Python that allows you to assign the elements of a sequence
(like a tuple or a list) to multiple variables in a single line. This is often used with tuples to assign
values to individual variables.

Example:

python
# Creating a tuple
coordinates = (3, 4)

# Unpacking the tuple into separate variables


x, y = coordinates

print("x:", x) # Output: x: 3
print("y:", y) # Output: y: 4

In this example, the tuple (3, 4) is unpacked into the variables x and y. This is a concise way to
assign values to multiple variables at once.

Swapping Variables:

python
a = 5
b = 10

# Swapping values using tuple unpacking


a, b = b, a

print("a:", a) # Output: a: 10
print("b:", b) # Output: b: 5

Unpacking sequences is not limited to tuples; it can be used with any iterable, such as lists.

Using the * Operator for Unpacking:

python
# Unpacking with the * operator
first, *rest = (1, 2, 3, 4, 5)

print("first:", first) # Output: first: 1


print("rest:", rest) # Output: rest: [2, 3, 4, 5]

Q7:
Describe the difference between append () and extend () methods
in Python lists.
ANS:

In Python, append() and extend() are two methods used to add elements to lists, but they behave
differently:

1. append() Method:
o The append() method is used to add a single element to the end of the list.
o The element can be of any data type, including another list or any other object.
o It modifies the original list in place by adding the specified element as a single item.

python
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]

If the element being appended is a list, it is added as a single element, not as individual elements.

python
• my_list = [1, 2, 3]
my_list.append([4, 5])
print(my_list) # Output: [1, 2, 3, [4, 5]]

• extend() Method:

• The extend() method is used to append elements from an iterable (e.g., a list, tuple, string)
to the end of the list.
• It modifies the original list in place by adding each element from the iterable individually.

python
my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list) # Output: [1, 2, 3, 4, 5]

Q8:
Explain the algorithm Sieve of Eratosthenes used in Python
Programming
ANS:

The Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to a given limit.
It efficiently identifies prime numbers and eliminates multiples of each prime as it iterates through
the numbers. The algorithm is named after the ancient Greek mathematician Eratosthenes, who
first described it.

Here's a step-by-step explanation of the Sieve of Eratosthenes algorithm:

1. Create a List of Numbers:


o Generate a list of integers from 2 to the desired limit. These are the numbers we
want to check for primality.
2. Start with the First Prime (2):
o The first number in the list is 2, which is a prime number. Mark it as prime, and
eliminate all multiples of 2 from the list.
3. Move to the Next Unmarked Number:
o Move to the next unmarked number (in this case, 3). Mark it as prime, and eliminate
all multiples of 3 from the list.
4. Repeat Until the Square Root of the Limit:
o Continue this process, marking the next unmarked number as prime and eliminating
its multiples, until the square root of the limit is reached. This is because, for any
composite number n, its smallest prime factor is guaranteed to be less than or equal
to √n.
5. Remaining Unmarked Numbers are Primes:
o After completing the elimination process, the remaining unmarked numbers in the
list are prime.

Here's a Python implementation of the Sieve of Eratosthenes:

python
def sieve_of_eratosthenes(limit):
primes = []
is_prime = [True] * (limit + 1)
is_prime[0] = is_prime[1] = False

for number in range(2, int(limit**0.5) + 1):


if is_prime[number]:
primes.append(number)
for multiple in range(number * number, limit + 1, number):
is_prime[multiple] = False

for number in range(int(limit**0.5) + 1, limit + 1):


if is_prime[number]:
primes.append(number)

return primes

# Example usage:
limit = 30
result = sieve_of_eratosthenes(limit)
print(result)

In this implementation, is_prime is a boolean list where is_prime[i] is True if i is a prime


number and False otherwise. The function returns a list of prime numbers up to the specified limit.
The time complexity of the Sieve of Eratosthenes is generally considered to be O(n log log n),
making it an efficient algorithm for finding primes.

Q9:
Explain the concept of list slicing and demonstrate its usage with
a sample list.

ANS:

List slicing in Python is a technique that allows you to create a subsequence (or "slice") of a list
by specifying a range of indices. The general syntax for list slicing is list[start:stop:step],
where start is the index of the first element to include, stop is the index of the first element to
exclude, and step is the number of indices between each element in the slice. All three parameters
are optional.

Here's an explanation of each part of the syntax:

• start: The index of the first element to include in the slice. If omitted, it defaults to 0 for
positive step values or -1 for negative step values (i.e., it starts from the end of the list).
• stop: The index of the first element to exclude from the slice. If omitted, it defaults to the
end of the list for positive step values or the beginning of the list for negative step values.
• step: The number of indices between each element in the slice. If omitted, it defaults to 1.

Now, let's demonstrate list slicing with a sample list:

python
# Sample list
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Basic slicing examples


slice1 = my_list[2:6] # Elements from index 2 to 5 (exclusive)
slice2 = my_list[3:] # Elements from index 3 to the end
slice3 = my_list[:5] # Elements from the beginning to index 4
slice4 = my_list[1:8:2] # Elements from index 1 to 7, every 2nd element

# Negative indexing and reverse slicing


slice5 = my_list[-3:] # Last 3 elements
slice6 = my_list[::-1] # Reverse the entire list

# Printing the results


print("Original list:", my_list)
print("Slice 1:", slice1)
print("Slice 2:", slice2)
print("Slice 3:", slice3)
print("Slice 4:", slice4)
print("Slice 5:", slice5)
print("Slice 6:", slice6)

Output:
less
Original list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Slice 1: [2, 3, 4, 5]
Slice 2: [3, 4, 5, 6, 7, 8, 9]
Slice 3: [0, 1, 2, 3, 4]
Slice 4: [1, 3, 5, 7]
Slice 5: [7, 8, 9]
Slice 6: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

In the example above, various slices of the original list are created using different combinations of
start, stop, and step values. The results demonstrate how list slicing can be a powerful and
concise way to extract specific portions of a list.

Q10:
Discuss the concept of function decorators and their role in
Python
ANS:

In Python, a decorator is a design pattern that allows you to extend or modify the behavior of
functions or methods without changing their actual code. Decorators are applied using the
@decorator syntax before the function definition. They are a powerful and flexible feature in
Python, commonly used for tasks such as logging, timing, authentication, and more.

Key Concepts:

1. Syntax:
o A decorator is a function that takes another function as an argument and usually returns
a new function that enhances or modifies the behavior of the original function.

python
• @decorator
def my_function():
# Original function code

• Function as First-Class Object:

• In Python, functions are first-class objects, which means they can be passed as arguments to other
functions and returned as values from other functions.

• Applying Multiple Decorators:

• You can apply multiple decorators to a single function, and they are executed in the order they
are listed.

python
3. @decorator1
4. @decorator2
5. def my_function():
6. # Original function code
7. This is equivalent to decorator1(decorator2(my_function)).

Example:

Let's consider a simple example of a decorator that logs information about the function call:

python
def log_decorator(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}")
result = func(*args, **kwargs)
print(f"{func.__name__} returned: {result}")
return result
return wrapper

@log_decorator
def add(a, b):
return a + b

result = add(3, 5)

In this example, the log_decorator function takes another function (func) as its argument,
defines a new function (wrapper) that logs information before and after calling func, and returns
this new function. The @log_decorator syntax is used to apply the decorator to the add function.

When add(3, 5) is called, it is actually the wrapper function created by the decorator that gets
executed, and it logs information about the function call.

Role in Python:

• Code Reusability: Decorators allow you to encapsulate common behavior and reuse it
across multiple functions.
• Separation of Concerns: Decorators help separate the concerns of different aspects of
code, making it more modular and easier to maintain.
• Aspect-Oriented Programming (AOP): Decorators support the AOP paradigm by
providing a way to add cross-cutting concerns (e.g., logging, authentication) without
modifying the core logic of functions.
• Readability: Decorators improve code readability by keeping the core functionality of a
function separate from additional features.

Decorators are a powerful tool in Python, enabling clean and modular code design by promoting
the separation of concerns and facilitating code reuse. They are widely used in various frameworks
and libraries to enhance the functionality of functions and methods.

Q11:
Find all of the numbers from 1-1000 that are divisible by 7 using list
comprehension.
ANS:
divisible_by_7 = [num for num in range(1, 1001) if num % 7 == 0]

# Print the result


print(divisible_by_7)

In this code:

• range(1, 1001) generates numbers from 1 to 1000.


• The list comprehension [num for num in range(1, 1001) if num % 7 == 0] iterates
through each number in the range and includes it in the list only if it is divisible by 7 (num
% 7 == 0 checks for divisibility).

When you run this code, it will print a list containing all the numbers from 1 to 1000 that are
divisible by 7.

Q12:
Write a Python program to get a list, sorted in increasing order
by the last element in each tuple from a given list of non-empty
tuples.
Sample List: [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Expected Result: [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
ANS:
# Sample list of tuples
sample_list = [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]

# Define a custom key function to extract the last element from each tuple
def last_element_of_tuple(t):
return t[-1]

# Use sorted() with the custom key function to sort the list of tuples
sorted_list = sorted(sample_list, key=last_element_of_tuple)

# Print the result


print("Original List:", sample_list)
print("Sorted List:", sorted_list)

This program defines a custom key function last_element_of_tuple that takes a tuple and
returns its last element (t[-1]). Then, the sorted() function is used with this custom key function
to sort the list of tuples based on their last elements.

When you run this program, it will output the expected result:

less
Original List: [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Sorted List: [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]

Q13:
Write a Python script to sort (ascending and descending) a dictionary
by value
ANS:
#Sample dictionary
sample_dict = {'apple': 5, 'banana': 2, 'orange': 8, 'grape': 1}

# Sort dictionary by values in ascending order


sorted_dict_ascending = dict(sorted(sample_dict.items(), key=lambda item:
item[1]))

# Sort dictionary by values in descending order


sorted_dict_descending = dict(sorted(sample_dict.items(), key=lambda item:
item[1], reverse=True))

# Print the results


print("Original Dictionary:", sample_dict)
print("Sorted Dictionary (Ascending):", sorted_dict_ascending)
print("Sorted Dictionary (Descending):", sorted_dict_descending)

In this script:

• sorted(sample_dict.items(), key=lambda item: item[1]) is used to sort the


dictionary by values in ascending order. The key parameter is set to a lambda function that
extracts the values.
• sorted(sample_dict.items(), key=lambda item: item[1], reverse=True) is used
to sort the dictionary by values in descending order. The reverse=True argument is added
to the sorted() function.

When you run this script, it will output the original dictionary and two sorted dictionaries
(ascending and descending):

python
Original Dictionary: {'apple': 5, 'banana': 2, 'orange': 8, 'grape': 1}
Sorted Dictionary (Ascending): {'grape': 1, 'banana': 2, 'apple': 5, 'orange':
8}
Sorted Dictionary (Descending): {'orange': 8, 'apple': 5, 'banana': 2, 'grape':
1}

Q14:
Write a Python program to count the number of characters
(character frequency) in a string. Sample String: google.com'
ANS:
# Sample string
sample_string = 'google.com'

# Initialize an empty dictionary to store character frequencies


char_frequency = {}
# Count the frequency of each character
for char in sample_string:
char_frequency[char] = char_frequency.get(char, 0) + 1

# Print the character frequencies


print("Character Frequencies in the String:")
for char, frequency in char_frequency.items():
print(f"{char}: {frequency}")

This program uses a dictionary (char_frequency) to store the frequency of each character in the
given string. The for loop iterates through each character in the string, and the get() method is
used to retrieve the current count of the character (or 0 if it's the first occurrence), and then
increments it.

When you run this program with the provided sample string, it will output the character
frequencies:

makefile
Character Frequencies in the String:
g: 2
o: 4
l: 1
e: 1
.: 1
c: 1
m: 1

Q15:
Write a Python program to compute the square of the first N
Fibonacci numbers, using the map function and generate a list of
the numbers.
ANS:
# Function to generate the first N Fibonacci numbers
def generate_fibonacci(n):
fib_numbers = [0, 1]
while len(fib_numbers) < n:
fib_numbers.append(fib_numbers[-1] + fib_numbers[-2])
return fib_numbers[:n]

# Function to compute the square of a number


def square(x):
return x ** 2

# Specify the value of N


N = 10

# Use map to compute the square of each Fibonacci number


squared_fibonacci = list(map(square, generate_fibonacci(N)))

# Print the result


print(f"The first {N} Fibonacci numbers squared:")
print(squared_fibonacci)

In this program:

• The generate_fibonacci function generates the first N Fibonacci numbers.


• The square function computes the square of a given number.
• The map function applies the square function to each element of the list of Fibonacci
numbers.
• The list() constructor is used to convert the result of map into a list.

Adjust the value of N as needed, and when you run the program, it will output the squared Fibonacci
numbers:

python
The first 10 Fibonacci numbers squared:
[0, 1, 1, 9, 25, 121, 361, 1444, 3249, 8100]

Q16: Implement a function that finds the largest e;ement in a list without using the max() function?

ANS:
def find_largest_element(lst):
# Check if the list is empty
if not lst:
return None

# Initialize the maximum to the first element


largest = lst[0]

# Iterate through the list to find the largest element


for element in lst:
if element > largest:
largest = element

return largest

# Example usage:
my_list = [3, 8, 2, 7, 5, 1, 4]
result = find_largest_element(my_list)
print("List:", my_list)
print("Largest Element:", result)

Q17: Write a program that merges two dictionaries and handles duplicate keys intelligently.
ANS:
def merge_dictionaries(dict1, dict2, conflict_resolution=lambda x, y: x + y):
merged_dict = dict1.copy()

for key, value in dict2.items():


if key in merged_dict:
merged_dict[key] = conflict_resolution(merged_dict[key], value)
else:
merged_dict[key] = value

return merged_dict

# Example dictionaries
dict1 = {'a': 10, 'b': 20, 'c': 30}
dict2 = {'b': 25, 'c': 35, 'd': 40}

# Merge dictionaries with conflict resolution using maximum value


merged_dict = merge_dictionaries(dict1, dict2, conflict_resolution=max)

# Print the result


print("Dictionary 1:", dict1)
print("Dictionary 2:", dict2)
print("Merged Dictionary (using max):", merged_dict)

Q18: Develop a Python function that removes duplicates from a list while maintaining the original
order.
ANS:
def remove_duplicates(original_list):
seen = set()
unique_list = []

for element in original_list:


if element not in seen:
seen.add(element)
unique_list.append(element)

return unique_list

# Example usage:
my_list = [3, 2, 1, 2, 4, 3, 5, 4, 6]
result = remove_duplicates(my_list)

print("Original List:", my_list)

In this example:
• The remove_duplicates function takes a list (original_list) as input.
• It uses a set (seen) to keep track of elements that have already been encountered.
• It iterates through the original list, and if an element is not in the set, it adds it to both the
set and a new list (unique_list).
• The function returns the list with duplicates removed.

When you run this code with the provided example list, it will output:

mathematica
Original List: [3, 2, 1, 2, 4, 3, 5, 4, 6]
List with Duplicates Removed: [3, 2, 1, 4, 5, 6]

Q19: Write a Python program to create a lambda function that adds 15 to a given number passed in as
an argument, also create a lambda function that multiplies argument x with argument y and prints the
result
ANS:
# Lambda function to add 15 to a given number
add_15 = lambda x: x + 15

# Lambda function to multiply two numbers


multiply = lambda x, y: x * y

# Example usage
number_to_add_15 = 5
result_add_15 = add_15(number_to_add_15)
print(f"Adding 15 to {number_to_add_15}: {result_add_15}")

number1 = 3
number2 = 7
result_multiply = multiply(number1, number2)
print(f"Multiplying {number1} and {number2}: {result_multiply}")

In this program:

• The add_15 lambda function takes one argument x and adds 15 to it.
• The multiply lambda function takes two arguments x and y and multiplies them.

When you run this code, it will output:

yaml
Adding 15 to 5: 20
Multiplying 3 and 7: 21

Q20: Write a Python function to calculate the factorial of a number (a non-negative integer). The
function accepts the number as an argument.
ANS:
def factorial(n):
# Base case: factorial of 0 or 1 is 1
if n == 0 or n == 1:
return 1
else:
# Recursive case: n! = n * (n-1)!
return n * factorial(n - 1)

# Example usage:
number = 5
result = factorial(number)

print(f"The factorial of {number} is: {result}")

In this function:

• The base case checks if n is 0 or 1, in which case the factorial is 1.


• The recursive case calculates the factorial using the formula n! = n * (n-1)!.

When you run this code with the provided example (number = 5), it will output:

csharp
The factorial of 5 is: 120

You might also like