Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
Extensible : The programs or pieces of code written in C or C++ can be
integrated into Python and executed using PVM.
Embeddable : We can insert Python programs into C or C++ program.
PVM(Python virtual machine):
Let’s assume that we write Python program with the name x.py. Here ,x is the
program name and the .py is the extension name. The next step is to compile the
program using python compiler. The compiler converts the python program into
another code called byte code.
Byte code represents a fixed set of instructions that represents all operations like
arithmetic operations, comparision operations etc.
The next step is to run the program. If we directly give the byte code to the
computer, it cannot execute the code. Any computer can execute only binary code
which comprises 1s and 0s.
PVM uses interpreter which understands the byte code and converts it into
machine code. PVM understands the processor and OS in our computer. Then it
converts the byte code into machine code understandable to that processor. These
machine code instructions are then executed by the processor and results are
displayed.
An interpreter translates the program source code line by line.
2
2. Program to generate random numbers stored in array. Then find min, max
and sum of array elements and display array elements using slicing operators.
Program :
from random import*;
from array import*;
import sys
arr = array("f",[])
3
3.Write a program to demonstrate the usage of DICTIONARY and TUPLE
Program :
"""DICTIONARY"""
print(my_dict)
"""TUPLES"""
print("Tuples..")
4
#input("Enter the number of tuple values want to update : ")
a=("BMSCE","Bangalore")
print("Position value is : ",a[int(input("Enter the position of a tuple : "))])
b=(1,2,3,4,[5,6])
b[4][0]=10
print(tup3)
del a
print("After deleting tup : ")
if tup3 != None:
print(b)
else:
print("Tuple is deleted")
5
4.Write a program to use the all possible operations in Arrays and List.
Program :
print('Create an Array')
arr = [10, 20, 30, 40, 50]
print(arr)
print('list of integers')
my_list = [1, 2, 3]
print(my_list)
print(my_list[-1])
print(my_list)
7
5. Write a program to demonstrate the usage of operstors.
Program :
#Logical operators
x = True
y = False
8
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
#Membership operators
x = 'Hello world'
y = {1:'a',2:'b'}
print('H' in x)
print('hello' not in x)
print(1 in y)
print('a' in y)
9
UNIT - 2
1.Python Program to Append, Delete and Display Elements of a List Using Classes
Problem Solution
obj=check()
choice=1
while choice!=0:
print("0. Exit")
print("1. Add")
print("2. Delete")
print("3. Display")
choice=int(input("Enter choice: "))
if choice==1:
n=int(input("Enter number to append: "))
obj.add(n)
print("List: ",obj.dis())
elif choice==2:
n=int(input("Enter number to remove: "))
obj.remove(n)
print("List: ",obj.dis())
elif choice==3:
print("List: ",obj.dis())
elif choice==0:
print("Exiting!")
else:
print("Invalid choice!!")
print()
10
2. Python Program to create dictionary with a cricket player names and scores in a
match. Retrieve runs by entering the player’s name.
x = {}
n = int(input("How many players?"))
for i in range(n):
k = input("Enter Player's name:")
v = input("Enter Player's Score:")
x.update({k:v})
print('\n Players in this match:\n')
for pname in x.keys():
print(pname)
name = input('Enter the Player\'s name:')
runs = x.get(name,-1)
if(runs == -1):
print('Player not found')
else:
print('{}\'s score is {}'.format(name,runs))
3.Recursive function
def recur_fibo(n):
"""Recursive function to
print Fibonacci sequence"""
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
11
4.Tower of Hanoi
n=4
TowerOfHanoi(n, 'A', 'C', 'B')
12
UNIT - 3
def outerFunction():
global a
a = 20
def innerFunction():
global a
a = 30
print('a =', a)
a = 10
outerFunction()
print('a =', a)
ANS: 20
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
ANS: 4 6
13
3) Create a class TV with the attribute channel, volume, and switch indicating
whether
TV is On / Off. The methods needs to define:
a. Turn the TV ON or Off
b. Set the channel number 0 to 99
c. Raise the volume by one unit, Range from 0 to 20 d) View TV status (ON
/OFF), channel and volume.
Newly TV object is set to off with the channel set to 2 and volume initially 10.
class Tv:
def __init__(self):
self.status="OFF"
self.vol=10
self.channel=2
def changestatus(self):
if self.status=="OFF":
self.status="ON"
else:
self.status="OFF"
def changechannel(self):
ch=int(input("ENTER CHANNEL"))
if ch>=0 and ch<=99:
self.channel=ch
def setvol(self):
if self.vol<=20:
self.vol+=1
def display(self):
print(self.status,self.channel,self.vol)
tv=Tv()
while(True):
print("ENTER 1.TV STATUS 2.CHANGE CHANNEL 3.RAISE VOLUME 4.DISPLAY")
c=int(input("ENTER YOUR CHOICE"))
if c==1:
tv.changestatus()
tv.display()
elif c==2:
tv.changechannel()
tv.display()
elif c==3:
tv.setvol()
14
tv.display()
elif c==4:
tv.display()
else:
break
UNIT -4
1.Write a program for user defined exception that checks the internal and external
marks; if the internal marks greater than 40 it raises the exception “Internal Marks
is Exceed”; if the external marks is greater than 60 it raises the exception and
displays the message “The External Marks is Exceed”. Create the above exception
and implement in program.
15