Lesson 7

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Lesson 7: Conditionals,

Loops, and Input


Objectives

After studying this lesson, you


should be able to:
• define what conditionals and loops
are;
• use conditionals and loops; and
• allow user input in your programs.
Learning Essential Question:

How do we use conditionals, loops


and inputs in real-life scenarios?
PyCharm EditorClick the project root in the Project tool
window > New > Python File.
Select Python file from the popup
window, and then type the new filename.
Run the program (Shift-Alt-F10) or press
the run button.
Like every other programming language, Python also has
some predefined conditional statements. A conditional
statement as the name suggests itself, is used to handle
conditions in your program. These statements guide the
program while making decisions based on the conditions
encountered by the program.

Python has 3 key Conditional Statements that you should


know:

if statement
if-else statement
if-elif-else ladder

Output
Nesting Conditions

A nested conditional statement is an if or if


else statement inside another if else
statement.

Syntax

Output
The elif is short for else if. It allows us to check for
multiple expressions. If the condition for if is False , it
checks the condition of the next elif block and so on.We
can further refine the code using the elif keyword so we
can check for more than one condition.
Syntax

Output
Logical Operators

Syntax Output
Comparing with the in Keyword
Say we want to check a letter if it
is a vowel or not. We can
manually set multiple
conditions using if, elif, and else:

A shorter version of this uses


the or keyword.

You can also use the in keyword.


Loops- is used to create a repetition until a
particular condition is met.

The while keyword allows us to execute code


over and over until if a condition is true and
met.
Output
Syntax
We can also create loops using the for and in
keywords. Let’s say we want to print each letter
in the string “kerfuffle”
Syntax

A for loop in Python is used


to iterate over a sequence Output
(list, tuple, set, dictionary, and
string).
Iterating through Collections
Syntax Output

Tuples

Lists

Sets

Dictionaries
Break and Continue
The break keyword is used when you want to loop
something indefinitely until a condition is met.
Syntax Output

The continue keyword skips running


the code for an iteration.
Syntax Output
The range() function creates a
sequence of numbers. It takes on
three arguments:
• start - The start of the
sequence
• stop - The end of the sequence
• step - How much the range
increments
Input

• The input() function allows us to show a


prompt and allow users to provide some
information.
• The function takes on a parameter for a
prompt.

Syntax

Output
Simple Guessing Game

For this program, we had to import the random


module, which allows us to generate numbers
randomly. We also had to convert the number
(which is generated as an integer) to a string
since the input() function specifies numeric
inputs as strings.
PERFORMANCE TASK:
CODES IN A BOOKMARK! FRONT

In a bookmark, create a sample BACK

program using either nested


conditionals, logical operators,
Loops, Range or Input Function.
Write at least 8- 10 lines of code.

Deadline: January 04, 2023


SIZE: 2”X 10 inches
Materials: Photo Paper
( MUST BE LAMINATED )

You might also like