Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Top 20 Python Coding Questions and Answers for Programming

Last Updated on June 6, 2024 by Abhishek Sharma

Python is a versatile, high-level programming language known for its readability, simplicity, and wide range of applications. From web development and data analysis to artificial intelligence and automation, Python’s extensive libraries and frameworks make it a go-to choice for developers and engineers. As Python continues to gain popularity, it has become an essential skill for aspiring programmers and professionals seeking to excel in the tech industry. Python is one of the most widely used coding languages in the world. Python is used in web development, machine learning, web scraping and web automation and lots more in other fields. Let’s discuss the Coding Questions related to the basic Python programming language in detail.

  1. Write a program to print the given number is odd or even.
    Here is the Program to print the given number is odd or even:

    num = int(input("Enter a number: "))
    if (num % 2) == 0:
       print("{0} is Even".format(num))
    else:
       print("{0} is Odd".format(num))
    

  2. Write a program to find the given number is positive or negative.
    Below is the Program to find the given number is positive or negative:

    num = float(input("Enter a number: "))
    # Input: 1.2
    if num > 0:
       print("Positive number")
    elif num == 0:
       print("Zero")
    else:
       print("Negative number")
    
    #output: Positive number
    

  3. Write a program to find the sum of two numbers.
    Program to find the sum of two numbers is given below:

    num1 = int(input("Enter Number1: "))
    # Input1 : 21
    num2 = int(input("Enter Number2: "))
    #  Input2 : 11
    print("sum of given numbers is:", num1 + num2)
    #  Output2 : 32 
    

  4. Write a program to find if the given number is prime or not.
    Here is the Program to find if the given number is prime or not:

    num = int(input("enter a number: "))
    # input: 23
    flag = False
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
                flag = True
                break
    
    if flag:
        print(num, "is not a prime number")
    else:
        print(num, "is a prime number")
    # 23 is a prime number
    

  5. Write a program to check if the given number is palindrome or not.
    Below is the Program to check if the given number is palindrome or not:

    num = int(input("Enter a number: "))
    # Input: 12321
    temp = num
    reverse = 0
    while temp > 0:
        remainder = temp % 10
        reverse = (reverse * 10) + remainder
        temp = temp // 10
    if num == reverse:
      print('Palindrome')
    else:
      print("Not Palindrome")
    # Output: Palindrome
    

  6. Write a program to check if the given number is Armstrong or not.
    Below is the Program to check if the given number is Armstrong or not:

    num = int(input("Enter a number: "))
    # Input: 407
    sum = 0
    temp = num
    while temp > 0:
       digit = temp % 10
       sum += digit ** 3
       temp //= 10
    if num == sum:
       print(num,"is an Armstrong number")
    else:
       print(num,"is not an Armstrong number")
    # Output: 407 is an Armstrong number
    

  7. Write a program to check if the given strings are anagram or not.
    Here is the Program to check if the given strings are anagram or not:

    def check(s1, s2):
    	
    	if(sorted(s1)== sorted(s2)):
    		print("The strings are anagrams.")
    	else:
    		print("The strings aren't anagrams.")		
    		
    s1 = input("Enter string1: ")
    # input1: "listen"
    s2 = input("Enter string2: ")
    # input2: "silent"
    check(s1, s2)
    # Output: the strings are anagrams.
    

  8. Write a program to find a maximum of two numbers.
    Below is the Program to find a maximum of two numbers:

    def maximum(a, b):
    	
    	if a >= b:
    		return a
    	else:
    		return b
    	
    a = int(input("Enter a number: "))
    # input1: 2
    b = int(input("Enter a number: "))
    # input2: 4
    print(maximum(a, b))
    # output: 4
    

  9. Write a program to find a minimum of two numbers.
    Below is the Program to find a minimum of two numbers:

    def minimum(a, b):
    	
    	if a <= b:
    		return a
    	else:
    		return b
    	
    a = int(input("Enter a number: "))
    # input1: 2
    b = int(input("Enter a number: "))
    # input2: 4
    print(minimum(a, b))
    # output: 2
    

  10. Write a program to find a maximum of three numbers.
    Program to find a maximum of three numbers is given below:

    def maximum(a, b, c):
    
    	if (a >= b) and (a >= c):
    		largest = a
    
    	elif (b >= a) and (b >= c):
    		largest = b
    	else:
    		largest = c
    		
    	return largest
    
    
    a = int(input("Enter a number: "))
    # Input1: 10
    b = int(input("Enter a number: "))
    # Input2: 14
    c = int(input("Enter a number: "))
    # Input3: 12
    print(maximum(a, b, c))
    # Output: 14
    

  11. Write a program to find a minimum of three numbers.
    Here is the Program to find a minimum of three numbers:

    a = int(input('Enter first number  : '))
    # 12
    b = int(input('Enter second number : '))
    # 14
    c = int(input('Enter third number  : '))
    # 11
    smallest = 0
    if a < b and a < c :
        smallest = a
    if b < a and b < c :
        smallest = b
    if c < a and c < b :
        smallest = c
    print(smallest, "is the smallest of three numbers.")
    # 11 is the smallest of three numbers.
    

  12. Write a program to find a factorial of a number.
    Below is the Program to find a factorial of a number:

    num = int(input("Enter a number: "))
    # 7
    factorial = 1
    if num < 0:
       print("Sorry, factorial does not exist for negative numbers")
    elif num == 0:
       print("The factorial of 0 is 1")
    else:
       for i in range(1,num + 1):
           factorial = factorial*i
       print("The factorial of",num,"is",factorial)
       # 5040
    

  13. Write a program to find a fibonacci of a number.
    Program to find a fibonacci of a number is given below:

    nterms = int(input("How many terms? "))
    # 7
    n1, n2 = 0, 1
    count = 0
    
    if nterms <= 0:
       print("Please enter a positive integer")
    elif nterms == 1:
       print("Fibonacci sequence upto",nterms,":")
       print(n1)
    else:
       print("Fibonacci sequence:")
       while count < nterms:
           print(n1)
           nth = n1 + n2
           n1 = n2
           n2 = nth
           count += 1
    # Fibonacci sequence:
    # 0
    # 1
    # 1
    # 2
    # 3
    # 5
    # 8
    

  14. Write a program to find GCD of two numbers.
    Here is the Program to find GCD of two numbers:

    def gcd(a, b):
    
    	if (a == 0):
    		return b
    	if (b == 0):
    		return a
    
    	if (a == b):
    		return a
    
    	if (a > b):
    		return gcd(a-b, b)
    	return gcd(a, b-a)
    
    a = 98
    b = 56
    if(gcd(a, b)):
    	print('GCD of', a, 'and', b, 'is', gcd(a, b))
    else:
    	print('not found')
    

  15. Write a program to print the following pattern.
    Below is the Program to print the following pattern:

def myfunc(n):
	for i in range(0, n):
		for j in range(0, i+1):
			print("* ",end="")
		print("\r")

n = 5
myfunc(n)
  1. Write a program to print the following pattern.
    Here is the Program to print the following pattern:
def myfunc(n):
    k = n - 1
    for i in range(0, n):
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        for j in range(0, i+1):
            print("* ", end="")
        print("\r")
n = 5
myfunc(n)
  1. Write a program to print the following pattern.
    Below is the Program to print the following pattern:
def num(n):
	num = 1
	for i in range(0, n):
		num = 1
		for j in range(0, i+1):
			print(num, end=" ")
			num = num + 1
		print("\r")
n = 5
num(n)
  1. Write a program to print the following pattern.
    Below is the Program to print the following pattern:
def num(n):
	num = 1
	for i in range(0, n):
		for j in range(0, i+1):
			print(num, end=" ")
			num = num + 1
		print("\r")

n = 5
num(n)
  1. Write a program to print the following pattern.
    Here is the Program to print the following pattern:
def alphapat(n):
	num = 65
	for i in range(0, n):
		for j in range(0, i+1):
			ch = chr(num)
			print(ch, end=" ")
		num = num + 1
	
		print("\r")
n = 5
alphapat(n)
  1. Write a program to print the following pattern.
    Program to print the following pattern is given below:
def contalpha(n):
    num = 65
    for i in range(0, n):
        for j in range(0, i+1):
            ch = chr(num)
            print(ch, end=" ")
            num = num + 1
        print()
n = 5
contalpha(n)

FAQ Related to Top 20 Python Coding Questions and Answers for Programming

Below are some of the FAQs related to Top 20 Python Coding Questions and Answers for Programming:

1. What are some common Python coding interview questions?
Common Python coding interview questions include:

  • Write a function to reverse a string.
  • How do you remove duplicates from a list?
  • Explain the difference between a list and a tuple.
  • How do you implement a stack using a list?
  • Write a function to check if a number is prime.

2. How can I prepare for Python coding interviews?
To prepare for Python coding interviews:

  • Practice solving coding problems on platforms like LeetCode, HackerRank, and CodeSignal.
  • Review Python basics and advanced concepts.
  • Understand common data structures and algorithms.
  • Work on real-world projects to enhance problem-solving skills.
  • Participate in mock interviews.

3. What is the best way to learn Python for beginners?
For beginners, the best way to learn Python includes:

  • Taking online courses on platforms like Coursera, Udemy, or Codecademy.
  • Reading books such as "Automate the Boring Stuff with Python" and "Python Crash Course."
  • Practicing coding problems on websites like LeetCode and HackerRank.
  • Building small projects to apply concepts practically.

4. What is the difference between a list and a tuple in Python?
The main differences between a list and a tuple are:

  • Mutability: Lists are mutable (can be modified), whereas tuples are immutable (cannot be modified).
  • Syntax: Lists use square brackets [], while tuples use parentheses ().
  • Performance: Tuples are generally faster than lists due to their immutability.

Conclusion
In the ever-evolving landscape of programming, mastering Python can open doors to numerous opportunities and projects across various domains. This guide has covered a range of Python coding questions and answers, addressing fundamental concepts, common interview problems, and practical coding techniques. By exploring these topics, you can build a strong foundation in Python, enhancing your ability to write efficient, effective code and solve complex problems.

As you continue to practice and apply what you’ve learned, remember that coding is both an art and a science. Developing proficiency in Python requires not only understanding theoretical concepts but also gaining hands-on experience through real-world projects and challenges. Engage with the Python community, contribute to open-source projects, and continuously seek to improve your skills. To practice more problems you can check out MYCODE | Competitive Programming at Prepbytes.

Leave a Reply

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