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

1719213180Python2024_InsSheet modified

Python programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

1719213180Python2024_InsSheet modified

Python programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

INSTRUCTION SHEET

Paper Code: BCAC403


UPID: 400088
Paper Name: Python Programming
Group - A
1. Answer any ten of the following: (Full Marks should be awarded for writing proper answers. No
mark is to be awarded for incomplete / improper answers. Avoid part marking as far as possible).
(i) True
(ii) .py
(iii) 012
(iv) [2, 33, 222, 14]
(v) Tuple object has no attribute ‘append’
(vi) 7
(vii) Hello foo and bin
(viii) [13, 56, 17, [87], 45, 67]
(ix) (4, 5, 6, 2, 8, 6)
(x) Parentheses, Exponents, Multiplication and Division, Addition and Subtraction
(xi) Conversion of a Python object hierarchy into byte stream.
(xii) [1, 4, 8]
Group – B
Answer any three of the following: (Full Marks to be awarded for explaining the concepts and providing the
codes/example. Forproviding only explanation 2 marks are to be awarded and for only coding, 3 marks areto be
awarded.)
2. Class attributes are the variables defined directly in the class that are shared by all objects of the class. We can also
define attributes at the class level. Class attributes are attributes which are owned by the class itself. They will be
shared by all the instances of the class. Therefore, they have the same value for every instance.
3. A python variable is a reserved memory location to store values. In other words, a variable in a python program
gives data to the computer for processing. Variables can be declared by any name or even alphabets like a, aa, abc
etc. Variable name should start with letter or underscore. In variable name, no special characters allowed other than
underscore. Variables are case sensitive. Variable name can have numbers but not at the beginning. Variable name
should not be python keywords.
4. Yes, we can use the if – elif – else statement to tell python to try a different condition if the previous conditions were
not met. Sequence of an if – elif – else statement:
1. If the first condition is TRUE, then execute the first action. If it is FALSE, then test for the second condition.
2. If the second condition is TRUE, then execute the second action. If it is FALSE, then execute the final else
action.
Example:
x = 10
if x > 20:
print(“x is bigger than 10”)
elif x == 20:
print(“x is equal to 20”)
else:
print(“x is nither bigger than 10 or equal to 20”)
5. 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.
Example:
def my_function(fstring):
print(fstring + “ India”)

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')])

You might also like