0% found this document useful (0 votes)
4 views9 pages

lec2-preview

The document explains the use of conditional statements in Python, including if, if...else, and if...elif...else structures for decision making. It also covers the while and for loops, detailing their syntax and providing examples to illustrate their functionality. Key concepts include the importance of indentation and the evaluation of test expressions to control the flow of execution.

Uploaded by

Michael Chu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

lec2-preview

The document explains the use of conditional statements in Python, including if, if...else, and if...elif...else structures for decision making. It also covers the while and for loops, detailing their syntax and providing examples to illustrate their functionality. Key concepts include the importance of indentation and the evaluation of test expressions to control the flow of execution.

Uploaded by

Michael Chu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

9/22/22, 2:03 PM Python if, if...else, if...elif...

else and Nested if Statement

What is if...else statement in Python?


Decision making is required when we want to execute a code only if a certain condition is
satisfied.

The if…elif…else statement is used in Python for decision making.

Python if Statement Syntax

if test expression:
statement(s)

Here, the program evaluates the test expression and will execute statement(s) only if the test
expression is True .

If the test expression is False , the statement(s) is not executed.

In Python, the body of the if statement is indicated by the indentation. The body starts with an
indentation and the first unindented line marks the end.

Python interprets non-zero values as True . None and 0 are interpreted as False .

Python if Statement Flowchart

Flowchart of if statement in Python programming

Example: Python if Statement


https://www.programiz.com/python-programming/if-elif-else 1/4
9/22/22, 2:03 PM Python if, if...else, if...elif...else and Nested if Statement

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")

Run Code

When you run the program, the output will be:

3 is a positive number
This is always printed
This is also always printed.

In the above example, num > 0 is the test expression.

The body of if is executed only if this evaluates to True .

When the variable num is equal to 3, test expression is true and statements inside the body of
if are executed.

If the variable num is equal to -1, test expression is false and statements inside the body of if

are skipped.

The print() statement falls outside of the if block (unindented). Hence, it is executed
regardless of the test expression.

https://www.programiz.com/python-programming/if-elif-else 2/4
9/22/22, 2:03 PM Python if, if...else, if...elif...else and Nested if Statement

Python if...else Statement


Syntax of if...else

if test expression:
Body of if
else:
Body of else

The if..else statement evaluates test expression and will execute the body of if only when
the test condition is True .

If the condition is False , the body of else is executed. Indentation is used to separate the
blocks.

Python if..else Flowchart

Flowchart of if...else statement in Python

Example of if...else

https://www.programiz.com/python-programming/if-elif-else 3/4
9/22/22, 2:03 PM Python if, if...else, if...elif...else and Nested if Statement

# Program checks if the number is positive or negative


# And displays an appropriate message

num = 3

# Try these two variations as well.


# num = -5
# num = 0

if num >= 0:
print("Positive or Zero")
else:
print("Negative number")

Run Code

Output

Positive or Zero

In the above example, when num is equal to 3, the test expression is true and the body of if

is executed and the body of else is skipped.

If num is equal to -5, the test expression is false and the body of else is executed and the
body of if is skipped.

If num is equal to 0, the test expression is true and body of if is executed and body of else is
skipped.

https://www.programiz.com/python-programming/if-elif-else 4/4
9/22/22, 2:05 PM Python while Loop

What is while loop in Python?


The while loop in Python is used to iterate over a block of code as long as the test expression
(condition) is true.

We generally use this loop when we don't know the number of times to iterate beforehand.

Syntax of while Loop in Python

while test_expression:
Body of while

In the while loop, test expression is checked first. The body of the loop is entered only if the
test_expression evaluates to True . After one iteration, the test expression is checked again.
This process continues until the test_expression evaluates to False .

In Python, the body of the while loop is determined through indentation.

The body starts with indentation and the first unindented line marks the end.

Python interprets any non-zero value as True . None and 0 are interpreted as False .

Flowchart of while Loop

https://www.programiz.com/python-programming/while-loop 1/3
9/22/22, 2:05 PM Python while Loop

Flowchart for while loop in Python

Example: Python while Loop

# Program to add natural


# numbers up to
# sum = 1+2+3+...+n

# To take input from the user,


# n = int(input("Enter n: "))

n = 10

# initialize sum and counter


sum = 0
i = 1

while i <= n:
sum = sum + i
i = i+1 # update counter

# print the sum


print("The sum is", sum)

Run Code

When you run the program, the output will be:

Enter n: 10
The sum is 55

In the above program, the test expression will be True as long as our counter variable i is
less than or equal to n (10 in our program).
https://www.programiz.com/python-programming/while-loop 2/3
9/22/22, 2:05 PM Python while Loop

We need to increase the value of the counter variable in the body of the loop. This is very
important (and mostly forgotten). Failing to do so will result in an infinite loop (never-ending
loop).

Finally, the result is displayed.

https://www.programiz.com/python-programming/while-loop 3/3
9/22/22, 2:02 PM Python for Loop

What is for loop in Python?


The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable
objects. Iterating over a sequence is called traversal.

Syntax of for Loop

for val in sequence:


loop body

Here, val is the variable that takes the value of the item inside the sequence on each iteration.

Loop continues until we reach the last item in the sequence. The body of for loop is separated
from the rest of the code using indentation.

Flowchart of for Loop

Flowchart of for Loop in Python

Example: Python for Loop


https://www.programiz.com/python-programming/for-loop 1/2
9/22/22, 2:02 PM Python for Loop

# Program to find the sum of all numbers stored in a list

# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

# variable to store the sum


sum = 0

# iterate over the list


for val in numbers:
sum = sum+val

print("The sum is", sum)

Run Code

When you run the program, the output will be:

The sum is 48

https://www.programiz.com/python-programming/for-loop 2/2

You might also like