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

Ternary Operator

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

Logical Operator

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

"and" operator

"or" operator

"not" operator

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

Chaining Condition Operator

Loops

"range"

"for" Loop

  • We use loops to create repetition

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

Break Statement

Nested Loop

for loop within for loop

Iterables

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

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

"while" Loop

Infinite Loop

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

Last updated

Was this helpful?