Python Miscellaneous Questions

Python Practice Questions

Here are a few Python practice problem statements at a moderate difficulty level with solutions,:

  1. Write a program to find the sum of the first n natural numbers.
n = int(input("Enter the value of n: "))
sum = 0
for i in range(1, n+1):
    sum = sum + i
print("The sum of the first", n, "natural numbers is", sum)
  1. Write a program to find the factorial of a given number.
n = int(input("Enter a number: "))
factorial = 1
for i in range(1, n+1):
    factorial = factorial * i
print("The factorial of", n, "is", factorial)
  1. Write a program to check if a given year is a leap year or not.
year = int(input("Enter a year: "))
if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print(year, "is a leap year")
        else:
            print(year, "is not a leap year")
    else:
        print(year, "is a leap year")
else:
    print(year, "is not a leap year")
  1. Write a program to check if a given number is prime or not.
n = int(input("Enter a number: "))
if n > 1:
    for i in range(2, n):
        if (n % i) == 0:
            print(n, "is not a prime number")
            break
    else:
        print(n, "is a prime number")
else:
    print(n, "is not a prime number")
</code>
  1. Write a program to find the largest and smallest number in a list.
numbers = [int(x) for x in input("Enter numbers separated by space: ").split()]
minimum = numbers[0]
maximum = numbers[0]
for num in numbers:
    if num < minimum:
        minimum = num
    if num > maximum:
        maximum = num
print("Minimum:", minimum)
print("Maximum:", maximum)
  1. Write a program to print the first n Fibonacci numbers.
<code>n = int(input("Enter the value of n: "))
fib1 = 0
fib2 = 1
print(fib1)
print(fib2)
for i in range(2, n):
    fib3 = fib1 + fib2
    print(fib3)
    fib1 = fib2
    fib2 = fib3
</code>
  1. Write a program to find the average of n numbers.
n = int(input("Enter the number of elements: "))
numbers = [int(x) for x in input("Enter numbers separated by space: ").split()]
sum = 0
for num in numbers:
    sum = sum + num
average = sum / n
print("Average:", average)
  1. Write a program to check if a given string is a palindrome or not.
string = input("Enter a string: ")
if string == string[::-1]:
    print(string, "is a palindrome")
else:
    print(string, "is not a palindrome")
  1. Write a program to print the multiplication table of a given number.
n = int(input("Enter a number: "))
for i in range(1, 11):
    product = n * i
    print(n, "x", i, "=", product)
  1. Write a program to generate all the prime numbers between a given range.
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
for num in range(start, end + 1):
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
                break
        else:
            print(num)
  1. Write a program to find the number of occurrences of a given character in a string.
string = input("Enter a string: ")
char = input("Enter a character: ")
count = 0
for c in string:
    if c == char:
        count = count + 1
print("The character", char, "occurs", count, "times in the string")
  1. Write a program to convert a given number of seconds into hours, minutes, and seconds.
seconds = int(input("Enter the number of seconds: "))
hours = seconds // 3600
seconds = seconds % 3600
minutes = seconds // 60
seconds = seconds % 60
print(hours, "hours", minutes, "minutes", seconds, "seconds")
  1. Write a program to print all the odd numbers between a given range.
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
for num in range(start, end + 1):
    if num % 2 != 0:
        print(num)
  1. Write a program to calculate the power of a given number using recursion.
def power(base, exponent):
    if exponent == 0:
        return 1
    else:
        return base * power(base, exponent-1)

base = int(input("Enter the base: "))
exponent = int(input("Enter the exponent: "))
result = power(base, exponent)
print(base, "to the power", exponent, "is", result)
  1. Write a program to find the largest number in a list of numbers.
numbers = [int(x) for x in input("Enter numbers separated by space: ").split()]
largest = numbers[0]
for num in numbers:
    if num > largest:
        largest = num
print("Largest number:", largest)
  1. Write a program to find the sum of all the elements in a list of numbers.
numbers = [int(x) for x in input("Enter numbers separated by space: ").split()]
sum = 0
for num in numbers:
    sum = sum + num
print("Sum:", sum)
  1. Write a program to find the second largest number in a list of numbers.
numbers = [int(x) for x in input("Enter numbers separated by space: ").split()]
largest = numbers[0]
second_largest = numbers[0]
for num in numbers:
    if num > largest:
        second_largest = largest
        largest = num
    elif num > second_largest and num != largest:
        second_largest = num
print("Second largest number:", second_largest)
  1. Write a program to find the sum of the first n natural numbers.
n = int(input("Enter the value of n: "))
sum = 0
for i in range(1, n + 1):
    sum = sum + i
print("Sum:", sum)
  1. Write a program to reverse a given string.
string = input("Enter a string: ")
print("Reverse:", string[::-1])
  1. Write a program to check if a string is a palindrome or not.
string = input("Enter a string: ")
if string == string[::-1]:
    print("The string is a palindrome")
else:
    print("The string is not a palindrome")
  1. Write a program to count the number of vowels in a string.
string = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for char in string:
    if char in vowels:
        count = count + 1
print("Number of vowels:", count)
  1. Write a program to find the first non-repeated character in a string.
string = input("Enter a string: ")
for char in string:
    if string.count(char) == 1:
        print("First non-repeated character:", char)
        break
  1. Write a program to remove all the occurrences of a given character in a string.
string = input("Enter a string: ")
char = input("Enter a character: ")
new_string = ""
for c in string:
    if c != char:
        new_string = new_string + c
print("String after removing all occurrences of", char, ":", new_string)
  1. Write a program to find the longest word in a given sentence.
sentence = input("Enter a sentence: ")
words = sentence.split()
longest = words[0]
for word in words:
    if len(word) > len(longest):
        longest = word
print("Longest word:", longest)
  1. Write a program to convert a given sentence to title case.
sentence = input("Enter a sentence: ")
words = sentence.split()
new_sentence = ""
for word in words:
    new_sentence = new_sentence + word.capitalize() + " "
print("Title case sentence:", new_sentence.strip())
  1. Write a program to count the number of words in a given sentence.
sentence = input("Enter a sentence: ")
words = sentence.split()
print("Number of words:", len(words))
  1. Write a program to find the most common letter in a string.
string = input("Enter a string: ")
letters = {}
for char in string:
    if char in letters:
        letters[char] = letters[char] + 1
    else:
        letters[char] = 1
most_common = max(letters, key=letters.get)
print("Most common letter:", most_common)
  1. Write a program to find the frequency of each word in a given sentence.
sentence = input("Enter a sentence: ")
words = sentence.split()
word_count = {}
for word in words:
    if word in word_count:
        word_count[word] = word_count[word] + 1
    else:
        word_count[word] = 1
print("Word frequency:", word_count)
  1. Write a program to replace all occurrences of a word in a given sentence with another word.
sentence = input("Enter a sentence: ")
old_word = input("Enter the word to be replaced: ")
new_word = input("Enter the new word: ")
new_sentence = sentence.replace(old_word, new_word)
print("Sentence after replacing", old_word, "with", new_word, ":", new_sentence)
  1. Write a program to check if a number is even or odd.
num = int(input("Enter a number: "))
if num % 2 == 0:
    print(num, "is even")
else:
    print(num, "is odd")
  1. Write a program to find the largest of three numbers.
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
if num1 > num2 and num1 > num3:
    largest = num1
elif num2 > num1 and num2 > num3:
    largest = num2
else:
    largest = num3
print("Largest number:", largest)
  1. Write a program to check if a year is a leap year or not.
year = int(input("Enter a year: "))
if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0):
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")
  1. Write a program to find the roots of a quadratic equation.
import math
a = int(input("Enter the coefficient of x^2: "))
b = int(input("Enter the coefficient of x: "))
c = int(input("Enter the constant: "))
d = b**2 - 4*a*c
if d < 0:
    print("The equation has no real roots")
elif d == 0:
    root = -b / (2*a)
    print("The equation has one real root:", root)
else:
    root1 = (-b + math.sqrt(d)) / (2*a)
    root2 = (-b - math.sqrt(d)) / (2*a)
    print("The equation has two real roots:", root1, "and", root2)
  1. Write a program to check if a number is positive, negative, or zero.
num = int(input("Enter a number: "))
if num > 0:
    print(num, "is positive")
elif num < 0:
    print(num, "is negative")
else:
    print(num, "is zero")
  1. Write a program to find the grade of a student based on their marks.
marks = int(input("Enter the marks: "))
if marks >= 90:
    grade = "A"
elif marks >= 80:
    grade = "B"
elif marks >= 70:
    grade = "C"
elif marks >= 60:
    grade = "D"
else:
    grade = "F"
print("Grade:", grade)
  1. Write a program to check if a triangle is valid or not based on its sides.
side1 = int(input("Enter the first side: "))
side2 = int(input("Enter the second side: "))
side3 = int(input("Enter the third side: "))
if side1 + side2 > side3 and side2 + side3 > side1 and side3 + side1 > side2:
    print("The triangle is valid")
else:
    print("The triangle is not valid")
  1. Write a program to check if a number is divisible by both 3 and 5 or not.
num = int(input("Enter a number: "))
if num % 3 == 0 and num % 5 == 0:
    print(num, "is divisible by both 3 and 5")
else:
    print(num, "is not divisible by both 3 and 5")
  1. Write a program to find the largest of four numbers.
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
num4 = int(input("Enter the fourth number: "))
if num1 > num2 and num1 > num3 and num1 > num4:
    largest = num1
elif num2 > num1 and num2 > num3 and num2 > num4:
    largest = num2
elif num3 > num1 and num3 > num2 and num3 > num4:
    largest = num3
else:
    largest = num4
print("Largest number:", largest)
  1. Write a program to find the smallest of four numbers.
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
num4 = int(input("Enter the fourth number: "))
if num1 < num2 and num1 < num3 and num1 < num4:
    smallest = num1
elif num2 < num1 and num2 < num3 and num2 < num4:
    smallest = num2
elif num3 < num1 and num3 < num2 and num3 < num4:
    smallest = num3
else:
    smallest = num4
print("Smallest number:", smallest)
  1. Write a program to print the first n natural numbers.
n = int(input("Enter the value of n: "))
for i in range(1, n+1):
    print(i)
  1. Write a program to print the first n even numbers.
n = int(input("Enter the value of n: "))
for i in range(2, n*2+1, 2):
    print(i)
  1. Write a program to print the first n odd numbers.
n = int(input("Enter the value of n: "))
for i in range(1, n*2+1, 2):
    print(i)
  1. Write a program to find the sum of first n natural numbers.
n = int(input("Enter the value of n: "))
sum = 0
for i in range(1, n+1):
    sum += i
print("Sum of first", n, "natural numbers:", sum)
  1. Write a program to find the product of first n natural numbers.
n = int(input("Enter the value of n: "))
product = 1
for i in range(1, n+1):
    product *= i
print("Product of first", n, "natural numbers:", product)
  1. Write a program to find the factorial of a number.
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num+1):
    factorial *= i
print("Factorial of", num, ":", factorial)
  1. Write a program to print the table of a given number.
num = int(input("Enter a number: "))
for i in range(1, 11):
    print(num, "x", i, "=", num*i)
  1. Write a program to print the multiplication table of numbers from 1 to 10.
for i in range(1, 11):
    for j in range(1, 11):
        print(i, "x", j, "=", i*j)
    print()
  1. Write a program to print the sum of all elements of a list.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum = 0
for num in numbers:
    sum += num
print("Sum of all elements in the list:", sum)
  1. Write a program to print the largest number in a list.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
largest = numbers[0]
for num in numbers:
    if num > largest:
        largest = num
print("Largest number in the list:", largest)
  1. Write a program to print the reverse of a list.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(len(numbers)-1, -1, -1):
    print(numbers[i], end=', ')
  1. Write a program to check if a given number is present in a list.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num = int(input("Enter a number: "))
found = False
for n in numbers:
    if n == num:
        found = True
        break
if found:
    print(num, "is present in the list.")
else:
    print(num, "is not present in the list.")

If you have solved all the problems I have mentioned, you have a good understanding of basic to intermediate level Python concepts, such as variables, data types, control structures, loops, functions, and some string and list manipulation.

However, solving these problems alone is not a comprehensive measure of your proficiency in Python. There are many other areas of the language, such as file I/O . etc. that you may want to explore to gain a more comprehensive understanding of the language. We will update more practice Questions Here.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top