Interview PrepInterview Questions

Top 30 Python Coding Questions and Answers

Table of Contents

🐍 Top 30 Python Coding Questions and Answers for Placements (With Clear Explanations & Examples)


In today’s competitive job market, Python has become one of the most in-demand programming languages. Whether you’re preparing for a campus placement, technical interview, or coding test, having a strong grip on Python is essential. This post provides you with Top 30 frequently asked Python coding questions and answers that are ideal for students, beginners, and job seekers. Each question comes with a clear explanation, input-output examples, and practical logic to help you crack any placement test confidently.

Let’s dive into these must-practice questions! πŸ‘‡


πŸ”’ 1. Write a program to check if a number is even or odd.

Explanation: An even number is divisible by 2, odd is not.


πŸ” 2. Write a program to print the Fibonacci series up to n terms.

Explanation: Fibonacci series: 0, 1, 1, 2, 3, 5, 8…


πŸ” 3. Check if a number is prime or not.

Explanation: Prime numbers are divisible only by 1 and themselves.


πŸ”„ 4. Reverse a string without using built-in functions.

Explanation: Loop through the string backwards.


🎯 5. Find the factorial of a number using recursion.

Explanation: Factorial of n = n * (n-1)…


πŸ“Š 6. Count the vowels in a string

Explanation:
Vowels in English are a, e, i, o, and u. This problem asks us to count how many vowels are present in a given string. We’ll check each character to see if it’s a vowel.

Why it works:
We loop through each character in the string and increment the counter only if it matches any vowel (uppercase or lowercase).


πŸ”„ 7. Check if a string is a palindrome

Explanation:
A palindrome is a string that reads the same backward as forward. Examples: madam, racecar, level.

To check this, we simply compare the original string with its reverse.

Why it works:
s[::-1] reverses the string using Python slicing. If the original and reversed strings are equal, it’s a palindrome.


βž• 8. Find the sum of digits of a number

Explanation:
To find the sum of all digits in a number, we convert it to a string, loop through each character (digit), convert it back to an integer, and add it.

Why it works:
The str(n) breaks the number into individual digits, and we accumulate the sum using a loop.


🎯 9. Find the largest of three numbers

Explanation:
To find the largest number among three, we use Python’s built-in max() function or logic with conditional statements.

Alternative (Without using max):

Why it works:
We simply compare the values to determine the highest. Both max() and if-else logic are acceptable in interviews.


πŸ” 10. Print all prime numbers in a range

Explanation:
A prime number is a number greater than 1 that has no divisors other than 1 and itself. We use a helper function to check if a number is prime and print all such numbers in a given range.

Why it works:
We check divisibility up to the square root of the number for efficiency. This reduces unnecessary checks and improves performance.


βš–οΈ 11. Check if a number is an Armstrong number

Explanation:
An Armstrong number is one whose sum of its digits raised to the power of the number of digits is equal to the number itself.
For example, 153 is an Armstrong number because:

1Β³ + 5Β³ + 3Β³ = 1 + 125 + 27 = 153

Why it works:
We convert the number to a string to iterate over its digits, raise each to the power of the number of digits, and compare the result with the original number.


πŸ”’ 12. Calculate the GCD of two numbers

Explanation:
GCD (Greatest Common Divisor) is the largest number that divides both numbers without leaving a remainder.

Why it works:
This is known as Euclid’s Algorithm. We repeatedly replace a with b and b with a % b until it b becomes 0. The final a is the GCD.


πŸ” 13. Calculate the LCM of two numbers

Explanation:
LCM (Least Common Multiple) of two numbers is the smallest number that is a multiple of both.
We use the formula:

LCM(a, b) = (a * b) / GCD(a, b)

Why it works:
We first calculate the GCD and then use it to compute the LCM using the standard formula.


πŸ”„ 14. Print a pattern – right-angled triangle of stars

Explanation:
A common pattern question in coding interviews. For a given number of rows n, print a right-angled triangle of stars (*).

Output:

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

Why it works:
In each iteration, we print i stars, increasing the count with each row.


Also Read,

Top 30 C Language Coding Questions and Answers

Top 50 Common Interview Questions & How to Answer Them

πŸ”€ 15. Count the frequency of characters in a string

Explanation:
We loop through each character in the string and maintain a dictionary to count occurrences.

Output:

{'h': 1, 'e': 1, 'l': 2, 'o': 1}

Why it works:
We use the .get() method to safely update the dictionary even if the character is not present initially.


πŸ”€ 16. Remove duplicates from a list

Explanation:
Lists can contain repeated elements. To get only unique values, we can convert the list to a set, then back to a list.

Output:

[1, 2, 3, 4, 5]

Why it works:
Sets automatically eliminate duplicates. Converting back to a list gives us the required format.


πŸ”„ 17. Reverse a list without using reverse() function

Explanation:
We can reverse a list manually using slicing. This is a common logic question asked to test your knowledge of Python list manipulation.

Output:

[40, 30, 20, 10]

Why it works:
The slicing method [::-1] returns the list in reversed order. It’s a clean, Pythonic solution.


πŸ” 18. Find the factorial of a number using recursion

Explanation:
The factorial of a number n (denoted n!) is the product of all positive integers less than or equal to n.
For example:
5! = 5 Γ— 4 Γ— 3 Γ— 2 Γ— 1 = 120

Why it works:
The function calls itself with a smaller number until it hits the base case n == 1, making it a classic example of recursion.


πŸ”„ 19. Check if two strings are anagrams

Explanation:
Two strings are anagrams if they contain the same characters in any order.
Example: listen and silent are anagrams.

Why it works:
By sorting both strings, we bring the characters into the same order. If they match, they are anagrams.


πŸ“š 20. Merge two dictionaries

Explanation:
Python allows us to combine two dictionaries using the update() method or the ** unpacking operator.

Output:

{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Why it works:
The ** operator unpacks key-value pairs from both dictionaries into a new one, creating a merged dictionary.


πŸ” 21. Find the second largest number in a list

Explanation:
To find the second largest number, we sort the list in descending order and pick the second element. This is a very common coding test question.

Why it works:
We use a set to remove duplicates, sort the list in descending order, and access the second element.


πŸ”„ 22. Rotate a list to the right by k steps

Explanation:
Rotation means shifting elements. Rotating to the right moves the last element to the front.

Why it works:
We use slicing to split and rearrange the list into rotated form. The modulo helps avoid errors for large k.


πŸ’― 23. Check if a number is a perfect number

Explanation:
A perfect number is a positive integer that is equal to the sum of its proper divisors.
Example: 28 β†’ Divisors = 1, 2, 4, 7, 14 β†’ Sum = 28 βœ…

Why it works:
We find all divisors less than n and check if their sum equals n.


πŸ”€ 24. Remove all special characters from a string

Explanation:
We can use a loop or a regex to remove non-alphanumeric characters from a string.

Why it works:
The regex [^A-Za-z0-9 ] targets all characters except letters, numbers, and space. The re.sub() function replaces them with an empty string.


πŸ” 25. Print the Fibonacci series up to n terms

Explanation:
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones.
Starting with: 0, 1, 1, 2, 3, 5, 8...

Output:

0 1 1 2 3 5 8

Why it works:
We use two variables a and b to generate the series iteratively. It’s efficient and beginner-friendly.


πŸ“¦ 26. Flatten a nested list

Explanation:
A nested list is a list inside another list. To flatten it, we extract all elements and put them into a single list.

Why it works:
We use two for loops to access elements of inner lists and collect them in a single flat list.


🧠 27. Find the most frequent element in a list

Explanation:
We count the frequency of each element and return the one with the highest count.

Why it works:
We use a dictionary to count and then find the key with the highest value using max() and key=freq.get.


πŸ’₯ 28. Find all pairs in a list that sum to a target

Explanation:
This is a classic coding interview question. We loop through the list and use a set to track complements.

Why it works:
We store elements in a set and check if the required number to reach the target is already seen.


🧾 29. Convert a string to title case

Explanation:
Title case means every word starts with an uppercase letter.

Why it works:
Python’s built-in .title() method automatically capitalizes the first letter of each word.


πŸ” 30. Count vowels in a string

Explanation:
We loop through each character and check if it’s a vowel (both lowercase and uppercase).

Why it works:
We use a generator expression to count all vowels efficiently without creating an intermediate list.


πŸŽ‰ That’s it! You’ve now got 30 beginner-friendly, placement-focused Python coding questions with solutions and logical clarity!

πŸ“Œ Conclusion

This blog will teach you from basic-level coding questions and answers to advanced-level which will be helpful in placement exam coding rounds. Whether you are a student, beginner, or career switcher, understanding these foundational concepts will boost your confidence in solving real-time coding problems.

βœ… Pro Tips:

  • Practice regularly on platforms like HackerRank, LeetCode, or GeeksforGeeks.

  • Understand the logic before memorising code.

  • Always dry-run your code with examples.

πŸ’‘ β€œPractice doesn’t make perfect. Perfect practice makes perfect.”

πŸ“€ Stay Updated with NextGen Careers Hub

πŸ“± Follow us onΒ Instagram
πŸ“Ί Subscribe to us on YouTube

Please share our website with others:Β NextGenCareersHub.in

Python coding questions

admin

Welcome to NextGen Careers Hub – your daily gateway to career growth, tech insights, and the future of work! πŸš€ In a world where everything moves fast – from job markets to AI breakthroughs – we’re here to keep you one step ahead. Whether you're hunting for your dream job, leveling up your coding skills, or staying informed on the latest in Artificial Intelligence, you're in the right place. πŸ’ΌπŸ’‘