lec2-preview
lec2-preview
if test expression:
statement(s)
Here, the program evaluates the test expression and will execute statement(s) only if the test
expression is True .
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 .
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
3 is a positive number
This is always printed
This is also always printed.
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
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.
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
num = 3
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
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
We generally use this loop when we don't know the number of times to iterate beforehand.
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 .
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 .
https://www.programiz.com/python-programming/while-loop 1/3
9/22/22, 2:05 PM Python while Loop
n = 10
while i <= n:
sum = sum + i
i = i+1 # update counter
Run Code
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).
https://www.programiz.com/python-programming/while-loop 3/3
9/22/22, 2:02 PM Python for Loop
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.
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
Run Code
The sum is 48
https://www.programiz.com/python-programming/for-loop 2/2