Python
Python
Python
PYTHON
Programming
Flowchart of if statement
Syntax of if...else
if test expression:
Body of if
else: Example of if...else
Body of else # 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")
Flowchart of if...elif...else
Python Nested if statements
We can have a if...elif...else statement inside another if...elif...else statement. This is called nesting in computer
programming.
Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of
nesting. They can get confusing, so they must be avoided unless necessary.
digits = [0, 1, 5]
for i in digits:
print(i)
else: program to display student's marks from record
print("No items left.") student_name = 'Soyuj'
marks = {'James': 90, 'Jules': 55, 'Arthur': 77}
for student in marks:
if student == student_name:
print(marks[student])
break
else:
print('No entry with that name found.')