Grade 10 Computer Nov 4 8 1.Pptx
Grade 10 Computer Nov 4 8 1.Pptx
User Input
Conditional
Statements
Loops
User Input
Example:
Loops
1. If Statement
An "if statement" is written by
using the if keyword.
a = 33
b = 200
if b > a:
print("b is greater than a")
Statements
Take Note
Python relies on indentation (whitespace at the
beginning of a line) to define scope in the code.
Other programming languages often use curly-
brackets for this purpose.
a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
Statements
2. Elif Statement
The elif keyword is Python's way of saying "if
the previous conditions were not true, then try
this condition".
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Statements
3. Else Statement
The else keyword catches anything which isn't
caught by the preceding conditions.
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Statements
Take Note
You can also have an else without the elif:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Loops
1. While Loops
With the while loop we can execute a set of
statements as long as a condition is true.
i = 1
while i < 6:
print(i)
i += 1
Loops
1. While Loops
Break Statements
With the break statement we can stop the loop
even if the while condition is true:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
Loops
1. While Loops
Continue Statements
With the continue statement we can stop the
current iteration, and continue with the next:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Loops
2. For Loops
A for loop is used for iterating over a sequence
(that is either a list, a tuple, a dictionary, a set,
or a string).
2. For Loops
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Loops
2. For Loops
Even strings are iterable objects, they contain a
sequence of characters:
for x in "banana":
print(x)
Loops
2. For Loops
Break Statements
With the break statement we can stop the loop
before it has looped through all the items: