Interview Questions

Ans.:

# Python | Print prime no between 1 to 100

This is an example of how to print all prime numbers between 1 and 100 using for loop.

A prime number is a natural number greater than 1 which has no other factors except 1 and the number itself.
The first few prime numbers are {2, 3, 5, 7, ….}

``````n = 100
total = 0
for i in range(2, n):
flag = True
for j in range(2,i):
if i%j == 0:
flag = False
break

if(flag == True):
total +=1
print(i)

print(f'total no of prime between 1 to {n} is {total}')``````

Ans.:

# Python | Ternary Operator

python uses an if-else conditional statement in a single line to represent the Ternary Operator.

`[true] if [expression] else [false]`

Ans.:

# Python | Check whether the given value is palindrome or not

This is an example of how to check whether the given value is palindrome or not. It takes input from the user to check palindrome.

A string is said to be palindrome if the string and reverse of the string will be the same.

``````value1 = input('Enter value to check palindrome: ')

# reverse of value1
value2 = value1[::-1]

# check value and its reverses(value2) is same
if value1==value2:
print(f'{value1} is palindrome')
else:
print(f'{value1} is not palindrome')``````

Ans.:

# Python | Bubble sort program

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are not in the right order.

This is an example for bubble sort in python

``````# unordered list
l = [5,6,8,5,4,1,9,3,7]

# bubble sort approch for sorting
for i in range(len(l)-1,0,-1):
for j in range(i):
if l[j] > l[j+1]:
l[j],l[j+1] = l[j+1],l[j]

print(l)``````

Ans.:

# Python | Create an empty class

In python, it required adding a pass statement to define an empty class. The pass is a special statement in python that does nothing. It just works as a dummy statement.

Ans.:

# Python | Print  Triangle pyramid pattern

This is an example of how to print the Triangle pyramid pattern. It takes an input number from the user to print the number of rows for the pattern.

``````n = int(input('enter no:'))

for i in range(n):
print(" "*(n-i-1) + '*'*(2*i+1))``````

Ans.:

# Python | Check whether the given number is a prime number or not

This is an example of how to check whether the given number is a prime number or not. It takes an input number from the user to check the prime number

``````n = int(input('enter no.:'))
flag = True

if n > 2:
for i in range(2, n):
if n%i == 0:
flag = False

if flag == True:
print(f'{n} is a prime number')
else:
print(f'{n} is not a prime number')
else:
print('number must be > 2')``````

Ans.:

# Python | Fibonacci series

This is an example of how to print the Fibonacci series. It takes an input number from the user to print the Fibonacci series.

The Fibonacci series is a sequence of numbers where each number in the sequence is the addition to the previous two consecutive numbers.

`0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ……..`

The first two terms are 0 and 1. The rest of the terms are obtained by adding the previous two consecutive numbers. This means the nth term is the sum of (n-1)th and (n-2)th term.

``````n = int(input('enter no:'))
a,b = 0,1
if n>1:
print(a,b,end=' ')
for i in range(2,n):
a,b = b,a+b
print(b, end=' ')``````

Ans.:

# Python | Program to find factorial of a number

This is an example of how to find the factorial of a number in python.

The factorial of a number is a product of all the positive integers less than or equal to that number.
For example, the factorial of 5 is  5x4x3x2x1 = 120

Factorial is not defined for negative integers, and the factorial of zero is one, 0! = 1.

Check here for factorial recursive way

``````n = int(input('Enter no.:'))
fact = 1
for i in range(1,n+1):
fact *= i
print(fact)``````