Python Question bank
Python Question bank
Explain two Membership and two logical operators in python with appropriate
examples.
Example-
2) Not in - The Not in operator also returns boolean. True if value doesn’t
exist and “False” if the value exists in the data structure.
Loop- Loops in python are the repetition block of code. It will run till the
condition is not satisfied.
There are mainly 2 types of loops in python:
1) For loop -
- The for loop is a loop in which it has declaration, condition and
increment or decrement in its structure.
- This loop is helpful when the user know the ending condition
- Example -
n = int(input("Enter the number: "))
fact = 1
for i in range(1, n+1):
fact*=i
print("Factorial of "+ str(n) + " is: ", fact)
2) While loop-
- The while loop has only condition in it’s block.
- This loop is helpful when we don’t know about ending condition.
- Example -
i = 0
while i < 10:
print(i)
i+=1
Identity Operators -
- Identity Operators in python are used to check if the variables are
pointing to the same objects because the python is dynamic language
then all the values are stored as an object. There are 2 types of Identity
operators-
- Is
a) The is operator returns true if the first when both the variables are
pointing to same objects otherwise will return false
Example -
a = [5]
b = [5]
c = b
- Is not
a) The is not operator return true if both the variables are not
pointing to similar objects. Otherwise it will return to be true.
Example -
a = [5]
b = [5]
c = b
print(a not is b) # This will evaluate to True
print(c not is b) # This will evaluate to
False