Python Solved Sample Paper
Python Solved Sample Paper
c. Describe Dictionary
A dictionary is collection which is unordered, changeable and indexed.
Dictionaries are written with curl brackets, and they have keys and values.
Example:
company = {
"name": "Apple",
"product": "IPhone"
"model": "11"
}
class Error(Exception):
print("Value can't be 0.")
number = 0
try:
if number == 0:
raise Error
else:
print("Value is more then 0.")
except Error:
pass
Output:
Value can't be 0.
Q2 Any THREE
Membership Operators
Membership operators are used to test whether a value is found within a sequence.
Example of in :
x = 4
y = 8
list = [1, 2, 3, 4, 5]
if (x in list):
print("X is in list array")
else:
print("X is not in list array")
Output:
X is in list array
Example of not in :
if (y not in list):
print("Y is not in list array")
else:
print("Y is in list array")
Output:
Logical Operators
Logical operators are usedto perform locical operations on the values of variables. The value is
either true or false
Example of and , or and not .
a = True
b = False
print('a and b is', a and b)
print('a or b is', a or b)
print('not a is', not a)
a and b is False
a or b is True
a not b is False
fruits.append("orange")
print(fruits)
fruits.pop(1)
print(fruits)
fruits.sort()
print(fruits)
fruits.clear()
print(fruits)
Output:
Local Global
n1, n2 = 0, 1
if term < 0:
print("Invalid term")
else:
for i in range(term):
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
Output:
Q3 Any THREE
a. Write a program to input any two and interchange the tuple variable.
Example:
a = (1, 2, 3, 4, 5)
b = (13, 23, 36, 47, 75)
a,b = b,a
print(a)
print(b)
Output:
while
Syntax:
Example:
count = 0
print("over")
Output:
0
1
2
3
4
over
for loop
It has the ability to iterate over the items of any sequence, such as a list or a string.
Syntax:
for iterating in sequence: statements(s)
Example:
print("over")
Output:
banana
apple
mango
over
Nested loops
Python programming language allows to use one loop inside another loop.
Syntax:
Example:
nums = [1, 2, 3]
words = ["hello", "hi", "bye"]
Output:
1
hello
hi
bye
2
hello
hi
bye
3
hello
hi
bye
r - Read - Opens a file for reading. Error if the file does not exist.
w - Write - Opens a file for writing. Creates the file if it does not exist.
x - Create - Creates the specified file. Error if file exist.
a - Append - Opens a file for appending. Creates the if it does not exist.
Read a file
The read() method and r mode is used to read files. Before read a file, the file must open using
open() function.
Hello World
Program:
f = open("text.txt", "r")
print(f.read())
Output:
Hello world
Write a file
f = open("text.txt", "w")
f.write("Hello World")
f.close()
Hello World
class Parent:
def echo(self):
print('I am from Parent class.')
class Child(Parent):
def echo(self):
print('I am from Child class.')
p = Parent()
c = Child()
p.echo()
c.echo()
Output:
Q4 Any THREE
Output:
6
45
8
2
Hello World
Program:
Hello world
try:
# Code
except:
# Code
Example:
try:
print(x)
except NameError:
print("Variable x is not defined")
Output:
d. Write the output for the following if the variable fruit = "banana"
>>> fruit[:3]
>>> fruit[3:]
>>> fruit[3:3]
>>> fruit[:]
Output:
Q5 Any TWO
Numbers
Example:
a = 5
a = 2.0
a = 1+2j
String
s = "This is string"
s = '''
A multi line string
'''
List
Tuple
Set
a = {5,2,3,1,4}
Dictionary
d = {1:'value','key':2}
print(f)
Output:
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print(c)
[1, 2, 3, 4, 5, 6]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
Q6 Any TWO
Example:
Output:
remove() - removes given item from set. If item is not avaliable it will give error.
Example:
Output:
{'cherry'}
Comparison of set
< , > , <= , >= , == - comparison operators can also be use in set.
print(fruits|fruits2)
print(fruits&fruits2)
print(fruits-fruits2)
print(fruits>fruits2)
print(fruits<fruits2)
print(fruits==fruits2)
Output:
Simple Inheritance
In inheritance, the child class acquires the properties and access all the data members and function
defined in the parent class.
Illustration:
┏━━━━━━━━━━┓
┃Base Class┃
┗━━━━━━━━━━┛
⇑
┏━━━━━━━━━━━━┓
┃Deived Class┃
┗━━━━━━━━━━━━┛
Syntax:
class Base:
# Body of base class
class Derived(Base):
# Body of derived class
Example:
class Parent:
parentname = ""
childname = ""
def show_parent(self):
print(self.parentname)
class Child(Parent):
def show_child(self):
print(self.childname)
c = Child()
c.parentname = "Arati"
c.childname = "Purva"
c.show_parent()
c.show_child()
Output:
Arati
Purva
Multiple inheritance
Multiple inheritace means that you're inheriting the property of multiple classes into one.
Illustration:
┏━━━━━━━━━━━━┓┏━━━━━━━━━━━━┓┏━━━━━━━━━━━━┓
┃Base Class 1┃┃Base Class 2┃┃Base Class 3┃
┗━━━━━━━━━━━━┛┗━━━━━━━━━━━━┛┗━━━━━━━━━━━━┛
┃ ┃ ┃
┗━━━━━━━━━━━━━━╋━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━┓
┃Deived Class┃
┗━━━━━━━━━━━━┛
Syntax:
class A:
# variable of class A
class B:
# variable of class B
class C(A, B):
# variable of class C
Example:
class Parent1:
def echo(self):
print("Parent class 1")
class Parent2:
def echo2(self):
print("Parent class 2")
c = Child()
c.echo()
c.echo2()
c.show()
Output:
Parent class 1
Parent class 2
Child class
class Employee:
name = ""
department = ""
salary = 0
def setData(self):
self.name = input("Enter Name: ")
self.department = input("Enter Department: ")
self.salary = int(input("Enter Salary: "))
def showData(self):
print("Name:", self.name)
print("Department:", self.department)
print("Salary:", self.salary)
e = Employee()
e.setData()
e.showData()
Output: