technical skills grow

Responsive Ads Here

Monday, May 12, 2025

Python questions for practice

#Q Sort a list of numbers in ascending and descending 
numbers = [45, 12, 78, 34, 89, 23]
# Sort in ascending order ascending = sorted(numbers)
# Sort in descending order descending = sorted(numbers, reverse=True) print("Original List:", numbers) print("Ascending Order:", ascending) print("Descending Order:", descending)


#Q :Remove a specific element from a list.
num = int(input("How many element enter : "))

element = []

for i in range(num):

    user_input =input("Enter your element: ")
    element.append(user_input)

print("Your element :", element)

rem_element = input("Enter your remove element no ")

element.remove(rem_element)

print(f"New List: {element}")

#output :
# How many element enter : 2
# Enter your element: a
# Enter your element: b
# Your element : ['a', 'b']
# Enter your remove element no b
# New List: ['a']

#Q : Insert an element at the second position in the list.
num = int(input("Enter your no : "))
number =[1,2,3,4,5]
number.insert(1,num)
print("First No :", number)

#Q : Python program that prints the first and last elements of a list:
num = int(input("Enter your no i will print from start and last"))
number =[]
for i in range(num):

    user_input=int(input(f"Enter your no : "))

    number.append(user_input)

print("First No :", number[0])
print("Last No :" , number[-1])

#Q:Ask the user to enter numbers and store them in a list.
print ("How many no you want to print: ")
num = int(input("Enter the number :"))
numbers = []
for i in range(num):
    user_input = int(input(f"Enter the no {i + 1} : "))
    numbers.append(user_input)
print(f"Your no list : {numbers}")

Q:#Create a list of 5 fruits and print each one using a loop.

a = input("Enter the 1st Fruit: ") b = input("Enter the 2nd Fruit: ") c = input("Enter the 3rd Fruit: ") d = input("Enter the 4th Fruit: ") fruit_list = [] fruit_list.append(a) fruit_list.append(b) fruit_list.append(c) fruit_list.append(d) print(fruit_list)



#Q :create a new list that contains the squares of each number..
list =[2,3,4,5]
list_sq =[]
sum = 0
c = len(list)
for i in range(c):
     sum = list[i] * list[i]
     
     list_sq.append(sum)
print(list_sq)

#Q :prints the sum of all even numbers in the list.
list =[1,2,3,4,5,6,7,8,9]
sum = 0
c = len(list)
for i in range(1,c):
    if i % 2 == 0:
        sum = sum + i
print(sum)

#Q: To add the number 100 to the beginning of a list in Python, you can use the .insert() method:
list = [1,10,100,100,1000]

list.insert(0,200)

print(list)


#Q :Create a list of 10 integers and print the first and last elements of the list.

my_list = [10, 20, 30, 40]
count= len(my_list)
for i in range(count):

    if i == 0 or i ==count-1:
        print(f"List no : {my_list[i]} ")
   

Q#Count how many numbers between 1 and 50 are divisible by 7.

while True:
    user_val = int(input("Enter the no: "))
    for i in range (user_val):
        if i % 7 == 0:
            print(f"This no is divided by 7 : {i}")

    choose = input("Do you want to continue y / n ")

    if choose.lower() !='y':
        break
       

 Q: Find the factorial of a number entered by the user.

while True:
        user_val = int(input("Enter the no: "))
        fact = 0
        sum =1
        for i in range(user_val):
               fact = user_val - i
               sum= sum * fact
        print(sum)
        choose = input("Do you want continue Y/N")

        if choose.lower() !='y':
                break

No comments:

Post a Comment

Powered by Blogger.

Labels

Contact Form

Name

Email *

Message *

Search This Blog

Blog Archive

Ad Code

Responsive Advertisement

Recent Posts