if, else, and elif Statements

Read this for more on conditional statements.

8. Membership and identity operators

Membership operators: in/not in

Quite often we need to check if a value can be or cannot be found within a container, such as a list or dictionary. 

in and not in operators, known as membership operators, can help us!

Example:

num = int(input("Enter an integer:")) myContainer = [1,2,3,4,5,6,7] if num in myContainer: print("Found it! It is in myContainer!") else: print("Nope. It is not in myContainer.")


Example:

name = input("Enter a name:")
MyNamesContainer = {
"Maria" : 23,
"Anna" : 19,
"Jack" : 5,
"Alex" : 12,
"John" : 18}
if name in MyNamesContainer:
  print("Found it! It corresponds to", MyNamesContainer[name])
else: 
  print("No such name in the container.")
** Note that the keys are matched, not the values!

Identity operators: is/is not

Sometimes we want to determine whether two variables are the same object.

is and is not operators, known as identity operators, can help us out!
Identity operators return True only if the operands reference the same object (they do not compare object's values).

Example:

myContainer = [1,2,3,4,5,6,7]
otherContainer = [9,8,7,6,5,4,3,2,1]
a = myContainer
b = otherContainer
a = b
if a is myContainer:
	print("a is myContainter!")
elif a is otherContainer:
	print("a is otherContainter!")
else: 
	print("I have no idea what a is!")