0% found this document useful (0 votes)
5 views16 pages

Grade 10 Computer Nov 4 8 1.Pptx

The document provides an overview of user input, conditional statements, and loops in Python. It explains how to use 'if', 'elif', and 'else' statements for logical conditions, as well as the functionality of 'while' and 'for' loops, including break and continue statements. Key points include the importance of indentation in Python and examples of iterating over sequences.

Uploaded by

elysiiae
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)
5 views16 pages

Grade 10 Computer Nov 4 8 1.Pptx

The document provides an overview of user input, conditional statements, and loops in Python. It explains how to use 'if', 'elif', and 'else' statements for logical conditions, as well as the functionality of 'while' and 'for' loops, including break and continue statements. Key points include the importance of indentation in Python and examples of iterating over sequences.

Uploaded by

elysiiae
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/ 16

Overview

User Input
Conditional
Statements
Loops
User Input

- Python allows for user input.


- That means we are able to ask the
user for input.
User Input

Example:
Loops

Logical Conditional Statements


Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly
in "if statements" and loops.
An "if statement" is written by using the if keyword.
Statements

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).

This is less like the for keyword in other


programming languages, and works more like
an iterator method as found in other object-
orientated programming languages.
Loops

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:

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)
if x == "banana":
break

You might also like