Control Flow and Loop

Comparison Operator

  • We use a comparison operator to compare values. It will return a boolean value

  • A comparison operator can be used to compare data of the same types

# Value for all comparison operators listed below is True

10 > 3 # Greater than
10 >=3 # Greater than or equal to
10 < 20 # Less than
10 <= 20 # Less than or equal to
10 == 10 # Equal to 
10 != "10" # Not equal to

"bag" > "apple" # When we sort these two strings bag comes after apple thats why true
print(ord("b")) # is 98
print(ord("B")) # is 66

Conditional Statements

  • If statement will contain an expression that will return a boolean(True, False) value

  • If we use an if statement, always terminate it with a colon

  • All the statement which are indented by a tab will belong to this if statement

# Self explanatory conditional statement example is here

temperature = 35
if temperature > 30:
    print("Inside if")
    print("Still inside if")
elif temperature > 20:
    print("Inside elif")
else:
    print("This is optional")
print("Outside if")

Ternary Operator

Using ternary operator we can write multiple conditions in one line itself

# Sample if condition
age = 22
if age >= 18:
    message = "Eligible"
else:
    message = "Not Eligible"
print(message)

# Same if condition using Ternary Operator
age = 22
message = "Eligible" if age >= 18 else "Not Eligible"
print(message)

Logical Operator

We have three types of logical operators: "and", "or", and "not"

"and" operator

# If both the conditions are true the result will be true

high_income = False
good_credit = True

if high_income and good_credit:
    print("Eligible")
else:
    print("Not eligible")

"or" operator

# If one of the many conditions are true the result will be true
high_income = False
good_credit = True

if high_income or good_credit:
    print("Eligible")
else:
    print("Not eligible")

"not" operator

# It inverses the value of a boolean

student = True
if not student:
    print("Eligible")
else:
    print("Not eligible")

Short Circuit Evaluation

  • In python the if statement is evaluated from left to right

  • In case of "and" if any argument is false then it stops the evaluation of further arguments

  • In case of "or" if any argument is true then it stops the evaluation of further arguments

  • In python, logical operators are short circuit

high_income = False
good_credit = True
student = True

# checked for high_income, it's False, so does not evaluate any further
if high_income and good_credit and not student: 
    print("Eligible")
else:
    print("Not eligible")

Chaining Condition Operator

age = 22

# Noraml if condition
if age >= 18 and age <65:
    print("Eligible")

# If condition using chaining
if 18 <= age < 65: # "18 <= age < 65" is called chaining comparison operator
    print("Eligible")

Loops

"range"

range(4) 
#It will return a sequence from 0 to 3

range(1,4) 
#It will return a sequence from 1 to 3

range(0,4,2) 
#It will return a sequence from 0 to 3 incremented by step of 2

"for" Loop

  • We use loops to create repetition

  • Similar to if statements we need to terminate our for loop with colon(:)

for number in range(1,4):
    print("Attempt",(number), (number) * "." )

Break Statement

count = 0
for number in range(25):
    print(number)
    if count == 10:
        break

Nested Loop

for loop within for loop

for x in range(5):
    for y in range(3):
        print("Coordinates", x, y)

Iterables

  • Iterable is an object, which one can iterate over.

  • Previously we saw range() function, which returns an object which is iterable.

# range as iteranle
for number in range(3):
    print(number)
    
# string as iterable
for character in "String":
    print(character )
    
# list as iterable (note that we can have different dtypes in lists)
for list_item in [1,"string",4,7]:
    print(list_item )

"while" Loop

number = 100
while number > 0:
    print(number)
    number = number/2

Infinite Loop

It a loop that runs forever We can break an infinite loop using a break statement

# the code will keep on taking input and printing, unless quit is the input

while True:
    command = input(">")
    print("Echo",command)
    if command.lower() == "quit":
        break

Last updated

Was this helpful?