Python Practice Questions And Answers
- Write a program to print:
Hello, Python
- Write a program to take your name as input and print
Welcome, <name>.
- Write a program to input two numbers and print their sum, difference, product, quotient, and remainder.
- Write a program to swap two numbers using a third variable.
- Write a program to swap two numbers without using a third variable.
- Write a program to input the radius of a circle and print its area and circumference.
- Write a program to input marks of 5 subjects and print total, average, and percentage.
- Write a program to convert temperature from Celsius to Fahrenheit.
- Write a program to input a number and print whether it is positive, negative, or zero.
- Write a program to check whether a number is even or odd.
- Write a program to check whether a person is eligible to vote.
- Write a program to find the greater of two numbers.
- Write a program to find the greatest among three numbers.
- Write a program to check whether a year is a leap year.
- Write a program to check whether a character is a vowel or consonant.
- Write a program to check whether a number is divisible by both 5 and 11.
- Write a program to input a number and print its absolute value without using any built-in function.
- Write a program to find whether a given number lies between 1 and 100.
- Write a program to print numbers from 1 to 10 using a loop.
- Write a program to print numbers from 10 to 1 in reverse order.
- Write a program to print all even numbers from 1 to n.
- Write a program to print all odd numbers from 1 to n.
- Write a program to find the sum of first n natural numbers.
- Write a program to find the factorial of a number.
- Write a program to print the multiplication table of a given number.
- Write a program to count how many digits are present in a number.
- Write a program to find the sum of digits of a number.
- Write a program to reverse a number.
- Write a program to check whether a number is a palindrome.
- Write a program to check whether a number is an Armstrong number.
- Write a program to print all Armstrong numbers between 1 and 1000.
- Write a program to check whether a number is prime.
- Write a program to print all prime numbers in a given range.
- Write a program to find the HCF of two numbers.
- Write a program to find the LCM of two numbers.
- Write a program to print the Fibonacci series up to n terms.
- Write a program to keep taking numbers as input until the user enters 0, then print their sum.
- Write a program to repeatedly ask for a password until the correct password is entered.
- Write a program to print a right-aligned triangle of stars.
- Write a program to count how many numbers between 1 and n are divisible by 3 but not by 5.
- Write a program to print the first n numbers that are divisible by 7.
- Write a program to input a string and print it character by character.
- Write a program to count the number of vowels, consonants, digits, and spaces in a string.
- Write a program to reverse a string without using slicing.
- Write a program to check whether a string is a palindrome.
- Write a program to count the occurrence of a given character in a string.
- Write a program to count the number of words in a sentence.
- Write a program to convert a string to uppercase without using upper().
- Write a program to convert a string to lowercase without using lower().
- Write a program to remove all spaces from a string.
- Write a program to find the longest word in a sentence.
- Write a program to replace every occurrence of one word in a sentence with another word.
- Write a program to check whether two strings are anagrams.
- Write a program to print only the characters at even positions in a string.
- Write a program to create a list of 10 integers from user input and print the largest and smallest values.
- Write a program to find the sum of all elements in a list.
- Write a program to count how many even and odd numbers are present in a list.
- Write a program to remove all duplicate elements from a list.
- Write a program to sort a list in ascending order without using sort().
- Write a program to merge two lists into one.
- Write a program to find the second largest element in a list.
- Write a program to rotate a list to the right by one position.
- Write a program to separate positive and negative numbers from a list.
- Write a program to count the frequency of each element in a list.
- Write a program to find the common elements in two lists.
- Write a program to read n numbers into a list and print only those numbers that are prime.
- Write a program to create a tuple with five elements and print each element separately.
- Write a program to find the maximum and minimum element in a tuple.
- Write a program to count how many times a given item appears in a tuple.
- Write a program to convert a list into a tuple and a tuple into a list.
- Write a program to take student names as keys and marks as values, then print the dictionary.
- Write a program to input a dictionary and print all keys, all values, and all key-value pairs separately.
- Write a program to find the student with the highest marks from a dictionary.
- Write a program to count the frequency of each character in a string using a dictionary.
- Write a program to merge two dictionaries.
- Write a program to check whether a given key exists in a dictionary.
- Write a program to invert a dictionary so that values become keys.
- Write a function to check whether a number is prime.
- Write a function to return the factorial of a number.
- Write a function to return the sum of digits of a number.
- Write a function that accepts a list and returns the largest element.
- Write a function that accepts a string and returns the number of vowels in it.
- Write a function with default arguments to calculate simple interest.
- Write a function that accepts any number of arguments and returns their sum.
- Write a recursive function to find the factorial of a number.
- Write a recursive function to print Fibonacci terms up to n.
- Write a lambda expression to square a number.
- Write a lambda expression to find the larger of two numbers.
- Write a program to generate 10 random numbers between 1 and 100.
- Write a program to simulate rolling a die 20 times.
- Write a program to choose a random item from a list.
- Write a program that uses the math library to compute square root, power, floor, and ceil of a number.
- Write a program that creates a file and writes 5 lines of text into it.
- Write a program to read a file and print its contents line by line.
- Write a program to count the number of lines, words, and characters in a file.
- Write a program to copy the contents of one file into another.
- Write a program to read a file and print only those lines that contain a given word.
- Write a program to append new data to an existing file.
- Write a program to store a list of numbers in a file, then read them back and print their sum.
- Write a program that asks the user for a filename and handles the case where the file does not exist.
- Write a program that takes two numbers and handles division by zero properly.
- Write a program that takes an integer input and handles invalid input using exception handling.
- Create your own exception for checking whether a number is negative where only positive numbers are allowed.
- Write a menu-driven calculator using functions for add, subtract, multiply, and divide.
- Write a menu-driven program for these operations on a list: insert, delete, search, display.
- Write a program to maintain a student record using a dictionary with options to add, update, delete, and display.
- Write a program to count uppercase letters, lowercase letters, digits, and special characters in a string.
- Write a program to print the following series up to n terms:
1, 8, 27, 64, 125, ...
- Write a program to check whether a given number is a strong number.
- Write a program to check whether a string contains only digits.
- Write a program to remove all punctuation marks from a string.
- Write a program to print the transpose of a 2D list representing a matrix.
- Write a program to add two matrices.
- Write a program to multiply two matrices.
- Write a program to search for an element in a list and print its position. If not found, print an appropriate message.
- Write a function to return the count of uppercase and lowercase letters in a string.
- Write a function that takes a list and returns a new list containing only unique elements in the same order.
- Write a function that accepts a sentence and returns a dictionary containing each word and its frequency.
- Write a program that stores attendance of students as
PresentorAbsentand counts how many are present.
- Write a program to create a billing system for n items where input is item name, quantity, and price, then print total bill.
- Write a program to generate a random password of given length using letters and digits.
- Write a program to validate a password using rules such as minimum length, at least one uppercase letter, one lowercase letter, one digit, and one special character.