Chapter 1
Chapter 1
Conditional Statements
if Statement
Sometimes the programmer needs to check the evaluation of
certain expression(s), whether the expression(s) evaluate to
True or False. If the expression evaluates to False, then the
program execution follows a different path then it would have if
the expression had evaluated to True.
Based on this, the conditional statements are further
classified into following types; if, if……else, elif, nested
if.
if Statement:
applePrice = 180
budget = 200
if (applePrice <= budget):
print("Alexa, add 1kg Apples to the cart.")
Output:
if-else Statement
An if……else statement works on the following principle,
applePrice = 210
budget = 200
if (applePrice <= budget):
print("Alexa, add 1kg Apples to the cart.")
else:
print("Alexa, do not add Apples to the cart.")
Copy
Output:
Alexa, do not add Apples to the cart.
elif Statement
Sometimes, the programmer may want to evaluate more than
one condition, this can be done using an elif statement.
num = 0
if (num < 0):
print("Number is negative.")
elif (num == 0):
print("Number is Zero.")
else:
print("Number is positive.")
Number vNested if Statement
We can use if, if….else, elif statements inside other if
statements.
Example:
num = 18
if (num < 0):
print("Number is negative.")
elif (num > 0):
if (num <= 10):
print("Number is between 1-10")
elif (num > 10 and num <= 20):
print("Number is between 11-20")
else:
print("Number is greater than 20")
else:
print("Number is zero")
Copy
Output:Number is zero
for Loop
Copy
Output:
A, b, h, i, s, h, e, k,
Copy
Copy
Output:
Red
Green
Blue
Yellow
Copy
for k in range(5):
print(k)
Copy
Output:
0
1
2
3
4
Copy
Here, we can see that the loop starts from 0 by default and
increments at each iteration.
for k in range(4,9):
print(k)
Copy
Output:
4
5
6
7
count = 5
while (count > 0):
print(count)
count = count - 1
Copy
Output:
5
4
3
2
1
Copy
Here, the count variable is set to 5 which decrements after each
iteration. Depending upon the while loop condition, we need to
either increment or decrement the counter variable (the
variable count, in our case) or the loop will continue forever.
We can even use the else statement with the while loop.
Essentially what the else statement does is that as soon as the
while loop condition becomes False, the interpreter comes out
of the while loop and the else statement is executed.
Example:
x = 5
while (x > 0):
print(x)
x = x - 1
else:
print('counter is 0')
Copy
Output:
5
4
3
2
1
while (i<=3):
for k in range(1, 4):
print(i, "*", k, "=", (i*k))
i = i + 1
print()
Copy
Output:
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
Copy
Copy
Output:
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9Break Statement
The break statement in Python brings control out of the loop.
Example: In this Python code, it iterates through the characters of
the string ‘geeksforgeeks’. When it encounters the
characters ‘e’ or ‘s’, it uses the break statement to exit the loop.
After the loop is terminated, it prints “Current Letter :” followed
by the last character encountered in the loop (either ‘e’ or ‘s’). So,
the output will display “Current Letter :” followed by the first
occurrence of ‘e’ or ‘s’ in the string.
Python3
break
Output
Current Letter : e
Pass Statement
We use pass statement in Python to write empty loops. Pass is also
used for empty control statements, functions and classes.
Example: This Python code iterates through the characters of the
string ‘geeksforgeeks’ using a ‘for' loop. However, it doesn’t
perform any specific action within the loop, and
the ‘pass' statement is used. After the loop, it prints “Last
Letter :” followed by the last character in the string, which is ‘s’.
Python3
pass
Output
Last Letter : s
How for loop in Python works internally?
Before proceeding to this section, you should have a prior
understanding of Python Iterators.
Firstly, lets see how a simple for loop looks like.
Example: This Python code iterates through a list called fruits,
containing “apple”, “orange” and “kiwi.” It prints each fruit
name on a separate line, displaying them in the order they appear
in the list.
Python3
print(fruit)
Output
apple
orange
kiwi
Here we can see the for loops iterates over iterable object fruit
which is a list. Lists, sets, dictionaries are few iterable objects while
an integer object is not an iterable object. For loops can iterate
over any of these iterable objects.
This Python code manually iterates through a list of fruits using an
iterator. It prints each fruit’s name one by one and stops when
there are no more items in the list.
Python3
iter_obj = iter(fruits)
while True:
try:
fruit = next(iter_obj)
print(fruit)
except StopIteration:
break