Project Ip
Project Ip
ANSWER:
string1=input("Enter a string :")
print(string1[a])
OUTPUT:
If input is PYTHON:
Q.2. Program to find frequencies of all elements of a list. Also, print the list
of unique elements in the list and duplicate elements in given list.
ANSWER:
lst=eval(input("Enter list:"))
length=len(lst)
uniq=[]
dupl=[]
count=i=0
while i<length:
element=lst[i]
count=1
if element not in uniq and element not in dupl:
i+=1
else:
if count==1:
uniq.append(element)
else:
dupl.append(element)
else:
i+=1
print("ORIGINAL LIST : ", lst)
OUTPUT:
Q.3. Write a program to create a dictionary containing names of the
competition winner students as keys and number of their wins as values.
ANSWER:
string1=input("Enter a string :")
print("The", string1, "in reverse order is:")
length=len(string1)
for a in range(-1, (-length-1),-1):
print(string1[a])
OUTPUT:
Q.4. Marks of three students “Suniti”, “Ryna” and “Zeba” in three subjects
are available in following three dictionaries respectively:
D1 = {1:40, 2:70, 3:70}
Create a nested dictionary that stores the marks details along with student
names and then prints the output as shown below:
Name
Ryna
2 50
3 60
Name
Zeba
Subject (key) Marks (value)
1 70
2 80
3 90
Name
Suniti
Subject (key) Marks (value)
1 40
2 70
3 70
ANSWER:
D1 = {1:40, 2:70, 3:70}
for x in D4.keys():
print("Name")
print(x)
print("Subject(keys)",'\t', "Marks(value)")
for y in D4[x].keys():
print()
OUTPUT:
ANSWER:
import math
amount=principal+interest
print("Simple interest:{0:16.2f}".format(interest))
rate=rate/100
periods=time*n
amount=principal*pow((1+rate/n), periods)
interest=amount-principal
print("Compound interest:{0:16.2f}".format(interest))
Q.5. Program to calculate the amount payable after sales discount, which is 10
10% up to the sales amount of 20000 and 17.5% on amounts above. (in
range 5-12%)
ANSWER:
salesamt=float(input("Enter sales amount:"))
if salesamt<=20000:
discountrate=10
else:
discountrate=17.5
discount=salesamt*discountrate/100
discount=salesamt*discountrate/100
afterdiscount=salesamt-discount
netpayable=afterdiscount-salestax
ANSWER:
"""
"""
totalsales+=items
count+=1
avgsales=totalsales/totaltrans
cgst : Central Government GST; sgst : State Government GST, SP: Selling
price; itc : itemcode
ANSWER:
itc=input("Enter item code (A) Apparel (F) Footwear:")
if SP<=1000:
gstRate=5
else:
gstRate=12
elif itc=='f' or itc=='F':
item='Footwear'
if SP<=500:
gstRate=5
else:
gstRate=18
cgst=SP*(gstRate/2)/100
sgst=cgst
print('-'*65)
print("\t\t\t INVOICE")
print('-'*65)
print()
print("Item: {0:>40s}".format(item))
print("Price: \t\t{0:30.2f}".format(SP))
print('-'*65)
OUTPUT:
Q.8. Write a program that reads from a csv file (csv1.csv stored in data
folder of D:drive having data as: Name and marks in 3 subjects) in a
dataframe. Then the program should add a column “Total” storing total of
marks in 3 subjects and another column storing average marks . Print the
dataframe.
ANSWER:
import pandas as pd
df=pd.read_csv("D:\\csv1.csv", names=["Name", "Marks1", "Marks2", "Marks3"
])
print(df)
df['Total']= df['Marks1']+df['Marks2']+df['Marks3']
df['AvgMarks']=df['Total'] /3
print("Dataframe after all the calculations")
print(df)
OUTPUT:
Q.10. Write a program to iterate over a DataFrame and print each data
value individually along with its row index and column name using iterrows().
ANSWER:
import pandas as pd
import numpy as np
df1=pd.DataFrame(disales)
i=0
for val in rowSeries:
i=i+1
OUTPUT:
Q.12. Write a program that reads from a csv file in a dataframe. (marks.csv)
Then the program should add a column “Total” storing total of marks in 3
subjects and another column storing average marks. Print the dataframe.
ANSWER:
import pandas as pd
print(df)
OUTPUT:
B=array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
ANSWER:
import numpy as np
B=np.array([[0, 1, 2, 3],
[4, 5, 6, 7],
OUTPUT:
(A)
(B)
(C)
(D)
(E)
(F)
(A) Adding 10 to P
(B) Multiplication of the two array P and Q
(C) Divide all elements of Q by 7
(D) Round all the elements of Q to nearest integers
(E) Calculate remainder of all the elements of P when divided by 7
(F) Calculate square root of the elements of Q
ANSWER:
Creating arrays P and Q:
OUTPUT:
(A)
(B)
(C)
(D)
(E)
(F)
Q.14. Create an array x in the range 1 to 10 with values 0.1 apart. Create
another two arrays namely a and b as cos and sin of the elements of x.
ANSWER:
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(0,10,0.1)
a=np.cos(x)
b=np.sin(x)
plt.scatter(x,b, c="b", marker="+")
plt.savefig("scatter1.pdf")
plt.show()
OUTPUT:
nz=np.array([3, 5, 4, 8, 11])
plt.xlabel("Overs")
plt.ylabel("Runs")
plt.xlim(0,6)
plt.ylim(0,18)
plt.title("IND Vs NZ")
plt.legend(loc="upper right")
plt.savefig("scatter.pdf")
plt.show()
OUTPUT:
overs=[1,2,3,4,5]
runs=[15,5,6,10,8]
plt.xlabel("Overs")
plt.ylabel("Runs")
plt.show ()
OUTPUT:
Q.17. Given the following set of data:
Weight measurements for 16 small orders of French Fries (in grams).
78 72 69 81 63 67 65 75
79 74 71 83 71 79 80 69
ANSWER:
(A)
import pandas as pd
import numpy as np
ary=np.array([78,72,69,81,63,67,65,75,79,74,71,83,71,79,80,69])
plt.hist(ary, bins=20)
plt.show()
(B)
import numpy as np
ary=np.array([78,72,69,81,63,67,65,75,79,74,71,83,71,79,80,69])
plt.hist(ary, bins=20, orientation="horizontal")
plt.show()
OUTPUT:
(C)
import numpy as np
ary=np.array([78,72,69,81,63,67,65,75,79,74,71,83,71,79,80,69])
plt.show()
OUTPUT:
(D)
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
ary=np.array([78,72,69,81,63,67,65,75,79,74,71,83,71,79,80,69])
OUTPUT:
Q.18. From the following ordered set of data
63, 65, 67, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 83
ANSWER:
(A)
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
ary=np.array([63,65,67,69,71,71,72,74,75,78,79,79,80,81,83])
plt.boxplot(ary, vert=False)
plt.show()
OUTPUT:
(B)
import numpy as np
ary=np.array([63,65,67,69,71,71,72,74,75,78,79,79,80,81,83])
plt.boxplot(ary, vert=True)
plt.show()
OUTPUT:
(C)
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
ary=np.array([63,65,67,69,71,71,72,74,75,78,79,79,80,81,83])
plt.boxplot(ary, showmeans=True)
plt.show()
OUTPUT:
(D)
import pandas as pd
import numpy as np
ary=np.array([63,65,67,69,71,71,72,74,75,78,79,79,80,81,83])
plt.boxplot(ary, showbox=False)
plt.show()
OUTPUT:
Q.21. Design a Python application to obtain a search criteria from user and
then fetch records based on that from empl table.
ANSWER:
import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
password="admin123", database="prerna")
mycursor=mydb.cursor()
data=mycursor.fetchall()
for x in data:
print(x)
OUTPUT:
Q.19. Design a python application that fetches all the records from pet
table of menagerie database.
ANSWER:
import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
password="admin123", database="menagerie")
mycursor=mydb.cursor()
data=mycursor.fetchall()
for x in data:
print(x)
OUTPUT:
Q.20. Design a Python application that fetches only those records from
Event table of Menagerie database where type is Kennel.
ANSWER:
import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
password="admin123", database="menagerie")
mycursor=mydb.cursor()
data=mycursor.fetchall()
for x in data:
print(x)
OUTPUT:
Q.22. Design a Python application to insert a record into fields person id
and last name of staff table.
ANSWER:
import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
password="admin123", database="esha")
mycursor=mydb.cursor()
pid=int(input("Enter required person id"))
mydb.close()
OUTPUT:
After inserting the records:
Q.23. Activate
(a) Write commands to create a Django project namely ‘classxii’ having two
apps in it namely list and order.
(b) Register these apps with the project.
(c) Create a templates folder in the base directory of your project.
(e) The list app intends to show list of items available. For this, it has an html
page that displays a list of the items as shown below:
ITEMS LIST
1. Bath tubs 7. Bituminous Paints
2. Battery Charger 8. Blotting Paper
3. Battery Eliminator 9. Bolts and Nuts
4. Beam Scale(upto 1.5 tons) 10. Bolts Sliding
5. Belt leather and straps 11. Bone Meal
6. Brass Wire 12. Boot Polish
Create an html file namely list.html for this and store it under templates
folder of yours project.
(f) The order app intends to displays a form as shown below:
Customer name:
Phone Number:
Address:
Item To Be Ordered:
Order Quantity:
Create an html file namely order.html that displays this form and store it in
the templates folder.
(h) Create a view function for order app that renders Add POST request
processing to it so that it stores the received from data in a csv file.
(i) In the urls.py file of yours project’s web- application folder (inner project
name folder),import the apps’ views.py modules by adding following import
commands.
ANSWER:
Creating the django project:
1. In the active virtual environment, type the following command in front
of the prompt:
django - admin startproject classxii
2. It will create a folder by the name classxii.
3. Now change to this folder, your project’s folder, using command CD,
i.e., give command:
cd classxii
4. Now type following command in front of the prompt to run the built-in
webserver:
python manage.py runserver
5. Finally, open your web browser window and type either of the
following commands in the address bar:
localhost:8000
127.0.0.1:8000
This would result in a web page showing the django has been installed
and is successfully running.
Creating app of the Django project:
Register the app to that django recognizes then as a part of the web
application.
For this open inner classxii folder’s setting.py file in any editor and add
your apps’ name (in string form) in INSTALLED_APPS list without
deleting any other setting.
Creating templates:
In the BASE DIRECTORY (outer project name folder i.e., classxii) create
a folder by the name templates.
<h1><b>ITEMS</b></h1><br>
<h3>Select the items you want to buy</h3><br>
<table style="width:30%">
<tr>
<th>ITEM NO</th>
<th>ITEM NAME</th>
<th>PRICE</th>
</tr>
<tr>
<td>I01</td>
<td>Bath Tubs</td>
<td>1000</td>
</tr>
<tr>
<td>I02</td>
<td>Battery Charger</td>
<td>500</td>
</tr>
<tr>
<td>I03</td>
<td>Battery Eliminator</td>
<td>2000</td>
</tr>
<tr>
<td>I04</td>
</tr>
<tr>
<td>I05</td>
<td>2500</td>
</tr>
<tr>
<td>I06</td>
<td>Brass Wire</td>
<td>1000</td>
</tr>
<tr>
<td>I07</td>
<td>Bituminous Paints</td>
<td>2300</td>
</tr>
<tr>
<td>I08</td>
<td>Blotting Paper</td>
<td>3000</td>
</tr>
<tr>
<td>I09</td>
</tr>
<tr>
<td>I10</td>
<td>Bolts Sliding</td>
<td>900</td>
</tr>
<tr>
<td>I11</td>
<td>Bone Meal</td>
<td>800</td>
</tr>
<tr>
<td>I12</td>
<td>Boot Polish</td>
<td>400</td>
</tr>
</table>
</body>
</html>
The HTML File - item1 is connected to the HTML File - item2 through the link
displayed as “PLACE ORDER”.
HTML File – item2
<html>
<body>
{% csrf_token %}
Customer Name: <input type="text" name="custname"> <br>
</FORM>
</body>
</html>
The HTML File - item2 is connected to other HTML File - item3 through a link
displaying the placed order have been successfully accepted.
<html>
<body>
</html>
Here, creating HTML Files comes to end, now it is needed to be saved in the
desired place so that it results in desired output.
Next thing is to create views. Views receive the request in form of URL
and provide response in form of templates which are then presented
on the web browser.The views.py file of the app (in app directory),
where is to write view functions - one view function for each html page.
def <viewname> (request):
return render (request, <htmlfilenamestring>)
Creating URL Confs:
Once the template and views are ready, it is needed to link all these
with URLs through URL Confs. This process is also called URL routing
localhost:8000/first/
Type the above code for running the first page of the project as given below:
The code input would result the first page that is the image given below:
The first page would be linked to the second page showing the image below
as for getting the information to store:
The second page would take the input and provide a link that specifies the
placed order is successfully accepted.
The input took in the second page would be stored in the csv connected and
coded in the views.