06 - Intro to Python Part 3 with Exercise
06 - Intro to Python Part 3 with Exercise
Python
Part 3
At the end of this lecture, you should learn:
3
Conditional - if
5
Conditional – if (exercise)
What is the
output if user
What is the
insert 29?
output if user
insert -17?
6
Conditional – if (exercise)
8
Conditional – if-else
◼ The syntax of If Else Statement in Python is as follows:
if (Test condition):
# If TRUE then these statements will be executed
True statements
else :
# If FALSE then these statements will be executed
False statements
9
Conditional – if - else
10
Conditional – if - else (exercise)
11
Conditional – if – elif - else
12
Conditional – if – elif - else
if (condition 1):
statements 1
elif (condition 2):
statements 2
elif (condition 3):
statements 3
...........
elif (condition n):
statements n
else:
default 13statements
Conditional – if – elif - else
14
Conditional – if – elif - else (exercise)
21
For Loop
22
For Loop
23
For Loop example (string)
25
For loop example (list)
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0
# Output sum
print("The sum is", sum)
26
For loop example (range)
◼ You can use range with for loop
◼ range function syntax as follows
range(start, stop, step)