Python Practical Practice Questions
Python Practical Practice Questions
S. Experiment
No.
1 Programs on the Basic Concepts of Python
if int(percentage)>=90:
print('your grade is A') elif
int(percentage)>=80 and
int(percentage)<=90:
print('your grade is B') elif
int(percentage)>=70 and
int(percentage)<=80:
print('your grade is C') elif
int(percentage)>=60 and
int(percentage)<=70:
print('your grade is D') elif
int(percentage)>=50 and
int(percentage)<=60:
print('your grade is E') elif
int(percentage)>=40 and
int(percentage)<=50:
print('your grade is F')
else:
print('you are fail')
40 your grade
is F
1 2 3
4
5
1
2
3 4
5
6
7
8
9
10
1
2 3 4
5
6
7
8
9
10
21 3 2 4 123
123
12 34 21 32 1
The maximum number is b = 34
12 32 1 -2 3
-2
1505
1540
1575
1610
1645
1680
1715
1750
1785
1820
1855
1890
1925
1960
1995
2030
2065
2100
2135
2170
2205
2240
2275
2310
2345
2380
2415
2450
2485
2520
2555
2590
2625
2660
2695
3 is 37.4 in fahrenheit
45 is 7.222222222222222 in celsius
0
1
2
4
5
1
1
2
3
5
8
13
21
34
55
4
29.0
12
123
1234
12345
In [11]: for i in range (1,6):
for j in range (1, i+1):
print(j, end=' ')
print()
1
Question 2: Print the Following patterns
45
345
2345
12345
In [12]: for i in range(5, 0, -1):
for j in range(i, 6):
print(j, end=' ' )
print()
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
5
54
543
5432
54321
In [26]: for i in range (5, 0, -1):
for j in range (5, i-1, -1):
print(j, end=' ') print()
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
22
333
4444
55555
for i in range(1, 6):
In [24]:
for j in range(i):
print(i, end=' ')
print()
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
---12
--123
-1234
12345
In [29]: for i in range(1,6):
print(' '* (5-i), end= ' ')
for j in range(1, i+1):
print(j, end ='')
print()
1
12
123
1234
12345
----1
---121
---12321
-1234321
123454321
In [27]:
i = 1 while i<6:
print(" "* (5-i), end = '')
j = 1 while j<(i+1):
print(j, end =
'') j += 1
k = 1 while k < i:
print(i-k, end = '')
k += 1 print()
i += 1
1
121
12321
1234321
123454321
1
121
12321
1234321
123454321
123454321
-1234321
--12321
---121
----1
In [31]: i = 6
while
(i>0):
print(" "* (6-i),
end='') j = 1
while j<i:
print(j, end = "")
j += 1
k = 1
while k<(i-1):
print(i-k-1, end =
"") k += 1
print() i -= 1
123454321
1234321
12321
121
1
123454321
1234321
12321
121
1
ABC
ABCD
ABCDE
from string import *
In [33]:
for i in range(0, 5):
for j in range(0, i+1):
print(ascii_uppercase[j], end= " ")
print()
A
A B
A B C
A B C D
A B C D E
In [ ]:
b = 12 is the greatest
a = -2 is the greatest
def sum(m):
if (type(m) == list):
sum = 0 for i in m: sum
+= i print("The sum of the elements in this
list is:", sum) else:
print("Wrong Input! Enter a
list.") a = list(range(10)) b = 5
sum(b) print() sum(a)
0! = 1
while (a != 0):
rem = a%10 p =
(p * 10) + rem
a = a//10
if (p
== m):
print("Number %s is a
Palindrome."%str(p)) else:
print("Number %s is not a
Palindrome."%str(p
)) pdm(12321) print() pdm(123) print()
pdm(0)
Number 0 is a Palindrome.
(1+2+4+7+14+28)/2 = 28
In [61]: def test_perfect(a):
sum = 0 for i
in range(1, a):
if (a%i == 0):
sum += i if (sum ==
a):
print("Number %s is a Perfect Number."%str(a))
else:
print("Number %s is not a Perfect Number."%str(a))
test_perfect(6)
print()
test_perfect(496)
print()
test_perfect(8128)
print()
test_perfect(7)
In [ ]:
Experiment 5 : Programs demonstrating the
Concept of Lists
length of list a is : 5
[1, 2, 6]
Elements with index value 0 are not same in both the lists
Elements with index value 1 are not same in both the lists
Elements with index value 2 are same in both the lists
2 occurs 3 times
<ipython-input-75-57b2145c2d44> in len_element(a)
1 def len_element(a):
2 for i in a:
----> 3 print("length of %s is:"%str(i), len(i), "\n")
4 x = ["Codeblocks", "Jupyter"]
5 n = ["JS", 1, "Android"]
Yes, they are the same for all strings. The function doesn't
work for integers and float values.
import string a = "africa"
string.join(string.split(a)) ==
a
In [5]: song = "Africa"
song.join(song.split()) == song
Out[5]:
True
For example:
Input: 1 1 1 1 1 2 2 2
Output: 1 2
In [79]: a = input("Enter numbers here, seperated by a space:
").split() lst_new = [] for i in a: if i not in
lst_new: lst_new.append(i)
print("The distinct numbers from the input are:", "
".join([str(i)])) print(lst_new)
SAMPLE:
Input: 2 5 2 5 8 8 10 8 2 4 4 5 2
5 occurs 3 times
8 occurs 3 times 3
10 occurs 1 times
4 occurs 2 times
In [85]:
def counter(): a = input("Enter numbers
between 1 to 20: ").split() lst = [] for
i in a: if i not in lst:
lst.append(i)
for i in lst:
count = 0 for j
in a: if (j
== i):
count += 1
print(i, "occurs %s times"%str(count))
counter()
In [ ]:
In [ ]: a=(2,)+a[1:] a
In [ ]: print(len(a))
In [ ]: b+c
[0, 1, 2, 4]
(0, 1, 2, 4)
8 - Reverse a tuple
In [ ]: t7=(1,2,3,4,'nancy')
l7=list(t7)
l7.reverse()
b=tuple(l7)
print(b)
In [ ]:
key is present
key is not
present
22 , 21 , 23 , 22 , 24 , 23 , 25 , 24 , 26 , 25 , 27 , 26 , 28 ,
27 , 29
, 28 , 30 , 29 , 31 , 30 , 32 , 31 , 33 , 32 , 34 , 33 , 35 , 34
, 36 , 3
5 , 37 , 36 , 38 , 37 , 39 , 38 , 40 , 39 , 41 , 40 , 42 , 41 ,
43 , 42 ,
44 , 43 , 45 , 44 , 46 , 45 , 47 , 46 , 48 , 47 , 49 , 48 , 50 ,
49 , 51 , 50 , 52 , 51 , 53 , 52 , 54 , 53 , 55 , 54 , 56 , 55 ,
57 , 56 , 58 , 5
7 , 59 , 58 , 60 , 59 , 61 , 60 , 62 , 61 , 63 , 62 , 64 , 63 ,
65 , 64 ,
66 , 65 , 67 , 66 , 68 , 67 , 69 , 68 , 70 , 69 , 71 , 70 , 72 ,
71 , 73 , 72 , 74 , 73 , 75 , 74 , 76 , 75 , 77 , 76 , 78 , 77 ,
79 , 78 , 80 , 7 9 ,
53, 53, 40, 40, 27, 27, 14, 14, 1, 1, -12, -12, -25, -25, -38, -
38, -51, -51, -64, -64, -77, -77, -90, -90, -103, -103, -116, -
116, -129, -129, -1
42, -142, -155, -155, -168, -168, -181, -181, -194, -194, -207, -
207, -22
0, -220, -233, -233, -246, -246, -259, -259, -272, -272, -285, -
285, -29
8, -298, -311, -311, -324, -324, -337, -337, -350, -350, -363, -
363, -37
6, -376, -389, -389, -402, -402, -415, -415, -428, -428, -441, -
441, -45
4, -454, -467, -467, -480, -480, -493, -493, -506, -506, -519, -
519, -53 2, -532, -545, -545,
14 , 28 , 20 , 40 , 32 , 64 , 50 , 100 ,
In [ ]:
b) Multiplication
c) Transpose
In [36]: import numpy as np #Creating Arrays r1 =
int(input("Enter row number of array1 : "))
c1 = int(input("Enter column number of array1
: ")) print()
a1 = np.zeros([r1, c1], dtype
= int) for i in range(r1):
for j in range(c1):
a1[i, j] = int(input("Enter value here: "))
Array is [1 2 4]
Key 2 is found at index 1
Key 6 is not found
[ 0 1 2 4 4 5 6 9 53 56]
<class 'numpy.ndarray'>
Key 70 is not Found
Key 4 is found at index 4
In [ ]:
Rollno
Name
Marks
1 John PWP 20
and so on...
In [9]: df
1 s1 PWP 95.0
2 s2 OOC 94.0
3 s3 OOC 92.5
4 s4 PWP 99.0
5 s5 PWP NaN
6 s6 MATH 92.0
7 s7 PWP 94.0
8 s8 OOC 95.0
9 s9 OOC 97.0
10 s10 IIS 96.0
In [10]: df = df.drop(0)
In [11]: df
Out[11]: StudentName Subject Marks
1 s1 PWP 95.0
2 s2 OOC 94.0
3 s3 OOC 92.5
4 s4 PWP 99.0
5 s5 PWP NaN
6 s6 MATH 92.0
7 s7 PWP 94.0
8 s8 OOC 95.0
9 s9 OOC 97.0
10 s10 IIS 96.0
In [13]:
# Maximum Marks
column =
df["Marks"]
maxMarks = column.max()
maxIndex = column.idxmax()
name =
df['StudentName'][maxIndex]
print("Maximum Marks obtained are:", maxMarks, "of Roll No.
%d"%maxIndex
# Minimum Marks column = df["Marks"] minMarks = column.min()
minIndex = column.idxmin() name = df['StudentName'][minIndex]
print("\n\nMinimum Marks obtained are:", minMarks, "of Roll No.
%d"%minIn
In [15]: df
Out[15]: StudentName Subject Marks
1 s1 PWP 95.0
2 s2 OOC 94.0
3 s3 OOC 92.5
4 s4 PWP 99.0
5 s5 PWP NaN
6 s6 MATH 92.0
7 s7 PWP 94.0
8 s8 OOC 95.0
9 s9 OOC 97.0
10 s10 IIS 96.0
In [16]: # Students who got A+ Grade i.e, more than 95
marks temp = df[df['Marks'] > 95]
print("Students who got A+ grade are: ")
print(temp.StudentName)
Out[19]: 94.0 2
95.0 2
96.0 1
97.0 1
92.0 1
99.0 1
92.5 1
Name: Marks, dtype: int64
1 s1 PWP 95.0
2 s2 OOC 94.0
3 s3 OOC 92.5
4 s4 PWP 99.0
6 s6 MATH 92.0
7 s7 PWP 94.0
8 s8 OOC 95.0
9 s9 OOC 97.0
10 s10 IIS 96.0
Question 2:-
In [23]: import pandas as pd
deli=pd.read_csv('C:/deliveries.csv')
Out[29]: batsman
V Kohli 5434
SK Raina 5415
RG Sharma 4914
DA Warner 4741
S Dhawan 4632
Name: batsman_runs, dtype: int64
In [ ]: