Python Programming
EXERCISE
GROUP (Until this Quarter Ends) Grade 10 - Mercy
EXERCISE
GROUP (Until this Quarter Ends) Grade 10 Grace
EXERCISE (group 5 plus 2)
GROUP (Until this Quarter Ends) Grade 10 Grace
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
Converting Input to any data type
A = input(“one number: “)
Print(”Result: “, A)
In order to convert the input, you will need to specify what
data type to use before the “input”
A = int(input(“one number: “))
Print (“result: “, A)
Assignment (By Group)
Create a program that will input 2
numbers and add them:
Output:
Name/s:
Date:
Enter first number: 1
Enter second number: 2
Result: 3
Create a program that will input your
name and date and age and print it
Output:
Name/s:
Date:
Age:
Quiz
1-3. What are the 3 statements
4-5. Give the code for inputting a
username
Activity by Group
Create a program that will loop
your input using While Loop
Using if…else statements create a
program that if your input is equal to 100
then it will print “congratulations”. Else, it
should print “Boo”. Else if, it reaches 50, it
should print “almost there”
Using if…else statements create a
program that if your input is equal to 75
to 100 then it will print “Passed”. Else if, it
reaches 74 to 60 , it should print “Failed”
Else, it should print “Zero”.