Program 2
STOCKS PYTHON PROGRAM
Create a class Stock having attributes StockName,StockSector,StockValue. Create a method getStockList with takes a list of Stock objects and a StockSector(string) and returns a list containing stocks of the given sector having value more than 500.
input:
3
TCS
IT
1000
INFY
IT
400
BMW
Auto
1200
IT
OUTPUT:
TCS
ANSWER:
class Stock:
def __init__(self,sn,ss,sv):
self.sn=sn
self.ss=ss
self.sv=sv
class StockDemo:
def __init__(self):
self.a=10
def gets(self,l,s):
k=[]
for i in l:
if(i.ss==s and i.sv>500):
k.append(i)
return k
if __name__==’__main__’:
c=input()
p=[]
for i in range(c):
sn=raw_input()
ss=raw_input()
sv=input()
o=Stock(sn,ss,sv)
p.append(o)
s=raw_input()
pra=StockDemo()
l=pra.gets(p,s)
for i in l:
print i.sn,i.sv
EXPLANATION FOR THE ABOVE PROGRAM
This Python code defines a class Stock and a class StockDemo. The Stock class has attributes StockName, StockSector, and StockValue. The StockDemo class has a method gets which takes a list of Stock objects and a StockSector (a string), and returns a list containing stocks of the given sector having a value greater than 500.
The input is taken as follows:
First, the number of stocks is taken as input (c).
Then, a loop is run c times to take input for each stock. For each stock, the stock name (sn), stock sector (ss), and stock value (sv) are taken as input using raw_input() and input() functions.
Finally, the stock sector for which we want to get the list of stocks is taken as input (s) using the raw_input() function.
The program then creates a Stock object for each stock and adds it to the list p. An instance of StockDemo is created, and the gets method is called with the p list and the s sector. The resulting list is stored in l, and each element in l is printed with its stock name and stock value using a loop.
This is a Python code block that executes the following steps:
if __name__==’__main__’: checks whether the script is being executed as the main program or being imported as a module. If it is the main program, then the code block under this if statement will be executed.
c=input() takes the number of stocks as input from the user and stores it in the variable c.
p=[] initializes an empty list p which will store the Stock objects.
A for loop is run c times to take input for each stock. For each stock, the following steps are executed:
a. sn=raw_input() takes the stock name as input from the user and stores it in the variable sn.
b. ss=raw_input() takes the stock sector as input from the user and stores it in the variable ss.
c. sv=input() takes the stock value as input from the user and stores it in the variable sv.
d. o=Stock(sn,ss,sv) creates a Stock object with the given sn, ss, and sv and assigns it to the variable o.
e. p.append(o) adds the Stock object o to the p list.
s=raw_input() takes the stock sector for which we want to get the list of stocks as input from the user and stores it in the variable s.
pra=StockDemo() creates an instance of the StockDemo class and assigns it to the variable pra.
l=pra.gets(p,s) calls the gets method of the pra object with the p list and the s sector as parameters and assigns the resulting list to the variable l.
A for loop is used to iterate over the list l and print the stock name (i.sn) and stock value (i.sv) of each stock object i in the list.
Comments
Post a Comment