Control Flow: The if, elif, else Statements

is Operator

Python is an object oriented programming language. Each object-data type, i.e. class, can be used to create individual instances. Recall that when we define functions they are not executed until a function call statement is encountered in the program. Similarly the class instances are definitions of different types such as student, course, homework, etc. When an instance is to be created from the student class, that instance is used for keeping the individual data of an actual student, where two different student instances will have different values for the same data types such as student_number, student_name, email_address, and so on.

Two data type instance can be compared to each other to check whether the two are the same instances in the program using the boolean is operator:

a = "banana"
b = "banana"

if (a is b):
    print("a is the same instance as b")

a = [1,2,3]
b = [1,2,3]

if (a is not b):
    print("a is not the same instance as b")

Although the two lists contain the same elements, they are different list instances, hence is evaluates to False and is not evaluates to True. On the other hand, strings are mutable data structures which means any two strings having the same sequence of characters are treated as a single instance. If an existing string is modified, then it is automatically assigned to a different location in memory.

Two tuples or lists can be compared using the comparison operators where the result is based on the comparison of respective items in the tuples:

print ((0, 1, 2) > (0, 3, 4))         # False
print ([0, 1, 2] > [0, 3, 4])         # False
print ((0, 1, 2000000) >  (0, 3, 4))  # False

Recall that multiple variables can be assigned in a single statement, the same is applicable to assignment to and from the tuples and lists:

my_list = [ 'have', 'fun']

(x, y) = my_list

print(x)
print(y)

list_of_tuples = [(1,2,3), ("p", "q")]

a, b = list_of_tuples

print(a)
print(b)