Ekansh Practical File XII-A

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

INFORMATICS PRACTICES – PRACTICAL FILE

1. Write a program to read two lists, L1 and L2 from user. Now merge L1 and L2 and
print “mostly odd” if merged list contains more odd numbers than even numbers
else it should print “mostly even”
L1=eval(input("Enter list 1"))
L2=eval(input("Enter list 2"))
L=L1+L2
m=0
n=0
for i in L:
if i%2==0:
n=n+1
else:
m=m+1
if n>m:
print("Mostly Even")
else:
print("Mostly Odd")

Output

1|Page Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE
2. Write a menu driven program which takes a string as input and as per user choice
perform the respective operations.

Code:

while True:
s= input("Enter the string")
print("1. To count the no. of uppercase and lowercase alphabets.")
print("2. To check if the string is palindromic")
print("3. To check repeating characters (yes/no)")
print("4. To count special characters (any non alphanumeric character is special")
print("5. Exit")
ch=int(input("Enter the Choice"))
if ch==1:
u=0
l=0
for i in s:
if i.isupper():
u+=1
elif i.islower():
l+=1
print("UpperCase=", u)
print("LowerCase=", l)
elif ch ==2:
if(s == s[:: - 1]):
print("It is palindromic")
else:
print("It is not palindromic")
elif ch==3:
duplicate=[]
for a in s:
if s.count(a)>1:
print('Yes',a ,"is the repeating
character")
elif ch==4:
for b in s:
if not(b.isalnum()):
print ("Special Characters:" , b)
elif ch==5:
break

3. Write a python program to input ‘n’ names and phone numbers to store it in a
dictionary with names as keys paired with their phone numbers as values. Your code
should now print the following:

2|Page Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE
a. The Phone Number of the person whose name is entered by the user.
b. The Name of the person whose phone is entered by the user.

Code:
n=int(input("Enter the number of entries"))
d={}
for i in range(n):
name=input("Enter the name")
pno= int(input("Enter the Phone Number"))
d[name]=pno
print(d)
print("1. Search Name")
print("2. Search Phone Number")
ch=int(input("Enter the Choice"))
if ch==1:
name1=input("Enter the Name of the Person whose number is to be searched")
for j in d:
if j==name1:
print(d[name])
elif ch==2:
pnon=int(input("Enter the Ph No. to find the name"))
for a,b in d.items():
if b==pnon:
print(a)
else:
print("Invalid Input")

Choice 1:- Choice 2:-

3|Page Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE
4. Write a program to read a sentence from the user and create a dictionary with each
word as a key and its frequency (no of occurrences) of that word as value. For
example, if sentence read is
“It is true that the joy of giving is true joy’
Then dictionary should be
{“It”:1, “is”:2,”true”:2.”that”:1,“the”:1, “joy”:2,”of ”:1,”giving”:1}
Code:
x=(input("enter string: "))
l=x.split()
b={}
for i in l:
if i in b:
b[i]+=1
else:
b[i]=1
print(b)

Output:

4|Page Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE
5. Three Series objects stores the marks of 10 students in 3 terms. Roll numbers of
students form the index of these Series objects .The 3 Series objects have the same
indexes. calculate the total weighted marks obtained by students as per the following
formula :
Final_marks=25% term1 + 25% term2+ 50% term3
Store the final marks of students in another series object.
Perform the following operations on the final marks series:
a. Display the top 3 marks in the series
b. Display the bottom 3 marks in the series
c. Display the average marks of students
d. Display the maximum marks in the series
e. Display the minimum marks in the series
f. Display the marks which are greater than 90
import pandas as pd
import random
roll_number=[1,2,3,4,5,6,7,8,9,10]
term1 = pd.Series([random.randint(0,100) for x in range(10)], index=roll_number)
term2 = pd.Series([random.randint(0,100) for x in range(10)], index=roll_number)
term3 = pd.Series([random.randint(0,100) for x in range(10)], index=roll_number)

Final_Marks = pd.Series(0.25*term1 + 0.25*term2 + 0.5*term3)


print(Final_Marks)
print('Top 3 Marks' , Final_Marks.head(3) , sep="\n")
print('Bottom 3 Marks' , Final_Marks.tail(3) , sep="\n") Output:
print('Mean is' , Final_Marks.mean())
print('Maximum Marks is' , Final_Marks.max())
print('Minimum Marks is' , Final_Marks.min())
print('Marks greater>90' Final_Marks[Final_Marks>80])

5|Page Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE

6. Series objects week1, week2, week3, week4 store the temperatures of days of 4 weeks
of a month respectively. Write a script to:
a) Print the average temperature per week
b) Store the average temperature of each day for 4 weeks in another series
AVG_TEMP.
c) Print the maximum temperature from the AVG_TEMP Series
d) Print the minimum temperature from the AVG_TEMP Series
e) Print the days where temperature is above 35 degrees.
days=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
week1=pd.Series(data=[34.5,44.3,68.4,46.5,33.4,34.9,45.6],index=days)
week2=pd.Series(data=[36.5,43.7,69,41.5,31.4,38.9,35.7],index=days)
week3=pd.Series(data=[36.5,44.3,63.4,42.5,39.4,32.9,48.6],index=days)
week4=pd.Series(data=[31.5,54.3,35.4,46.5,33.4,34.9,45.6],index=days)
print('Average of week 1' , week1.mean())
print('Average of week 2' , week2.mean())
print('Average of week 3' , week3.mean())
print('Average of week 4' , week4.mean())
Avg_Temp=(week1+week2+week3+week4)/4
print('average temperature of each day for 4 week:\n',Avg_Temp)
print('maximum temperature :',Avg_Temp.max())
print('Minimum Temperature :',Avg_Temp.min())
print('days where temperature is above 35 degrees')
Avg_Temp[Avg_Temp>35]
f)

6|Page Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE
7. Create a dataframe “temp” from the four series created above -week1, week2, week3
and week4. The indexes should be ‘sun’, ‘mon’ ….’sat’ and columns should be
week1, week2,week3 and week4. Write a script to calculate:.
a) Average temperatures for each day of the week i.e. average temperature for
Monday, Tuesday etc.
b) Average temperature per week
c) Average temperature for whole month
temp=pd.DataFrame({'week1':week1,'week2':week2,'week3':week3,'week4':week4})
print('Average temperatures for each day')
print(temp.mean(axis=1))
print('Avg Temperature per week', temp['week1'].mean())
temp['Total']=temp.sum(axis=1)
avg=temp.Total.sum()/temp.size
print('Average Temperature for Whole Week', avg)

Output:

7|Page Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE
8. Given a data frame DATA. Write code statement to:

Company Quantity Price


Printer HCL 32 18000
Printer Lenovo 7 12000
Scanner HCL 12
Monitor Asus 46 15000
Scanner Samsung 23 17000

a) Find all rows with label “Printer”. Extract all columns.


b) List all the peripherals with quantity more than 30.
c) Fill Nan values with a specific value enter by user
d) List single true or false to signify if all prices are more than 15000 or not
e) List 2nd, 3rd and 4th rows.
f) List only the columns quantity and price using loc.
g) List only columns 1 and 2 (column indexes) using iloc.
h) List only rows with labels “Printer” and “Scanner” using loc.
i) Write statement to delete rows with labels “Printer” and “monitor”
j) Write statement to delete columns with labels “company” and “price”
items=['Printer','Printer','Scanner','Monitor','Scanner']
data=pd.DataFrame({'Company':pd.Series(['HCL','Lenovo','HCL','Asus','Samsung'],index=items),'Qu
antity':pd.Series([32,7,12,46,23], index=items),'Price':pd.Series([18000,12000,None,
15000,17000], index=items)})
print(data.loc['Printer'])
print(data[data['Quantity']>30])
print(data['Price']>15 000)
print(data.iloc[1:4])
print(data.loc[:,['Quantity','Price']])
print(data.iloc[:,0:2])
print(data.loc[['Printer','Scanner']])
print(data.drop(['Printer','Monitor'],inplace=True))

8|Page Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE
9. Write a python program to create a dataframe called “student” containing the following
columns:
Rollno, name,Eng, Phy, Chem, Math, IP
Input the rollno, name and marks in 5 subjects. Calculate the total and percentage. Calculate
the grade as:

Percentage Grade

91-100 A1

81-90 A2

71-80 B1

61-70 B2

51-60 C1

41-50 C2

33-40 D

<40 E

All the newly created columns (totalmarks, per, grade) should be inserted in the dataframe.
Print the final dataframe.
f={'Roll_No':pd.Series([1,2,3,4,5]),'Name':pd.Series(['Rohan','Anuj','Ekansh','Anshul','Mehul']),'Eng':
pd.Series([67,78,98,75,35]),'Phy':pd.Series([56,78,86,67,78]),'Chem':pd.Series([65,78,70,57,89]),'Mat
h':pd.Series([75,86,58,79,90]),'IP':pd.Series([86,94,76,93,76])}
student=pd.DataFrame(df)
tot=[]
per=[]
grade=[]
for (x,y) in student.iterrows():
total=y['Eng']+y['Phy']+y['Chem']+y['Math']+y['IP']
tot.append(total)
avg_marks=total/5
per.append(avg_marks)
if avg_marks>=91:
grade.append('A1')
elif avg_marks>=81:
grade.append('A2')
elif avg_marks>=71:
grade.append('B1')
elif avg_marks>=61:
grade.append('B2')

9|Page Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE
elif avg_marks>=51:
grade.append('C1')
elif avg_marks>=41:
grade.append('C2')
elif avg_marks>=33:
grade.append('D')
else:
grade.append('E')
student['Total']=tot
student['Percentage']=per
student['Grade']=grade
print(student)

10 | P a g e Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE
10. Write a Python program to read a given CSV file “country.csv” into the dataframe and
display the data.
Sample Output
country_id country_name region_id
AR Argentina 2
AU Australia 3
BE Belgium 1
BR Brazil 2
CA Canada 2

11 | P a g e Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE
11. Write a Python program to convert the dataframe given below into a Dept.CSV file and
print the content of the file.
Sample Output
ID Department Name
10 Administration
20 Marketing
30 Purchasing
40 Human Resources
50 Shipping
60 IT

12 | P a g e Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE

13 | P a g e Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE

14 | P a g e Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE

15 | P a g e Name- Ekansh Girdhar XII-A


INFORMATICS PRACTICES – PRACTICAL FILE

16 | P a g e Name- Ekansh Girdhar XII-A

You might also like