1719213180Python2024_InsSheet modified
1719213180Python2024_InsSheet modified
my_function(“Hello”)
my_function(“Welcome”)
6. class py_solution:
def sub_sets(self, sset):
return self.subsetsRecur([], sorted(sset))
def subsetsRecur(self, current, sset):
1
if sset:
return self.subsetsRecur(current, sset[1:]) + self.subsetsRecur(current + [sset[0]], sset[1:])
return [current]
print(py_solution().sub_sets([4,5,6]))
Group – C
Answer any threeof the following: (Full Marks should be awarded for writing proper answers. No
Marks is to beawarded for incomplete / improper answers.).
7. (a) A function is a block of code which only runs when it is called. We can pass data, known as parameters into a
function. A function can return data as a result. In python a function is defined using the ‘def’ keyword.
(b) n=int(input("Enter number: "))
k=0
for i in range(2,n//2+1):
if(n%i==0):
k=k+1
if(k<=0):
print("Number is prime")
else:
print("Not a Prime Number")
8. (a) Advantages of For Loops:
• Readability: For loops are intuitive and easy to understand, making them suitable for beginners and maintaining
code readability.
• Versatility: For loops offer greater flexibility and can handle a wide range of scenarios, including complex logic and
conditional statements.
• Debugging: For loops provide better visibility during debugging, aseach iteration can be individually inspected.
Disadvantages of For Loops:
• Lengthy Syntax: For loops tend to require more lines of code compared to list comprehension, which can impact
readability in dense code.
• Performance: In certain cases, for loops may be slower than list comprehension due to the overhead of function
calls.
(b) We can use a break statement with both for loops and while loops. In a nested loop, break will stop execution of
the innermost loop. The flow of the program resumes at the next line of code immediately after the block.
# break statement for for loop
for item in iterable:
if some_condition:
break # exit the loop
9. (a) It is a mechanism that allows you to create a hierarchy of classes that share a set of properties and
methods by deriving a class from another class. Inheritance is the capability of one class to derive or
inherit the properties from another class.
Example:
class Country:
def ShowCountry(self):
print(“This is Spain”);
class State(Country):
def ShowState(self):
print(“This is State”);
st =State();
st.ShowCountry();
st.ShowState();
(b) To create a class, use the keyword class:
For example,Create a class named MyClass, with a property named x:
class MyClass:
x=5
10. def student_data(student_name, student_class):
return f'\nStudent Name: {student_name}\nClass: {student_class}'
2
print(student_data('Firstname Lastname', '12'))
11. (a) To comment out multiple lines in Python, you can use triple-quoted strings (''' or """) if not
assigned to aiable, or use the hash (#) symbol at the beginning of each line. The backslash (\) method
can also be used to indicate line continuation, though it's less common. Most developers prefer triple-
quoted strings or multiple single-line comments for clarity.
(b) A dictionary is a kind of data structure that stores items in key-value pairs. A key is a unique
identifier for an item, and a value is the data associated with that key. Dictionaries often store
information such as words and definitions, but they can be used for much more. Dictionaries are
mutable in Python, which means they can be changed after they are created. They are also
unordered, indicating the items in a dictionary are not stored in any particular order.
Dictionaries are created using curly braces {}. The key is on the left side of the colon (:) and the value
is on the right. A comma separates each key-value pair. Creating a Python dictionary is
straightforward. Remember to use curly braces {} and separate each key-value pair with a comma.
We can use the dict() function in Python to create a dictionary. This function takes two arguments:
The first argument is a list of keys.
The second argument is a list of values.
Check out the example of how to create a dictionary using the dict() function:
# empty dictionary
my_dict = {}
# dictionary with integer keys
my_dict = {1: 'apple', 2: 'ball'}
# dictionary with mixed keys
my_dict = {'name': 'John', 1: [2, 4, 3]}
# using dict()
my_dict = dict({1:'apple', 2:'ball'})
# from sequence having each item as a pair
my_dict = dict([(1,'apple'), (2,'ball')])