Examples

1. Palindrome

  1. Check whether given string is Palindrome or not

"""Python Program to Check Whether a String is Palindrome or Not"""
# Program to check if a string is palindrome or not
# 1) Find reverse of string
# 2) Check if reverse and original are same or not
# Driver code
s = "malayalam"
if s == s[::-1]:
    print("The string is a palindrome.")
else:
    print("The string is not a palindrome.")

2. Even-Odd

  1. Check Given Number is Even or Odd

"""Python Program to Check if a Number is Odd or Even"""
# Python program to check if the input number is odd or even.
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.

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

3. Factorials

  1. Calculate Factorial of a Given Number

"""Python Program to Find the Factorial of a Number"""
# Python program to find the factorial of a number provided by the user.

# change the value for a different result
num = 7

# To take input from the user
# num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero
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)

4. Prime Numbers

  1. Check Number is Prime or Not

"""Python Program to Check Prime Number"""

# Program to check if a number is prime or not

num = 307

# To take input from the user
# num = int(input("Enter a number: "))
# prime numbers are greater than 1
if num > 1:
    # check for factors
    for i in range(2, num):
        if (num % i) == 0:
            print(num, "is not a prime number")
            print(i, "times", num // i, "is", num)
            break
    else:
        print(num, "is a prime number")

# if input number is less than
# or equal to 1, it is not prime
else:
    print(num, "is not a prime number")
  1. Generate Prime Numbers between 1 to 100

"""Python Program to Print all Prime Numbers in an Interval"""
# Python program to display all the prime numbers within an interval

lower = 1
upper = 100

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):
    # all prime numbers are greater than 1
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
                break
        else:
            print(num)

5. Fibonacci Sequence

  1. Print Fibonacci sequence for a given range

"""Python Program to Print the Fibonacci sequence"""
# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms
n1, n2 = 0, 1
count = 0

# check if the number of terms is valid
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
       # update values
       n1 = n2
       n2 = nth
       count += 1

6. Armstrong Number

  1. Check Give number is Armstrong Number or not

"""Python Program to Check Armstrong Number"""
# abcd... = an + bn + cn + dn + ...
# 153 = 1*1*1 + 5*5*5 + 3*3*3  // 153 is an Armstrong number.
# 407
"""Source Code: Check Armstrong number (for 3 digits)"""
# Python program to check if the number is an Armstrong number or not

# take input from the user
num = int(input("Enter a number: "))

# initialize sum
sum = 0

# find the sum of the cube of each digit
temp = num
while temp > 0:
   digit = temp % 10
   sum += digit ** 3
   temp //= 10

# display the result
if num == sum:
   print(num,"is an Armstrong number")
else:
   print(num,"is not an Armstrong number")

7. Transpose a Matrix

  1. Python Program to Transpose a Matrix

"""Python Program to Transpose a Matrix"""
# Program to transpose a matrix using a nested loop

X = [[12,7],
    [4 ,5],
    [3 ,8]]

result = [[0,0,0],
         [0,0,0]]

# iterate through rows
for i in range(len(X)):
   # iterate through columns
   for j in range(len(X[0])):
       result[j][i] = X[i][j]

for r in result:
   print(r)

Interview Questions

Descriptive

  1. Why is Python Preferred over JAVA or C++?

  2. What is the difference between module and package in python?

  3. How can I run a function over a list of items?

  4. How to find address of a variable?

  5. What is list comprehension and dictionary comprehension?

  6. How to avoid redundancy of items in a list?

  7. What is the difference between list and tuple?

  8. What is a Lambda function? What is the return type of it?

  9. What is a Pickle and its utility?

  10. Give the difference between range and xrange?

  11. Give difference between Iterators and Generator?

  12. What are decorators and there utility?

  13. Which is faster List and Tuple and why?

  14. Python follows depth first search or breadth first search algorithm, with example?

  15. What is slicing and negative Indexing?

  16. What is the difference between Static Methods and Class Method?

  17. Write a program for traversing binary tree in Python?

  18. Write a program for checking number is Prime or not?

  19. How Zip, Map, Reduce and filter is used?

  20. How to connect databases with Python?

MCQ

Comming Soon…