Practice Problems (05 Aug 2023)

Q. 1] Write a Python Program to check whether a number is prime or not.(Take numbers whatever you want)
Q. 2] How can you randomize the items of a list in place?
List = [‘He’, ‘Loves’, ‘To’, ‘Code’, ‘In’, ‘Python’]
Q. 3] How will you remove duplicate elements from a list?
demo_list = [5, 4, 4, 6, 8, 12, 12, 1, 5]
Q. 4] Write a code to sort a numerical list (Take numbers as your wish).
Q. 5] Write a program in Python to produce Star triangle.

4 Likes

Q1). Program to check if number is prime or not

num = int(input(“Enter a number:”))
flag = 0
for i in range(2,num):
if num%i == 0:
flag = 1
break

if(num <= 1):
print(“Not a prime number”)
elif(flag == 1):
print(“Not a prime number”)
else:
print(“Prime number”)

#Q2). Randomize items in list
import random
List = [‘He’, ‘Loves’, ‘To’, ‘Code’, ‘In’, ‘Python’]
random.shuffle(List)
print(List)

#Q3). Remove duplictes from list
demo_list = [5, 4, 4, 6, 8, 12, 12, 1, 5]
res = [*set(demo_list)]
print(res)

#Q4). Sort numbers in a List
numbers = [5, 4, 4, 6, 8, 12, 12, 1, 5]
numbers.sort()
print(numbers)

#Q5). Print Star triangle
rows = 5
for i in range(rows):
for j in range(i+i+1):
print(“*”, end = “”)
print(“\n”)

@pirjadesimran815

1 Like

Hello… @pankajbhise.rmdssoe
It is amazing…Keep going

1 Like
1 Like

Hello… @akhand789ww
It is awesome…

1 Like