Python Practical Imp Questions
Python Practical Imp Questions
DATA SCIENCE
Output:
[1, 'a', 3, 4, 5]
import numpy as np
import statistics as stat
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
mn = np.mean(speed)
mdn = np.median(speed)
md = stat.mode(speed)
print(mn)
print(mdn)
print(md)
Output:
89.76923076923077
87.0
86
Output
6. Read csv file saved in your system and display its information
import pandas as pd
df=pd.read_csv(‘C:\JaipurFinalCleanData.csv’)
print(df.head(10))
print(df.shape)
print(df.dtypes)
Output:::
7. Program to Plot a pie chart for marks scored in various subjects
from matplotlib import pyplot as pltimport
numpy as np marks=[23,45,34,41,13,49,78]
subjects=["Eng","Hindi","Sanskrit","Maths", "Science","Social","AI"]
plt.pie(marks,labels=subjects, autopct='%1.2f%%')
plt.show()
Output::::
COMPUTER VISION
8. Write a program to read an image and display using Python
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('c:\man.jpg')
plt.imshow(img)
plt.title('Man')
plt.axis('off')
plt.show()
Output::::
9. Write a program to read an image and identify its shape using Python
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('c:\man.jpg')
plt.imshow(img)
plt.title('Man')
plt.axis('off')
plt.show()
print(img.shape)
Output:::
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('man.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.title('Man')
plt.axis('off')
plt.show()
Output::
11. Write a program to crop an image
import cv2
from matplotlib import pyplot as plt
import numpy as np
img = cv2.imread('man.jpg')
roi = img[1500:2500,1000:2000]
plt.imshow(cv2.cvtColor(roi, cv2.COLOR_BGR2RGB))
plt.title('man')
plt.axis('off')
plt.show()
Output:::
ADVANCE PYTHON
12. Python program to find the factorial of a number provided by the user.
# To take input from the user
num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num+1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Output:::
Output:::
14. Python program to check if two strings are anagrams using sorted()
str1 = "Race"
str2 = "Care"
# convert both the strings into
lowercasestr1 = str1.lower()
str2 = str2.lower()
# check if length is same
if(len(str1) == len(str2)):
# sort the strings sorted_str1 = sorted(str1)sorted_str2 = sorted(str2)
# if sorted char arrays are same
if(sorted_str1 == sorted_str2):
print(str1 + " and " + str2 + " are anagram.")
else:
print(str1 + " and " + str2 + " are not anagram.")
else:
print(str1 + " and " + str2 + " are not anagram.")
Output:::
Output:::
****