Condional Statement N Loops
Condional Statement N Loops
&
LOOPS PYTHON
(BASIC)
Introduction to Conditional Statements
if condition:
1|Page
If the condition is true, the indented code block under the "if"
statement is executed.
x=7
if x > 5:
if condition:
else:
You can use the "else" statement to handle the default case
when none of the conditions in the "if" or "elif" blocks are true.
2|Page
x=3
if x > 5:
else:
The "elif" (short for "else if") statement allows you to check
multiple conditions one after another.
if condition1:
elif condition2:
elif condition3:
else:
3|Page
Example of "elif" Statement:
x = -2
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is zero")
Logical Operators:
x=6
if x > 0 and x % 2 == 0:
x=5
if x > 0:
if x % 2 == 0:
else:
else:
if x % 2 == 0:
else:
5|Page
Example of Using Parentheses:
x=5
score = 75
print(grade)
6|Page
Incorrect code:
x = 10
Python has the "is" operator to check for object identity, which
means checking if two variables refer to the same object.
x = [1, 2, 3]
y=x
if x is y:
Floating-Point Comparisons:
7|Page
Let's compare two floating-point numbers:
b = 0.3
if a == b:
else:
Python allows you to use the "in" and "not in" operators to
check for membership in collections like lists, tuples, strings, etc.
my_list = [1, 2, 3, 4, 5]
item = 3
if item in my_list:
8|Page
Example of List Comprehension with Condition:
print(squared)
score = 75
x = 10
if x < 5:
9|Page
Best Practices for Readable Code:
Conclusion:
The "if", "else", and "elif" statements provide different paths for
the code to take, depending on the conditions.
10 | P a g e
The "in" and "not in" operators help check for membership in
collections.
Following best practices for writing readable code will make your
programs more maintainable and easier to understand.
if condition:
x = 10
if x > 5:
11 | P a g e
The syntax of the "else" statement is:
if condition:
else:
x=3
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
5. The "elif" Statement:
if condition1:
elif condition2:
12 | P a g e
elif condition3:
else:
x = -2
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is zero")
7. Logical Operators:
13 | P a g e
8. Example of Logical Operators:
x=6
if x > 0 and x % 2 == 0:
x=5
if x > 0:
if x % 2 == 0:
print("x is a positive even number")
else:
print("x is a positive odd number")
else:
if x % 2 == 0:
print("x is a negative even number")
else:
14 | P a g e
print("x is a negative odd number")
11. Using Parentheses to Control Evaluation Order:
x=5
score = 75
print(grade)
15 | P a g e
16. Avoiding Assignment Mistakes:
Incorrect code:
x = 10
Python has the "is" operator to check for object identity, which
means checking if two variables refer to the same object.
x = [1, 2, 3]
y=x
if x is y:
16 | P a g e
Floating-point arithmetic may lead to small rounding errors,
causing unexpected results in comparisons.
b = 0.3
if a == b:
else:
Python allows you to use the "in" and "not in" operators to check
for membership in collections like lists, tuples, strings, etc.
my_list = [1, 2, 3, 4, 5]
item = 3
if item in my_list:
17 | P a g e
25. Example of List Comprehension with Condition:
print(squared)
score = 75
x = 10
if x < 5:
18 | P a g e
• Use meaningful variable names to make your code more
readable and self-explanatory.
• Write clear and concise conditions to avoid confusion and
bugs in your code.
• Avoid creating deeply nested conditional structures to
maintain code readability.
• Make use of comments to explain complex conditions or
reasoning behind decisions.
Conclusion:
19 | P a g e
Introduction to Loops in Python
Concept of Looping/Iteration
counter = 0
20 | P a g e
while counter < 5:
counter += 1
print(fruit)
21 | P a g e
The continue keyword works a bit differently. Instead of
terminating the loop, it immediately jumps to the next
iteration, skipping the rest of the code inside the loop for the
current iteration.
if number == 5:
break
print(number)
if number == 5:
continue
print(number)
22 | P a g e
Understanding the While loop
While Loop
while condition:
# statements
Here is an example:
i=0
while i < 5:
print(i)
i += 1
i=0
while i < 5:
23 | P a g e
print(i)
You can use a break statement to forcefully exit the loop. Here
is an example:
i=0
while i < 5:
print(i)
if i == 3:
break
i += 1
i=0
while i < 5:
print(i)
i += 1
else:
24 | P a g e
In this example, the else block executes when i is no longer
less than 5.
# Break example
i=0
while i < 5:
if i == 3:
break
print(i)
i += 1
# Continue example
i=0
25 | P a g e
while i < 5:
i += 1
if i == 3:
continue
print(i)
# Pass example
while False:
pass
26 | P a g e
For Loop
# statements
Here, variable is the variable that takes the value of the item
inside the sequence on each iteration. sequence is a list, string,
tuple, set, or any other iterable object.
print(num)
In this case, the variable num takes on the value of each item
in the list [1, 2, 3, 4, 5] sequentially and the print statement is
executed for each item.
You can use the for loop to iterate over sequences such as
strings, lists, tuples, sets, and dictionaries.
print(char)
27 | P a g e
for num in [1, 2, 3, 4, 5]: # Works the same way for tuples
print(num)
print(num)
Here's an example:
print(num)
else:
if num == 3:
break
print(num)
if num == 3:
continue
print(num)
29 | P a g e
One argument: generates numbers from 0 to the argument
minus 1.
for i in range(5):
print(i)
print(i)
print(i)
# This will print the index and value for each item in the
list.
In this example, i is the index and num is the value for each
item in the list. The enumerate() function makes it easy to get
both the index and the value in the same loop, which can be
very helpful in many situations.
30 | P a g e
Remember that for loops in Python are a powerful tool to
iterate over sequences and other iterable objects. By
understanding how to use for loops effectively, you can write
more efficient and cleaner code.
Nested Loops
31 | P a g e
for item in outer_list:
print()
123
456
789
In this example, for each iteration of the outer loop, the inner
loop runs completely. The end=' ' in the print statement is used
to print on the same line, and the print() statement without
any arguments is used to print a newline.
j=1
while j <= 3:
j += 1
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
32 | P a g e
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3
In this example, the inner while loop executes 3 times for each
single execution of the outer for loop.
Exercises
print(i * j, end='\t')
***
*****
*******
*********
height = 5
for i in range(height):
# print spaces
# print stars
33 | P a g e
for j in range(2 * i + 1):
print("*", end="")
List Comprehension
34 | P a g e
Using a for loop:
squares = []
squares.append(x**2)
35 | P a g e
Nested List Comprehension
Break Statement
36 | P a g e
Example:
if num == 5:
break
print(num)
Continue Statement
Example:
if num == 5:
continue
print(num)
In this example, the loop will print all the numbers except 5.
When the loop variable num is 5, the continue statement is
executed, and the current iteration is skipped, but the loop
continues with the next iteration.
Pass Statement
37 | P a g e
Example:
if num == 5:
pass
print(num)
In this case, pass does nothing and the number 5 is printed like
all the other numbers. The pass statement is also useful in
places where your code will eventually go, but has not been
written yet (e.g., in stub functions or loops).
38 | P a g e