Cycle Test 2 QB
Cycle Test 2 QB
(Autonomous)
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC with Section 2(f), Accredited by NAAC with A+, NBA Accredited – Aero & CSE
COIMBATORE - 641105
c) Both local and global scope. d) It depends on the value of the variable.
3
Choose the best description about recursion. CO4 L1
a)A function calling itself directly or b) A function with no parameters
indirectly.
c) A function with a large number of return d) A function that cannot be called more
statements. than once.
4 Given the string s = "hello world", what will s[0:5] return?
CO3 L1
a) "world” b) "hello"
Part B 5 x 2 = 10 Marks
6 What is the significance of fruitful functions in Python? CO4 L2
Fruitful functions are those that perform computations and return a value using the
return statement. They are significant because:
● They make programs modular and reusable by allowing the returned value to
be utilized in other parts of the code.
● They reduce code duplication, enhancing readability and efficiency.
● For example, a function to calculate the area of a circle can be called
multiple times with different inputs, and its returned value can be directly
used in other calculations.
7 Differentiate local and global variables. CO4 L3
Difference Between Local and Global Variables
● Local Variables:
o Defined inside a function or block and are accessible only within that
scope.
o Created when the function starts and destroyed when the function
ends.
o Example:
def my_function():
print(local_var)
my_function()
● Global Variables:
o Defined outside all functions and accessible across the entire
program, including within functions (unless shadowed).
o They persist throughout the program’s lifecycle.
o Example:
def my_function():
print(global_var)
my_function()
print(global_var)
Example:
my_list = [1, "apple", True, 3.14]
print(my_list[0]) # Access the first element (1)
9 How to slice a list in Python? CO5 L2
List slicing allows you to extract a part of a list by using the syntax
list[start:end:step]:
● start: Index where slicing begins (inclusive).
● end: Index where slicing stops (exclusive).
● step: Interval between elements (default is 1).
Example:
my_list = [0, 1, 2, 3, 4, 5]
print(my_list[1:4]) # [1, 2, 3]
print(my_list[:3]) # [0, 1, 2]
print(my_list[::2]) # [0, 2, 4]
print(my_list[::-1]) # [5, 4, 3, 2, 1, 0] (reversed)
greet(age=30, name="Charlie")
def sum_all(*args):
return sum(args)
print(sum_all(1, 2, 3, 4)) # Outputs: 10
def print_info(**kwargs):
print(f"{key}: {value}")
Part C2 x 10 = 20 Marks
1 What is function? How a function is defined and called in python? Explain with a CO4 L2
simple program.
1. Definition: Functions are defined using the def keyword followed by the
function name and parentheses (). Optionally, you can include parameters
inside the parentheses. The body of the function contains the code to
execute, which is indented.
2. Calling: To execute a function, you "call" it by writing its name followed by
parentheses. If the function accepts parameters, you pass the required
arguments inside the parentheses.
# Define a function
Output
A recursive function is a function that calls itself, either directly or indirectly, in its
definition. Recursion allows a problem to be broken down into smaller subproblems
of the same type, making it an efficient approach for solving problems that can be
defined in terms of themselves.
1. Base Case: This is a condition that stops the recursion. Without a base case,
the function would call itself indefinitely, leading to a stack overflow error.
2. Recursive Case: This is the part of the function where it calls itself with a
modified parameter, gradually reducing the problem size.
This problem is naturally recursive because: [ n! = n \times (n-1)! ] and the base
case is: [ 0! = 1 ]
def factorial(n):
# Base case
if n == 0 or n == 1:
return n * factorial(n - 1)
Output
120
3 Explain in detail about creating the list, accessing values in the lists,updating the CO5 L2
lists and deleting the list elements .
1. Creating a List
A list is created using square brackets []. It can store items of mixed types, such as
integers, strings, or even other lists.
Example:
# Creating lists
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "cherry"]
mixed = [1, "hello", 3.5, [10, 20]]
print(numbers)
print(fruits)
print(mixed)
You can access items in a list using their index, which starts from 0 for the first
element.
Example:
# Accessing values
numbers = [1, 2, 3, 4, 5]
print(numbers[0]) # First element (1)
print(numbers[2]) # Third element (3)
# Accessing nested elements
mixed = [1, "hello", 3.5, [10, 20]]
print(mixed[3][1])
3. Updating a List
You can update list elements by assigning a new value to an existing index or by
adding elements to the list.
Example:
# Updating existing elements
numbers = [1, 2, 3, 4, 5]
numbers[1] = 10 # Update the second element
print(numbers) # Output: [1, 10, 3, 4, 5] # Adding elements
numbers.append(6) # Append a single element
numbers.extend([7, 8]) # Add multiple elements
print(numbers) # Output: [1, 10, 3, 4, 5, 6, 7, 8]
You can remove elements using methods like del, remove(), pop(), or clear().
Example:
# Deleting elements
numbers = [1, 2, 3, 4, 5]
# Using 'del' to remove by index
del numbers[0] # Deletes the first element
print(numbers) # Output: [2, 3, 4, 5]
# Using 'remove()' to delete by value
numbers.remove(3) # Removes the element with value 3
print(numbers) # Output: [2, 4, 5]
# Using 'pop()' to remove and return an element by index
last_element = numbers.pop(-1) # Removes the last element
print(last_element) # Output:5
print (numbers) #Output:[2,4]
# Using 'clear()' to delete all elements
numbers.clear()
print(numbers) #output:[]
4 Define methods in a string and write an example program using at least 5 methods. CO3 L2
def simple_string_methods():
text = "Welcome to Python programming"
# 1. Convert to lowercase
print("Lowercase:", text.lower())
# 2. Convert to uppercase
print("Uppercase:", text.upper())