Python Conditions, If statements and Loops
Python Conditions and Loops
Introduction
- Python supports common logical conditions:
- Equals: a==b
- Not Equals: a!=b
- Less than: a < b
- Less than or equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
- These conditions are used in "if statements" and loops.
If Statement
- An "if statement" is written using the
if keyword. - Example:
python
a = 33
b = 200
if b > a:
print("b is greater than a") # prints “b is greater than a”
- Python uses indentation (whitespace) to define scope, unlike other languages that use curly brackets. An indentation error will occur if indentation rules are not followed.
python
a = 33
b = 200
if b > a:
print("b is greater than a") #IndentationError: expected an indented block
Elif Statement
- The
elif keyword signifies "if the previous conditions were not true, then try this condition". - Example:
python
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Else Statement
- The
else keyword catches anything not caught by preceding conditions. - Example:
python
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
else can be used without elif.- Example:
python
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Short Hand If
- Single statement
if can be on the same line:
python
if a > b: print("a is greater than b")
- Single statement
if and else can be on the same line:
python
a = 2
b = 330
print("A") if a > b else print("B")
- Multiple
else statements on the same line:
python
a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")
And Operator
- The
and keyword combines conditional statements. - Example:
python
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
Or Operator
- The
or keyword combines conditional statements. - Example:
python
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
Nested If
if statements inside if statements.- Example:
python
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
Pass Statement
- The
pass statement avoids errors when an if statement has no content. - Example:
python
a = 33
b = 200
if b > a:
pass
While Loop
- Executes a set of statements as long as a condition is true.
- Example:
python
i = 1
while i < 6:
print(i)
i += 1
- Remember to increment the loop variable to avoid infinite loops.
Break Statement
- Stops the loop even if the while condition is true.
- Example:
python
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
Continue Statement
- Stops the current iteration and continues with the next.
- Example:
python
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
While - Else
- The
else block executes when the condition is no longer true. - Example:
python
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
For Loops
- Iterates over a sequence (list, tuple, dictionary, set, string).
- Works like an iterator method.
- Executes a set of statements for each item in the sequence.
- Example:
python
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
- No indexing variable is needed.
- Example: Looping through a string
python
for x in "banana":
print(x)
Break Statement With For Loop
- Stops the loop before it finishes.
- Example:
python
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
- The break can come before the print statement.
python
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x) # Prints: apple
Continue Statement With For Loop
- Stops the current iteration and continues with the next.
- Example:
python
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
range() function
- Loops through a set of code a specified number of times.
- Returns a sequence of numbers, starting from 0 (default), incrementing by 1 (default), and ending at a specified number.
- Example:
python
for x in range(6):
print(x) # Prints: 0 1 2 3 4 5
- Can specify the starting value:
python
for x in range(2, 6):
print(x) # Prints: 2 3 4 5
- Can specify the increment value:
python
for x in range(2, 30, 3):
print(x) # Prints: 2 5 8 11 14 17 20 23 26 29
For - Else
- The
else block executes when the loop is finished. - Example:
python
for x in range(6):
print(x)
else:
print("Finally finished!")
Nested For
- A loop inside a loop.
- The inner loop executes once for each iteration of the outer loop.
- Example:
python
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y) #Prints: red apple red banana red cherry big apple big banana big cherry tasty apple tasty banana tasty cherry
Pass Statement In For Loop
- The
pass statement avoids errors when a for loop has no content. - Example:
python
for x in [0, 1, 2]:
pass