Python supports if condition along with elif and else clauses.
x, y = 7,8 # multiple variables can be assigned in one statements
print ( str(x) + " " + str(y) )
# if, elif and else statements
arr1 = [1,2,3]
arr2 = [4,5,6]
arr3 = [7,8,9]
if x in arr1: # <var> in <list> can be used to check
print ("x is in arr1") # presence of <var> in the <list>
elif x in arr2:
print ("x is in arr2")
else:
print ("x is in arr3")
Note: The copy paste from the above may remove the indentation spaces due to which python can give indentation errors.
If such a thing happens, then indentation spaces have to be put manually after copy-paste.
Output:
7 8
x is in arr3
The below example shows the use of more conditions in python.
Comparison of lists
Use of operators: 'is' and '=='
The 'not' operator
# declare two lists with similar contents
arr1 = [1,2,3]
arr2 = [1,2,3]
arr3 = arr1 # arr3 and arr1 are now two references to the same variable
print ( arr1 == arr2 ) # '==' in lists compares elements for equality
print ( arr1 is arr3 ) # 'is' returns true only when the operands refer to the same object
# "not" operator
print ( not (arr1 == arr2) )
print ( not (arr1 is arr3) )
Output:
True
True
False
False
Got a thought to share or found a bug in the code? We'd love to hear from you: