Informatic Practices HHW
Informatic Practices HHW
IP HOLIDAY HOMEWORK
1) The python code written below has syntactical errors. Rewrite the correct code and
underline the corrections made.
Import pandas as pd
df={"Technology":["Programming","Robotics","3DPrinting"],"Time(in months)":[4,4,3]}
df= Pd.dataframe(df)
Print(df)
ANSWER
Import pandas as pd
df={"Technology":["Programming","Robotics","3DPrinting"],"Time(in months)":[4,4,3]}
df = pd.DataFrame(df)
Print(df)
2. Complete the given Python code to get the required output as: Rajasthan
import _______as pd
print(NP[ ___________])
ANSWER
import pandas as pd
NP = pd.Series(di)
print(NP['Sariska'])
3)
import pandas as pd
rollno=[1,2,3,4,5,6,7]
_____=pd.Series(rollno) #Statement 1
ANSWER
import pandas as pd
rollno = [1,2,3,4,5,6,7]
p = pd.Series(rollno) # Statement 1
print(p.head()) # Statement 2
OUTPUT
0 1
1 2
2 3
3 4
4 5
5 6
6 7
dtype: int64
4) 4. Consider two objects L and S. L is a list whereas S is a Series. Both have values 20,
40,90, 110. What will be the output of the following two statements considering that the
above objects have been created already.
ANSWER
Statement 1: print(L+2)
Since L is a list, the + operator is not defined for lists and integers. You will get a
TypeError:
This is because lists in Python do not support element-wise operations like addition with a
scalar value.
Statement 2: print(S+2)
0 22
1 42
2 92
3 112
dtype: int64
This is because the + operator is applied element-wise to each value in the Series, adding 2
to each value.
5. Mr. Harry wants to draw a line chart using a list of elements named LIST. Complete the
code to perform the following operations:
LIST=[10,20,30,40,50,60]
#Statement 1
#Statement 2
ANSWER
plt.plot(LIST)
# Statement 2: Give a y-axis label to the line chart named "sample number"
plt.ylabel("sample number")
plt.show()
6. A dictionary ‘stationary’ contains the following:
ANSWER
print(stationary['Name'])
print(stationary['Price'])
print(new_dict)
b. Add a column called ‘discount’ with the following data: [ 15, 20, 30, 25]
ANSWER
import pandas as pd
stock = pd.DataFrame({
})
# b. Add a column called ‘discount’ with the following data: [ 15, 20, 30, 25]
ANSWER
The main difference between single row and multiple row functions is that single row
functions work on one row at a time and returns one result for every row. While multiple
row functions work on a group of rows and return one result for every group
9. Write a program to create the series that stores the term marks of 10 students. Find the
highest and lowest three marks scored by students.
ANSWER
import pandas as pd
marks = pd.Series([85, 92, 78, 95, 88, 76, 91, 89, 90, 82])
print("Original Series:")
print(marks)
highest_three = marks.nlargest(3)
print(highest_three)
lowest_three = marks.nsmallest(3)
print(lowest_three)
OUTPUT
10. Write a program to create a series from list marks and taking index values from list roll
no. Show all elements that are above 80 marks.
ANSWER
import pandas as pd
marks = [85, 92, 78, 95, 88, 76, 91, 89, 90, 82]
print("Original Series:")
print(series)
88
9
OUTPUT
11. What is the difference between the following two statements, which are creating two
dataframes using the same dataframe (Pandas library has been imported as pd)?
ANSWER
The difference between the two statements lies in how they handle the data when creating
a new DataFrame.
i. Df2=pd.DataFrame(df1)
When you create a new DataFrame Df2 from an existing DataFrame df1 without specifying
the copy parameter, it creates a new DataFrame object but shares the same data as df1.
This means that both Df2 and df1 point to the same data in memory. Any changes made to
Df2 will also affect df1, and vice versa.
When you create a new DataFrame Df3 from an existing DataFrame df1 with the
copy=True parameter, it creates a new DataFrame object and copies the data from df1 to
Df3. This means that Df3 has its own separate data in memory, independent of df1. Any
changes made to Df3 will not affect df1, and vice versa
ANSWER
print(aid[['Books', 'Uniform']])
print(aid['Shoes'])
13. What are aggregate functions? How are they useful?
ANSWER
An aggregate function performs a calculation on a set of values, and returns a single value.
Except for COUNT(*) , aggregate functions ignore null values. Aggregate functions are often
used with the GROUP BY clause of the SELECT statement. All aggregate functions are
deterministic
ANSWER
The GROUP BY clause causes the rows of the items table to be collected into groups, each
group composed of rows that have identical order_num values (that is, the items of each
order are grouped together). After the database server forms the groups, the aggregate
functions COUNT and SUM are applied within each group.
15. WAP to plot a line chart between two given arrays
A=[4,56,78,23,45,67,89]
B=[0,1,2,3,4,5,6]
ANSWER
B = [0, 1, 2, 3, 4, 5, 6]
plt.plot(B, A)
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Line Chart')
plt.show()
ANSWER
import pandas as pd
df1 = pd.DataFrame([
print(df1)
ANSWER
import pandas as pd
D1 = pd.Series({
}, name='MAX MARKS')
print(D1)
18. Consider the decimal number x with value 54567.8577. Write commands in SQL to: i.
display the whole number without round it off. ii.round it to 2 places of the decimal.
ANSWER
19. A company randomly generated a weekly winning number (between 0. 1000) for 10
weeks. An employee of the company wants to plot the sine values of winningnumbers
(nump.sin( ) function) on a line chart. Write a program to help him accomplish this.
ANSWER
import numpy as np
sine_values = np.sin(winning_numbers)
plt.plot(sine_values)
plt.xlabel('Week')
plt.ylabel('Sine Value')
plt.show()
20. Write a small python code to create a dataframe with headings (a and b) from the list
given below: [ [1,2],[3,4],[5,6],[7,8] ]
ANSWER
import pandas as pd
data = [[1,2],[3,4],[5,6],[7,8]]
print(df)
OUTPUT
SELF QUERIES
Question 1
Answer
Output
+-----------+------+
| ENAME | SAL |
+-----------+------+
| MAHADEVAN | 2985 |
| BINA | 2850 |
| AMIR | 5000 |
| SHIAVNSH | 2450 |
| SCOTT | 3000 |
| FAKIR | 3000 |
+-----------+------+
Question 2
Answer
SELECT *
FROM empl
WHERE COMM IS NULL OR COMM = 0;
Output
+-------+-----------+-----------+------+------------+------+------+---
-----+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM |
DEPTNO |
+-------+-----------+-----------+------+------------+------+------+---
-----+
| 8369 | SMITH | CLERK | 8902 | 1990-12-18 | 800 | NULL |
20 |
| 8566 | MAHADEVAN | MANAGER | 8839 | 1991-04-02 | 2985 | NULL |
20 |
| 8698 | BINA | MANAGER | 8839 | 1991-05-01 | 2850 | NULL |
30 |
| 8839 | AMIR | PRESIDENT | NULL | 1991-11-18 | 5000 | NULL |
10 |
| 8844 | KULDEEP | SALESMAN | 8698 | 1991-09-08 | 1500 | 0 |
30 |
| 8882 | SHIAVNSH | MANAGER | 8839 | 1991-06-09 | 2450 | NULL |
10 |
| 8886 | ANOOP | CLERK | 8888 | 1993-01-12 | 1100 | NULL |
20 |
| 8888 | SCOTT | ANALYST | 8566 | 1992-12-09 | 3000 | NULL |
20 |
| 8900 | JATIN | CLERK | 8698 | 1991-12-03 | 950 | NULL |
30 |
| 8902 | FAKIR | ANALYST | 8566 | 1991-12-03 | 3000 | NULL |
20 |
| 8934 | MITA | CLERK | 8882 | 1992-01-23 | 1300 | NULL |
10 |
+-------+-----------+-----------+------+------------+------+------+---
-----+
Question 3
Output
+----------+------+
| ENAME | SAL |
+----------+------+
| SMITH | 800 |
| ANYA | 1600 |
| SETH | 1250 |
| MOMIN | 1250 |
| AMIR | 5000 |
| KULDEEP | 1500 |
| SHIAVNSH | 2450 |
| ANOOP | 1100 |
| JATIN | 950 |
| MITA | 1300 |
+----------+------+
Question 4
Write a query to display the name, job title and salary of employee who
do not have manager.
Answer
+-------+-----------+------+
| ENAME | JOB | SAL |
+-------+-----------+------+
| AMIR | PRESIDENT | 5000 |
+-------+-----------+------+
Question 5
Answer
SELECT ENAME
FROM empl
WHERE ENAME LIKE '__A%' ;
Explanation
There are no employees whose name contains 'A' as the third alphabet
in the empl table. Therefore, the output will be empty.
Question 6
Answer
SELECT ENAME
FROM empl
WHERE ENAME LIKE '%T' ;
Output
+-------+
| ENAME |
+-------+
| SCOTT |
+-------+
Question 7
Answer
SELECT ENAME
FROM empl
WHERE ENAME LIKE 'M_L%' ;
Explanation
There are no employees whose name contains 'M' as first alphabet and
'L' as third alphabet in the empl table. Therefore, the output will be
empty.
Question 8
Write a query on the customers table whose output will exclude all
customers with a rating <= 100, unless they are located in Shimla.
Answer
SELECT *
FROM customers
WHERE rating > 100 OR city = 'Shimla' ;
Question 9
Write a query that selects all orders (Order table) except those with
zeros or NULLs in the amt field.
Answer
SELECT *
FROM order
WHERE amt IS NOT NULL AND amt <> 0 ;
Question 10
Write SQL commands for the following on the basis of given table
STUDENT :
Table : STUDENT
Same Photogra
10 7 Cricket B A
er phy
Gardenin
11 8 Sujit Tennis A C
g
Swimmi Photogra
12 7 Kamal B B
ng phy
Archa Basket
14 9 A Literature A
na Ball
Gardenin
15 10 Arpit Cricket A C
g
1. Display the names of the students who are getting a grade 'C' in
either GAME or SUPW.
2. Display the different games offered in the school.
3. Display the SUPW taken up by the students, whose name starts
with 'A'.
Answer
1.
SELECT Name
FROM STUDENT
WHERE Grade1 = 'C' OR Grade2 = 'C' ;
Output
+-------+
| Name |
+-------+
| Sujit |
| Veena |
| Arpit |
+-------+
2.
Output
+-------------+
| GAME |
+-------------+
| Cricket |
| Tennis |
| Swimming |
| Basket Ball |
+-------------+
3.
SELECT SUPW
FROM STUDENT
WHERE Name LIKE 'A%' ;
Output
+------------+
| SUPW |
+------------+
| Literature |
| Gardening |
+------------+
Question 11
Write SQL commands for the following on the basis of given table
SPORTS :
Table : SPORTS
Same Swimmi
10 7 Cricket B A
er ng
Archa Basketb
14 9 A Cricket A
na all
Athletic
15 10 Arpit Cricket A C
s
4. Display the names of the students who have grade 'C' in either
Game1 or Game2 or both.
5. Display the names of the students who have same game for both
Game1 and Game2.
6. Display the games taken up by the students, whose name starts
with 'A'.
Answer
1.
SELECT Name
FROM SPORTS
WHERE Grade1 = 'C' OR Grade2 = 'C' ;
Output
+-------+
| Name |
+-------+
| Sujit |
| Venna |
| Arpit |
+-------+
2.
SELECT Name
FROM SPORTS
WHERE Game1 = Game2 ;
Output
+-------+
| Name |
+-------+
| Venna |
+-------+
3.
Output
+------------+-----------+
| Game1 | Game2 |
+------------+-----------+
| Basketball | Cricket |
| Cricket | Athletics |
+------------+-----------+
Question 12
Write SQL commands for the following on the basis of given table
CLUB :
Table : CLUB
12 1998-01-
2 RAVINA 34 KARATE F
00 20
20 1998-02-
3 KARAN 34 SQUASH M
00 19
BASKETBA 15 1998-01-
4 TARUN 33 M
LL 00 01
SWIMMIN 75 1998-01-
5 ZUBIN 36 M
G 0 12
SWIMMIN 80 1998-02-
6 KETAKI 36 F
G 0 24
22 1998-02-
7 ANKITA 39 SQUASH F
00 20
11 1998-02-
8 ZAREEN 37 KARATE F
00 22
SWIMMIN 90 1998-01-
9 KUSH 41 M
G 0 13
BASKETBA 17 1998-02-
10 SHAILYA 37 M
LL 00 19
7. To show all information about the swimming coaches in the club.
8. To list names of all coaches with their date of appointment
(DATOFAPP) in descending order.
9. To display a report, showing coachname, pay, age and bonus (15%
of pay) for all the coaches.
Answer
1.
SELECT *
FROM CLUB
WHERE SPORTS = 'SWIMMING' ;
Output
+----------+-----------+-----+----------+-----+-----+------------+
| COACH_ID | COACHNAME | AGE | SPORTS | PAY | SEX | DATOFAPP |
+----------+-----------+-----+----------+-----+-----+------------+
| 5 | ZUBIN | 36 | SWIMMING | 750 | M | 1998-01-12 |
| 6 | KETAKI | 36 | SWIMMING | 800 | F | 1998-02-24 |
| 9 | KUSH | 41 | SWIMMING | 900 | M | 1998-01-13 |
+----------+-----------+-----+----------+-----+-----+------------+
2.
SELECT COACHNAME, DATOFAPP
FROM CLUB
ORDER BY DATOFAPP DESC ;
Output
+-----------+------------+
| COACHNAME | DATOFAPP |
+-----------+------------+
| KETAKI | 1998-02-24 |
| ZAREEN | 1998-02-22 |
| ANKITA | 1998-02-20 |
| KARAN | 1998-02-19 |
| SHAILYA | 1998-02-19 |
| RAVINA | 1998-01-20 |
| KUSH | 1998-01-13 |
| ZUBIN | 1998-01-12 |
| TARUN | 1998-01-01 |
| KUKREJA | 1996-03-27 |
+-----------+------------+
3.
Output
+-----------+------+-----+--------+
| COACHNAME | PAY | AGE | BONUS |
+-----------+------+-----+--------+
| KUKREJA | 1000 | 35 | 150.00 |
| RAVINA | 1200 | 34 | 180.00 |
| KARAN | 2000 | 34 | 300.00 |
| TARUN | 1500 | 33 | 225.00 |
| ZUBIN | 750 | 36 | 112.50 |
| KETAKI | 800 | 36 | 120.00 |
| ANKITA | 2200 | 39 | 330.00 |
| ZAREEN | 1100 | 37 | 165.00 |
| KUSH | 900 | 41 | 135.00 |
| SHAILYA | 1700 | 37 | 255.00 |
+-----------+------+-----+--------+
Question 13
Write SQL commands for the following on the basis of given table
STUDENT1 :
Table : STUDENT1
Divaka
2 450.00 Commerce 89.2 A 11C
r
Humanitie
4 Arun 350.00 73.1 B 12C
s
Nonmedic
5 Sabina 500.00 90.6 A 11A
al
Humanitie
7 Robert 250.00 64.4 C 11A
s
Nonmedic
8 Rubina 450.00 88.5 A 12A
al
Nonmedic
9 Vikas 500.00 92.0 A 12A
al
SELECT *
FROM STUDENT1
WHERE Stream = 'Nonmedical' ;
Output
+-----+--------+---------+------------+---------+-------+-------+
| No. | Name | Stipend | Stream | AvgMark | Grade | Class |
+-----+--------+---------+------------+---------+-------+-------+
| 5 | Sabina | 500 | Nonmedical | 90.6 | A | 11A |
| 8 | Rubina | 450 | Nonmedical | 88.5 | A | 12A |
| 9 | Vikas | 500 | Nonmedical | 92.0 | A | 12A |
+-----+--------+---------+------------+---------+-------+-------+
2.
SELECT Name
FROM STUDENT1
WHERE Class LIKE '12%'
ORDER BY Stipend ;
Output
+--------+
| Name |
+--------+
| Divya |
| Mohan |
| Arun |
| Karan |
| John |
| Rubina |
| Vikas |
+--------+
3.
SELECT *
FROM STUDENT1
ORDER BY AvgMark DESC ;
Output
+-----+---------+---------+------------+---------+-------+-------+
| No. | Name | Stipend | Stream | AvgMark | Grade | Class |
+-----+---------+---------+------------+---------+-------+-------+
| 9 | Vikas | 500 | Nonmedical | 92.0 | A | 12A |
| 5 | Sabina | 500 | Nonmedical | 90.6 | A | 11A |
| 2 | Divakar | 450 | Commerce | 89.2 | A | 11C |
| 8 | Rubina | 450 | Nonmedical | 88.5 | A | 12A |
| 1 | Karan | 400 | Medical | 78.5 | B | 12B |
| 6 | John | 400 | Medical | 75.4 | B | 12B |
| 4 | Arun | 350 | Humanities | 73.1 | B | 12C |
| 3 | Divya | 300 | Commerce | 68.6 | C | 12C |
| 10 | Mohan | 300 | Commerce | 67.5 | C | 12C |
| 7 | Robert | 250 | Humanities | 64.4 | C | 11A |
+-----+---------+---------+------------+---------+-------+-------+
4.
Output
+---------+---------+------------+----------------+
| Name | Stipend | Stream | Yearly_Stipend |
+---------+---------+------------+----------------+
| Karan | 400 | Medical | 4800 |
| Divakar | 450 | Commerce | 5400 |
| Divya | 300 | Commerce | 3600 |
| Arun | 350 | Humanities | 4200 |
| Sabina | 500 | Nonmedical | 6000 |
| John | 400 | Medical | 4800 |
| Robert | 250 | Humanities | 3000 |
| Rubina | 450 | Nonmedical | 5400 |
| Vikas | 500 | Nonmedical | 6000 |
| Mohan | 300 | Commerce | 3600 |
+---------+---------+------------+----------------+
Question 14
Consider the table Student1 of Q. 13. Give the output of following SQL
statement :
2.
Output
+----------------+
| ROUND(AvgMark) |
+----------------+
| 78 |
| 73 |
| 75 |
+----------------+
3.
Output
+----------------------+
| CONCAT(Name, Stream) |
+----------------------+
| RubinaNonmedical |
| VikasNonmedical |
+----------------------+
4.
Output
+------------------+
| RIGHT(Stream, 2) |
+------------------+
| al |
| ce |
| ce |
| es |
| al |
| al |
| es |
| al |
| al |
| ce |
+------------------+
Question 15
Table : STUDENT
No Stipen AvgMar Grad
Name Stream Class
. d k e
Divaka
2 450.00 Commerce 89.2 A 11C
r
Humanitie
4 Arun 350.00 73.1 B 12C
s
Nonmedic
5 Sabina 500.00 90.6 A 11A
al
Humanitie
7 Robert 250.00 64.4 C 11A
s
Nonmedic
8 Rubina 450.00 88.5 A 12A
al
Nonmedic
9 Vikas 500.00 92.0 A 12A
al
1.
Output
+--------------+
| MIN(AvgMark) |
+--------------+
| 64.4 |
+--------------+
2.
Output
+--------------+
| SUM(Stipend) |
+--------------+
| 1150 |
+--------------+
3.
Output
+--------------+
| AVG(Stipend) |
+--------------+
| 475 |
+--------------+
Write SQL commands for the following on the basis of given table MOV
Table : MOV
N Ratin Qt Pric
Title Type Stars
o g y e
69.9
2 Friday the 13th Horror R Jason 2
5
49.9
3 Top Gun Drama PG Cruise 7
5
Comed 29.9
4 Splash PG13 Hanks 3
y 5
Independence 19.9
5 Drama R Turner 3
Day 5
Comed 44.9
6 Risky Business R Cruise 2
y 5
Amech 31.9
7 Cocoon Scifi PG 2
e 5
Comed 69.9
8 Crocodile Dundee PG13 Harris 2
y 5
Comed 59.9
9 101 Dalmatians G 3
y 5
Comed Hoffma 29.9
10 Tootsie PG 1
y n 5
22. Display a list of all movies with Price over 20 and sorted by
Price.
23. Display all the movies sorted by QTY in decreasing order.
24. Display a report listing a movie number, current value and
replacement value for each movie in the above table. Calculate
the replacement value for all movies as : QTY * Price * 1.15.
Answer
1.
SELECT Title
FROM MOV
WHERE Price > 20
ORDER BY Price ;
Output
+--------------------+
| Title |
+--------------------+
| Splash |
| Tootsie |
| Cocoon |
| Gone with the Wind |
| Risky Business |
| Top Gun |
| 101 Dalmatians |
| Friday the 13th |
| Crocodile Dundee |
+--------------------+
2.
SELECT Title
FROM MOV
ORDER BY Qty DESC ;
Output
+--------------------+
| Title |
+--------------------+
| Top Gun |
| Gone with the Wind |
| Splash |
| Independence Day |
| 101 Dalmatians |
| Friday the 13th |
| Risky Business |
| Cocoon |
| Crocodile Dundee |
| Tootsie |
+--------------------+
3.
Output
+--------------+---------------+--------------------+
| Movie_Number | Current_Value | Replacement_Value |
+--------------+---------------+--------------------+
| 1 | 39.95 | 183.77000350952147 |
| 2 | 69.95 | 160.884992980957 |
| 3 | 49.95 | 402.09750614166256 |
| 4 | 29.95 | 103.3275026321411 |
| 5 | 19.95 | 68.8275026321411 |
| 6 | 44.95 | 103.38500175476074 |
| 7 | 31.95 | 73.48500175476073 |
| 8 | 69.95 | 160.884992980957 |
| 9 | 59.95 | 206.8275026321411 |
| 10 | 29.95 | 34.44250087738037 |
+--------------+---------------+--------------------+
INDIRA GANDHI
VS
SHRI RAJ NARAIN
MC MEHTA
VS
UNION OF INDIA
JESSICA LAL
MURDER CASE
RAJAGOPAL
VS
STATE OF TAMIL NADU