0% found this document useful (0 votes)
68 views

279 Python Practicals 1to4

The document contains 8 programming problems in Python with solutions. It includes problems to print "Hello World", swap variables with and without a third variable, find the square root of a positive number, calculate the area of shapes, find the sum of natural numbers without a loop, and check arithmetic operators. The problems demonstrate basic Python concepts like input/output, variables, operators, conditional statements, and loops.

Uploaded by

Royal Empire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

279 Python Practicals 1to4

The document contains 8 programming problems in Python with solutions. It includes problems to print "Hello World", swap variables with and without a third variable, find the square root of a positive number, calculate the area of shapes, find the sum of natural numbers without a loop, and check arithmetic operators. The problems demonstrate basic Python concepts like input/output, variables, operators, conditional statements, and loops.

Uploaded by

Royal Empire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Nehal .V.

Solanki 20BECE30279

Practical Set - 1

1. Write a Python program to print “Hello World”.


Code: print("Hello World")

Output: Hello World

2. Write a Python program to swap two variables using third variable.


Code: a=int(input("A: "))

b=int(input("B: "))
temp=a
a=b

b=temp
print("A:",a,"B:",b)

Output: A: 44
B: 28
A: 28 B: 44

3. Write a Python program to swap two variables without third variable.


Code: a=int(input("A: "))
b=int(input("B: "))
(a,b)=(b,a)
print("A:",a,"B:",b)

Output: A: 10
B: 45
A: 45 B: 10

Subject: Python Programming Page |1


Nehal .V. Solanki 20BECE30279

4. Write a Python program to find square root of positive number.


Code: n=int(input("Enter Number for Square Root: "))
sqr =n ** 0.5
print(f“Square root of {n} is : {sqr}”)

Output: Enter Number for Square Root: 16


Square root of 16 is : 4

5. Write a Python program to find area of a rectangle and circle.


Code:
PI = 3.14

length = int(input("Enter Length Of Rectangle : "))

breadth = int(input("Enter Breadth Of Rectangle : "))

radius = int(input("Enter Radius Of Circle : "))

Area_Rec = length * breadth

Area_Cir = PI * radius * radius

print("Area of rectangle :",Area_Rec)

print("Area of Circle :",Area_Cir)

print("Area of Rectangle=%0.4f" %areaOfRectangle)

Output: Enter Length Of Rectangle : 12


Enter Breadth Of Rectangle : 2
Enter Radius Of Circle : 2
Area of rectangle : 24
Area of Circle : 12.56

6. Write a Python program to find sum of n natural numbers without loop.


Code: num=int(input("Enter value of N:"))
sumOfNnumber=num*(num+1)/2
print("Sum Of %d natural number = %d"%(num,sumOfNnumber))

Output: Enter value of N:10


Sum Of 10 natural number = 5

Subject: Python Programming Page |2


Nehal .V. Solanki 20BECE30279

7. Check various arithmetic operators of Python.


Code: x=int(input("Enter first Value:"))
y=int(input("Enter Second Value:"))
print("Addition:",x+y)
print("Subtraction:",x-y)
print("Multiplication:",x*y)
print("Division:",x/y)

print("Floor Division:",x//y)

Output: Enter first Value:5


Enter Second Value:2
Addition: 7
Subtraction: 3
Multiplication: 10
Division: 2.5
Floor Division: 2

8. Write a Python program to check output of modulo operator.


Code: num3 = int(input("Enter num3:"))
num4 = int(input("Enter num4:"))
print("Modulo :", num1 % num2)
print(t%w)

Output: Enter num3:23


Enter num4:6
Modulo : 5

Subject: Python Programming Page |3


Nehal .V. Solanki 20BECE30279

Practical Set –2

1. WAP to check whether entered number is even or odd.


Code: number=int(input("Enter any Natural number: "))
if (number%2)==0:
print(number," is even number.")
else:
print(number," is odd number.")

Output: Enter any Natural number: 14


14 is even number.

2. WAP to find whether entered number is positive, negative or zero


Code: number=float(input("Enter Any number: "))
if number>0:
print(number," is positive.")
elif number<0:
print(number," is negative.")
else:
print("Entered number is zero.")

Output: Enter Any number: 25


25 is positive.

3. WAP to find roots of quadratic equations if roots are real.


Code: import math

print("ax^2 + bx + c")
a=int(input("Enter a: "))
b=int(input("Enter b: "))
c=int(input("Enter c: "))
# calculate the Discriminant

Subject: Python Programming Page |4


Nehal .V. Solanki 20BECE30279

dis= (b**2) - (4*a*c)


if(dis>0):
sqrt_dis=math.sqrt(abs(dis))
print("Different Roots")
print((-b + sqrt_dis)/2*a,end="\t")
print((-b - sqrt_dis)/2*a)
elif(dis==0):
print("Same Roots",-b/2*a)

else:
print("Real root is not possible")

Output: ax^2 + bx + c
Enter a: 2
Enter b: -11

Enter c: 5
Different Roots
20.0 2.0

4. WAP to check whether entered character is vowel or consonant.


Code: char1 = input("Enter any Chracter :")
vowels = ['a','e','i','o','u','A','E','I','O','U']
if char1 in vowels:
print(f"Character {char1} is vowel." )
else :
print(f"Chracter {char1} is Consonant ")

Output: Enter one Character: A


Character A is vowel.

Subject: Python Programming Page |5


Nehal .V. Solanki 20BECE30279

5. WAP to find maximum of three numbers (nested if-else).


Code: a=int(input("Enter First Value:"))
b=int(input("Enter Second Value:"))
c=int(input("Enter Third Value:"))

if a>b:
if a>c:
print(a,"is Maximum .")
else:
print(c,"is Maximum .")
else:
if b>c:
print(b,"is Maximum .")
else:

print(c,"is Maximum . ")

Output: Enter First Value:20


Enter Second Value:25
Enter Third Value:15
25 is Maximum .

6. WAP to calculate the salary of an employee based on following conditions (nested if-else):
1. if degree = B.E. and experience < 5 years, salary=30000
2. if degree = B.E. and experience >= 5 years, salary=40000
3. if degree = M.E. and experience < 5 years, salary=50000
4. if degree = M.E. and experience >= 5 years, salary= 60000
Code: degree=input("Enter Degree:(1 for B.E. and 2 for M.E.): ")
if degree = ='1':
degree="B.E."
else:
degree="M.E."

Subject: Python Programming Page |6


Nehal .V. Solanki 20BECE30279

experience=int(input("Enter years Experience: "))


if degree = ="B.E.":
if experience < 5:
salary=30000
else:

salary=40000
else:
if experience < 5:
salary=50000
else:

salary=60000
print("Salary: ",salary)

Output: Enter Degree:(1 for B.E. and 2 for M.E.): 1


Enter years Experience: 8
Salary: 40000

7. WAP to check whether entered input is character, digit or special symbol using ladder if-else.

Code: a = input("Enter any input (character or digit or special symbol) : ")


if a.isdigit():

print("a is digit")

elif a.isalpha():

print("a is character")

elif a==" ":

print("input value is Empty space")

else :

print("a is special symbol")

Output: Enter any input (character or digit or special symbol) : 25


a is digit.

Subject: Python Programming Page |7


Nehal .V. Solanki 20BECE30279

Practical Set – 3

1. WAP to find sum of first N numbers.


Code: n=int(input("Enter N:"))
sum=0
for i in range(1,n+1):
sum += i
print("Sum of ",n,"=",sum)

Output: Enter N:10


Sum of 10 = 55

2. WAP to find sum of N scanned numbers.


Code: n=int(input("Enter total no. :"))
sum=0
i=1
while i<n+1:
value=int(input("Enter Value:"))
sum+=value
i=i+1
print("Sum of Entered",n,"Numbers is ",sum)

Output: Enter total no. :5


Enter Value:10
Enter Value:20
Enter Value:14
Enter Value:6
Enter Value:5
Sum of Entered 5 Numbers is 55

Subject: Python Programming Page |8


Nehal .V. Solanki 20BECE30279

3. Write a Python program to find N!.


Code: n=int(input("Enter N for Factorial:"))
fact=1
for i in range(n,0,-1):
fact=fact*i
else:
print("Factorial of Entered no.=",fact)

Output: Enter N for Factorial:5


Factorial of Entered no.= 120

4. Write a Python program to print Fibonacci series upto n terms.


Code: n=int(input("Enter total terms:"))
second_last=0

last_term=1
print(second_last,last_term,sep="\t",end="\t")
for i in range(1,n-1):
last_term+=second_last
second_last=last_term
print(last_term,end="\t")

Output: Enter total terms:8


0 1 1 2 4 8 16 32

5. WAP to find the reverse of given numbers.


Code: num=int(input("Enter Number:"))
rev_num=0
while num!=0:
digit=num%10
rev_num=rev_num*10+digit
num=num//10

Subject: Python Programming Page |9


Nehal .V. Solanki 20BECE30279

else:
print("Reversed Number:",rev_num)
Output: Enter Number:8547
Reversed Number: 7458

6. WAP: to check whether entered number is prime or not.


Code: num=int(input("Enter Number:"))
for i in range(2,int(num**0.5)+1):
if (num%i)==0:
print(num,"is not a Prime Number.")
break
else:
print(num,"is a Prime Number.")

Output: Enter Number:17


17 is a Prime Number.

7. WAP to print all even numbers between 1 to n except the numbers divisible by 6.
Code: n=int(input("Enter N:"))
for i in range(2,n+1,2):
if i%6==0:
pass
else:
print(i,end="\t")

Output: Enter N:20


2 4 8 10 14 16 20

Subject: Python Programming P a g e | 10


Nehal .V. Solanki 20BECE30279

8. Write a python program to check whether given number is Armstrong or not.


Code: num=int(input("Enter Number:"))
power=len(str(num))
temp=num
sum=0
while temp>0:
digit=temp%10
sum += digit**power
temp=temp//10
if num==sum:

print(num,"is an Armstrong number")


else:
print(num,"is not an Armstrong number")

Output: Enter Number:370


370 is an Armstrong number

9. Write a python program to check whether given number is Palindrome or not


Code: num=int(input("Enter Number:"))
rev_num=0
temp=num
while temp!=0:
digit=temp%10

rev_num=rev_num*10+digit
temp//=10
if num==rev_num:

print(num,"is a Palindrome number")


else:
print(num,"is not a Palindrome number")

Subject: Python Programming P a g e | 11


Nehal .V. Solanki 20BECE30279

Output: Enter Number:121


121 is a Palindrome number

10. WAP to print the following: 1) 1 2) * * * * *


12 ****
123 ***
1234 **
12345 *

i. Code: rows=int(input("Enter total no. of Rows: "))


for i in range(rows):
for j in range(i+1):
print(j+1,end=" ")
print("")

Output: Enter total no. of Rows: 5


1
12
123
1234
12345

ii. Code: rows=int(input("Enter total no. of Rows: "))


for i in range(rows, 0, -1):
for j in range(i):
print("*",end=" ")
print("")

Output: Enter total no. of Rows: 5


*****
****
***
**
*

Subject: Python Programming P a g e | 12


Nehal .V. Solanki 20BECE30279

Practical Set - 4

1. Write a python program which covers all the methods (functions) of list.
Code: import copy
list1=["hii","good morning",1,4,3,6]
print("Default list:",list1)
list1.append(6)
print("After Append: ",list1)
list2=list1.copy()
print("Shallow copy List:",list2)
print("Deep Copy:",copy.deepcopy(list1))
list2.clear()
print("Cleared List2:",list2)
print("3 in list1:",list1.count(3))
list1.extend([7,8,[10,11]])
print("Extended List:",list1)
print("Index Of 4 in list1:",list1.index(4))
list1.insert(4,'ldrp')
print("insert Element at index 4:",list1)
print("Poped element:",list1.pop(5))
list1.remove("hii")
print("Removed hii:",list1)list1.reverse()
print("reversed list:",list1)
list4=[5,76,3,45,32,5,7]
print("maximum in list4:",max(list4))
list4.sort()
print("Sorted List",list4)

Subject: Python Programming P a g e | 13


Nehal .V. Solanki 20BECE30279

Output: Default list: ['hii', 'good morning', 1, 4, 3, 6]

After Append: ['hii', 'good morning', 1, 4, 3, 6, 6]

Shallow copy List: ['hii', 'good morning', 1, 4, 3, 6, 6]

Deep Copy: ['hii', 'good morning', 1, 4, 3, 6, 6]

Cleared List2: []

3 in list1: 1

Extended List: ['hii', 'good morning', 1, 4, 3, 6, 6, 7, 8, [10, 11]]

Index Of 4 in list1: 3

insert Element at index 4: ['hii', 'good morning', 1, 4, 'ldrp', 3, 6, 6, 7, 8, [10, 11]]

Poped element: 3

Removed hii: ['good morning', 1, 4, 'ldrp', 6, 6, 7, 8, [10, 11]]

reversed list: [[10, 11], 8, 7, 6, 6, 'ldrp', 4, 1, 'good morning']

maximum in list4: 76

Sorted List [3, 5, 5, 7, 32, 45, 76]

2. Write a Python program to append a list to the second list.


Code: list1=[1,2,3,4,5]
n=int(input("Enter Total No of Element you want to insert:"))
list2=[]
for i in range(n):
j=input("Enter Element:")
list2.append(j)
list1.append(list2)
print("Appended list:",list1)

Output: Enter Total No of Element you want to insert:3


Enter Element:10
Enter Element:14
Enter Element:28
Appended list: [1, 2, 3, 4, 5, ['10', '14', '28']]

Subject: Python Programming P a g e | 14


Nehal .V. Solanki 20BECE30279

3. Write a python program to check whether the given list is palindrome or not.
Code: list1=[]

n=int(input("Enter Total no. of Element:"))


for i in range(n):
j=input("Enter Element:")
list1.append(j)
list2=list1[::-1]
print(list2)
print("list is Palindrome") if list1==list2 else print("list is not palindrome")

Output: Enter Total no. of Element:3Enter


Element: hello
Enter Element:15
Enter Element: hello
['hello', '15', 'hello'] list is Palindrome

4. Write a python program to store strings in list and then print them.
Code: list1=[]
n=int(input("Enter Total no. of Element:"))
for i in range(n):
j=input("Enter Element:")
list1.append(j)
print("List 1 : " ,list1)

Output: Enter Total no. of Element:4


Enter Element:14
Enter Element: GM
Enter Element: NS28
Enter Element: GN
List 1 : ['14', 'GM', 'NS28', 'GN']

Subject: Python Programming P a g e | 15


Nehal .V. Solanki 20BECE30279

5. Write a python program to print list of prime numbers up to N using loop and else clause.
Code: n=int(input("Enter N:"))
prime=[]
for i in range(1,n+1):
for j in range(2,int(i**0.5)+1):
if (i % j) == 0:
break
else:

prime.append(i)
print(prime)

Output: Enter N:15


[1, 2, 3, 5, 7, 11, 13]

6. Write a Python program to multiply all the items in a list.


Code: n=int(input("Enter N:"))
list1=[]
total=1
for i in range(n):
j=int(input("Enter number:"))
list1.append(j)
total*=j
print("Multiplication :",total)

Output: Enter N:5


Enter number:5
Enter number:10
Enter number:4
Enter number:2
Enter number:5

Multiplication : 2000

Subject: Python Programming P a g e | 16


Nehal .V. Solanki 20BECE30279

7. Write a Python program to get the largest number from a list.


Code: n=int(input("Enter N:"))
list1=[]
for i in range(n):
j=int(input("Enter number:"))
list1.append(j)
print("Largest number:",max(list1))

Output: Enter N: 4
Enter number:14
Enter number:28
Enter number:100
Enter number:45
Largest number: 100

8. Write a Python program to find the second smallest number in a list.


Code: n=int(input("Enter N:"))
list1=[]
for i in range(n):
j=int(input("Enter number:"))
list1.append(j)
list1.sort()
print("Second smallest:",list1[1])

Output: Enter N:4


Enter number:10
Enter number:5
Enter number:2
Enter number:-15
Second smallest: 2

Subject: Python Programming P a g e | 17


Nehal .V. Solanki 20BECE30279

9. Write a Python program to count the number of strings where the string length is 2 or more
and the first and last character are same from a given list of strings.
Code: l1=[]
n=int(input("Enter Total no. of Element:"))
for i in range(n):
j=input("Enter Element:")
l1.append(j)
count=0
for i in l1:
if len(i)>2 and i[0]==i[-1]:
count+=1
else:
print("Total No. of strings with more than 2 len & same first and last char: ",count)

Output: Enter Total no. of Element:4


Enter Element:hihi
Enter Element:hihh
Enter Element:ookko
Enter Element:ok
Total No. of strings with more than 2 len & same first and last char: 2

10. Write a Python program to remove duplicates from a list.


Code: l1=["hii","hooo","hii",4,5,6,7,6,4]
print("given list:",l1)
l2=[]
for i in l1:
if i not in l2:l2.append(i)
print("List after removing duplicates:",l2)

Output: given list: ['hii', 'hooo', 'hii', 4, 5, 6, 7, 6, 4]


List after removing duplicates: ['hii', 'hooo', 4, 5, 6, 7]

Subject: Python Programming P a g e | 18


Nehal .V. Solanki 20BECE30279

11. Write a Python program to find the list of words that are longer than n from a given string.
Code: s1=input("Enter String:")
n=int(input("Enter min length of word: "))
l1=s1.split()
l2=[]
for i in l1:
if len(i)>n:l2.append(i)
print("List of words:",l2)

Output: Enter String: i am sahil vaghasiya and i am student at ldrp college


Enter min length of word: 2
List of words: ['sahil', 'vaghasiya', 'and', 'student', 'ldrp', 'college']

12. Write a Python function that takes two lists and returns True if they have at least one common
member.
Code: def match(l1,l2):
for i in l1:
for j in l2:
if i= =j:return True
else:return False
l1=[4,5,6,7,2,3]
l2=[8,9,10,5,11,2]
print("True") if match(l1,l2) else print("False")

Output: True

13. Write a Python program to print the numbers of a specified list after removing even from it.
Code: l1=[1,2,3,4,5,6,7,8,9,10]
for i in l1:
if i%2==0:
l1.remove(i)
print(l1)
Output: [1, 3, 5, 7, 9]
Subject: Python Programming P a g e | 19
Nehal .V. Solanki 20BECE30279

14. Write a Python program to add two matrices.


Code: def printMat(statement,M):
print(statement)
for i in M:
print(i)

X = [[2, 4, 5], [5, 6, 7], [1, 2, 3]]


Y = [[7, 8, 9], [10, 11, 12], [3, 4, 5]]
sum = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

for i in range(len(X)):
for j in range(len(Y)):
sum[i][j] = X[i][j]+Y[i][j]

printMat("X:",X)
printMat("Y:",Y)
printMat("Sum:",Y)

Output: X:
[2, 4, 5]
[5, 6, 7]
[1, 2, 3]
Y:
[7, 8, 9]
[10, 11, 12]
[3, 4, 5]
Sum:
[7, 8, 9]
[10, 11, 12]
[3, 4, 5]

Subject: Python Programming P a g e | 20


Nehal .V. Solanki 20BECE30279

15. Write a Python program to transpose a given matrix.


Code: def printMat(stmt,M):
print(stmt)
for i in M:
print(i)
m1=[[1,2,3],
[4,5,6],
[6,7,8],
[8,9,10]]
rows=4
cols=3
m2=[[0 for x in range(rows)] for y in range(cols)]
for i in range(cols):
for j in range(rows):
m2[i][j]=m1[j][i]
printMat("Original Matrix:",m1)
printMat("Transposed Matrix:",m2)

Output: Original Matrix:


[1, 2, 3]

[4, 5, 6]
[6, 7, 8]
[8, 9, 10]

Transposed Matrix:
[1, 4, 6, 8]
[2, 5, 7, 9]
[3, 6, 8, 10]

Subject: Python Programming P a g e | 21


Nehal .V. Solanki 20BECE30279

16. WAP to Flatten a nested list structure.


Code: l1=[1, [12, 3], [4, 5, [6, [7,8]] ] ]

flattenList = []
def flatten(L):
for element in L:

if type(element) is list:
flatten(element)
else:

flattenList.append(element)
return flattenList
print("Nested List:",l1)
print("Flatten List:",flatten(l1))

Output: Nested List: [1, [12, 3], [4, 5, [6, [7, 8]]]]
Flatten List: [1, 12, 3, 4, 5, 6, 7, 8]

17. Write a Python program to split a list every Nth element.


Code: l1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
n=int(input("Enter N:"))
l2=[]
for i in range(n):
l2.append(l1[i::n])
print("Original List:",l1)

print("Splitted List:",l2)

Output: Enter N:4

Original List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]


Splitted List: [[1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12]]

Subject: Python Programming P a g e | 22

You might also like