Make delicious recipes!

Conditions in Python

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")
  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.

7 8
x is in arr3

The below example shows the use of more conditions in python.
  1. Comparison of lists
  2. Use of operators: 'is' and '=='
  3. 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) )


Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal