Python Decision making and Loops

Python Decision making and Loops

Let us see about Python decision making and loops

  1. Decision making
  2. Loops

Watch on YouTube: https://youtu.be/A2qoypUV-ZE

Learn Python in Tamil | பைதான் மொழியைக் கற்றுக்கொள்ளுங்கள் – Python type casting and operators.

Decision making

Decision making evaluates multiple expressions which produce TRUE or FALSE as an outcome.

Which defines action to take and what statements has to execute when the outcome is TRUE or FALSE otherwise.

Note: Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value

Decision making statements are as follows:

  1. If statement
  2. If-else statement
  3. Nested if statement

If Statement

# if statement

# When you want to check some logical condition and perform actions based on positive result.

x = 8
y = 10

if x > y:
    print("X is greater")
    print(f"{x}")

print("Program ended")

If-else statement

# if-else statement

# An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE.

x = 80
y = 10

if x > y:
    print("X is greater")
else:
    print("Y is greater")

print("Program Ended")

Nested if-else statement

# nested/multiple if-else statement

# When you want to check multiple conditions and perform actions based on the result (TRUE or FALSE).

x = 80
y = 10
z = 90

# Multiple if

if x > y:
    print("X is greater")
elif x > z:
    print("X is greater")
elif y > z:
    print("Y is greater")
else:
    print("Z is greater")

# Nested If

if x > y:
    if x > z:
        print("x is greater")
    else:
        print("z is greater")
elif y > z:
    print("y is greater")
else:
    print("z is greater")

print("Program Ended")

Loops

A loop statement allows us to execute a statement or group of statements multiple times.

Along with above loops, we also have the following 3 keywords used during Loops.

  1. Break
  2. Continue
  3. Pass

While loop

# While loop repeatedly executes a target statement as long as a given condition becomes true.

# Example 1 (Simple while loop)
count = 1

while count < 4:
    print(count)
    count = count + 1

print("Out of while loop")
print("------------------------------")

# Example 2 : Using else Statement with While Loop

count = 0
while count < 4:
    print(count)
    count = count + 1
else:
    print("count is less than 4")

For loop

# For loop
# it has the ability to iterate over the items of any sequence,
# such as a list or a string.

# We can iterate Collection items
# Such as List, Tuple, Set and so on.

# Example 1 : looping list
blog_websites = ["aryanz.co.in", "balamt.in", "wecancode.live"]

for x in blog_websites:
    print(x)


# Example 2 : looping string value
name = "John"
for letter in name:
    print(letter)


# Example 3: Using else in For loop

for i in range(1,4):
    print(i)
else:
    print(f"Reached the max range")

Nested loop

# Nested loop
# looping inside another loop is called nested loop

# What is the output of the following code? - Comment your answer here > https://youtu.be/A2qoypUV-ZE

for x in range(1, 5):
    for y in range(x-1, x): # 1, 4
        print(f"{y}")
    print(f"\t")
Python decision making & loops - Learn Python in Tamil www.aryanz.co.in
Python decision making & loops – Learn Python in Tamil www.aryanz.co.in
Share this Post