Parikshit TASK 1
Parikshit TASK 1
DHRUV SARAOGI
Python Basics
EASY
1. Calculate the Area of a Circle
Write a Python program that calculates the area of a circle given its radius.
o Input:
5
o Output:
78.54
(Hint: Use the formula: πr²)
import math
pi=math.pi
a=int(input("Enter the radius of the circle: "))
area=pi*a**2
print("The area of the circle is: %.2f" %area)
o Input:
0
o Output:
32
(Hint: Use the formula: (C × 9/5) + 32)
MEDIUM
3. Remove Duplicates from a List
Write a Python program that removes duplicate elements from a list of integers.
o Input:
[1, 2, 2, 3, 4, 4, 5]
o Output:
[1, 2, 3, 4, 5]
n=int(input("Enter the number of elements in the list: "))
list1=[]
for i in range(0,n):
x=int(input("Enter the element: "))
list1.append(x)
set1=set(list1)
list1=list(set1)
print("The list after removing duplicates is: ",list1)
o Input:
"Python Programming"
o Output:
4
n="aeiouAEIOU"
s=input("Enter a string:")
count=0
for i in s:
if i in n:
count+=1
print("Number of vowels in the string is:",count)
o Input:
[12, 45, 67, 23, 89, 90]
o Output:
89
o Input:
"hello world from python"
o Output:
"Hello World From Python"
7. Reverse a String
Write a Python program to reverse a given string.
o Input:
"Innovation"
o Output:
"noitavonnI"
HARD
8. Palindrome Check
Write a Python program to check if a given string is a palindrome (reads the same forward
and backward).
o Input:
"madam"
o Output:
True
n=input("Enter a string: ")
print(n[::-1]==n)
o Input:
20
o Output:
[2, 3, 5, 7, 11, 13, 17, 19]
Input:
7
Output:
[0, 1, 1, 2, 3, 5, 8]
Input:
List: [1, 3, 5, 7, 9, 11, 13]
Target: 9
Output:
Found at index 4
Input:
5
Output:
120
def factorial(n):
if n==0:
return 1
else:
return n*factorial(n-1)
Input:
[[1, 2, 3], [4, 5, 6]]
Output:
[[1, 4], [2, 5], [3, 6]]
import numpy as np
n=int(input("Enter the size of the matrix(rows)"))
m=int(input("Enter the size of the matrix(columns)"))
matrix=np.zeros((n,m),dtype='int64')
mt=np.zeros((m,n),dtype='int64')
for i in range(0,n):
for j in range(0,m):
matrix[i][j]=int(input())
print(matrix)
for i in range(m):
for j in range(n):
mt[i][j]=matrix[j][i]
print(mt)
Output:
"Exploration"
n=input("enter a string")
s=n.split(" ")
max=0
word=""
for i in s:
if len(i)>max:
max=len(i)
word=i
print(word)
Input:
"(())"
Output:
True
Input:
"(()"
Output:
False
n=input("enter a string")
count =0
for chr in n:
if chr=="(":
count+=1
elif chr==")":
count-=1
if count == 0:
print(True)
else:
print(False)
Pandas and Numpy
EASY
1. Create a NumPy Array
Write a Python program to create a NumPy array with values ranging from 1 to 10.
o Input: None
o Output:
o array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
import numpy as np
a=np.arange(1,11)
print(a)
Given the DataFrame below, write a Pandas program to select the Name column.
o Input:
Python
df = pd.DataFrame({
30, 35],
})
o Output:
o 0 Alice
o 1 Bob
import pandas as pd
df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30,
35],
'City': ['New York', 'Los Angeles', 'Chicago']})
print(df['Name'])
3. Create a DataFrame from a Dictionary
Write a Python program to create a Pandas DataFrame from the dictionary below.
o Input:
Python
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
o Output:
o ABC
o 0147
o 1258
o 2369
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame (data)
print(df)
MEDIUM
4. Find the Mean of a NumPy Array
o Input:
o Output:
o 30.0
import numpy as np
a=np.array([10,20,30,40,50])
print(a.sum()/len(a))
Given the DataFrame below, write a Pandas program to filter rows where the Age is greater than 30.
o Input:
Python
df = pd.DataFrame({
30, 35],
})
o Output:
2 Charlie 35 Chicago
o
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
})
print(df[df['Age']>30])
Write a Python program to generate a 3x3 matrix of random values between 0 and 1 using NumPy.
o Input: None
import numpy as np
a=np.random.random((3,3))
print(a)
Given the DataFrame below, write a Pandas program to calculate the sum of each column.
o Input:
Python
2, 3],
'C': [7, 8, 9]
})
o Output:
o A 6
o B 15
o C 24
o dtype: int64
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame (data)
df.sum()
Write a Pandas program to replace missing values (NaN) in the DataFrame with the mean of the
respective column.
o Input:
Python
np.nan, 3],
'C': [7, 8, 9]
})
o Output:
o A BC
o 0 1.0 4.0 7
o 1 2.0 5.0 8
o 2 3.0 4.5 9
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, np.nan, 3],
'B': [4, 5, np.nan],
'C': [7, 8, 9]
})
df ['A'].fillna (df['A'].mean(), )
df ['B'].fillna (df['B'].mean(), )
df ['C'].fillna (df['C'].mean(), )
df
HARD
9. Find the Dot Product of Two NumPy Arrays
Write a Python program to calculate the dot product of two NumPy arrays.
o Input:
Python
o Output:
o 32
import numpy as np
array_1 = np.array([1, 2, 3])
array_2 = np.array([4, 5, 6])
a=np.dot(array_1,array_2)
print(a)
Given the DataFrame below, write a Pandas program to group by the City column and calculate the
mean Age for each city.
Input:
Python
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 30,
35, 40],
})
Output:
Age
City
Chicago 35.0
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 30, 35, 40],
'City': ['New York', 'Los Angeles', 'Chicago', 'New York']
})
result = df.groupby('City')['Age'].mean()
print(result)
Write a Python program to apply a custom function to the Age column of the DataFrame below to
convert the ages from years to months.
Input:
Python
df = pd.DataFrame({
30, 35]
})
def years_to_months(age):
return age * 12
Output:
Name Age
0 Alice 300
1 Bob 360
2 Charlie 420
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
})
def years_to_months(age):
return age * 12
df['Age'] = df['Age'].apply(years_to_months)
print(df)
Input:
Python
Output:
AB
013
124
057
168
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
df3 = pd.concat([df1, df2])
print(df3)
Write a Python program to find the eigenvalues and eigenvectors of a 2x2 matrix using NumPy.
Input:
Python
Output:
[[ 0.894, -0.707],
[ 0.447, 0.707]]
import numpy as np
matrix = np.array([[4, 2], [1, 3]])
u,v=np.linalg.eig(matrix)
Input:
Python
Output:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
import numpy as np
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
a = array.reshape(3, 3)
print(a)
Given the DataFrame below, write a Pandas program to pivot the DataFrame so that the Name column
becomes the index, and the Subject column values become columns with the corresponding Score.
Input:
Python
df = pd.DataFrame({
})
Output:
Math Science
Name
Alice 85 92
Bob 78 81
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Alice', 'Bob'],
'Subject': ['Math', 'Math', 'Science', 'Science'], 'Score': [85, 78,
92, 81]
})
pivot_df = df.pivot(index='Name', columns='Subject', values='Score')
print(pivot_df)