Upload
cuthbert-stanley-gibson
View
220
Download
0
Tags:
Embed Size (px)
Citation preview
Warm Up Practice Write a program that continually asks the user for a
price value
When the user enters the word “end” you should end the program and add the total price of all the products
In addition, display the highest priced item and the lowest priced item
Count Controlled Loops Previously, we learned the “while” loop, which we
defined as the condition controlled loop
It is so called “condition controlled” because it iterates the number of times in which a Boolean expression holds True
Today, we will look at what is called a “count controlled” loop
Count Controlled Loops It is called “count controlled” because it iterates a
specific number of times
It is not dependent on the truth value of a Boolean expression, or condition
Count Controlled Loop Now, it’s important to remember that a lot of times
in Python, we can accomplish the same tasks with various different methods i.e. ELIF’s versus nested IF’s
In the same way, a count controlled loop can be created by using a “while” loop
These various methods are really for convenience’s sake
(We should also note that a while loop can be duplicated by a function controlled loop)
Count Controlled LoopExample:
counter = 0
while counter < 5:
print (“this will print 5 times!”)
counter +=1
Lists Python has such things called “lists”
Lists are denoted by brackets [ a, b, c, d ] and each item in the list is separated by commas
Lists can be stored and named as variables
Example:
x = [a, b, c, d]
Lists One important thing to note about lists is that they
can hold various data types all at once
Example:
list = [“name”, “word”, 1, 2, 3]
The ‘’for’’ loop The “for” loop is Python’s native count controlled
loop
Example:
for num in [1, 2, 3, 4, 5]:
print(“this will also print 5 times”)
The ‘’for’’ loop The “for” keyword starts the loop
The “num” is the name of the target variable
“in” is another keyword
[1, 2, 3, 4, 5] is the list of items to iterate over
Note the indentation
The ‘’for’’ loop The “for” loop will iterate once for each item in the
list passed to it when the loop begins
During the first iteration, the target variable will assume the value of the first item in the list
During the second iteration, it will assume the second item in the list
This continues until you reach the end of the list
The ‘’for’’ loop
for name in [“Josh”, “Jeen”, “Nicole”]:
print(“My favorite student is”, name)
>> My favorite student is Josh
My favorite student is Jeen
My favorite student is Nicole
Practice – Make Dat DOE Write a program that asks the user how much
money they made from Monday to Sunday
The program should specify the day each time it asks the user for a value
Then sum up the total amount they made and print out
Practice – Mechanics Rewrite the following loop as a “while” loop:
for x in [10, 20, 30, 40]:
print (x)
The range( ) Function So far, we’ve been TIRELESSLY writing out lists of
pre-defined values in our “for” loops
The range( ) function allows us to dynamically generate lists based on pre-determined criteria
The range( ) Function
for x in range(5): >> iteration # 0
print(“iteration #”, x) iteration # 1
iteration # 2
iteration # 3
iteration # 4
The range( ) Function The range( ) function takes at least one argument
In it’s simplest form, it takes a single integer
The range( ) function returns what we can think of as a list in Python
When passed a single integer, it will return a list of integers from 0 to the number specific minus one
The range( ) Function However, the range( ) function can behave in
different ways
It can take two arguments, which sets a start and an end value
By default, the function increments by 1
range (1, 5) [ 1, 2, 3, 4]
range (5, 10) [ 5, 6, 7, 8, 9]
The range( ) Function You can also pass three arguments and set a start
value, an end value, and a step value
range(0, 10, 2) [0, 2, 4, 6, 8]
range(1, 15, 3) [1, 4, 7, 10, 13]
If the step count does not perfectly fall on the end value, it will just include the number before the end value is reached
The range( ) Function Lastly, you can ask the range( ) function to count backwards by passing in a negative step count
Import ‘’time’’ We’ll talk more about this later, but another module
we can import into Python is “time”
We can ask Python to pause (for dramatic effect) by calling the sleep( ) function in time
The sleep( ) function can take one argument, denoting the number of seconds to “sleep”
**sleep( ) can also take floats
Import ‘’time’’ import time
for x in range(10, 0, -1):
print(x)
time.sleep(1)
print(“HAPPY NEW YEAR!”)
Loop Targets In a “for” loop, we generally use the target variable
as a reference value for some kind of calculation
Remember that the value of the target variable changes with each iteration of the loop
Practice – Squares Write a program that calculates the square of the
numbers between 1 and 10
Print out the number and it’s square as your loop iterates
Practice – Stair Master Write a program that prints out the following:
** (2 stars)
**** (4 stars)
****** (6 stars)
******** (8 stars)
********** (10 stars)
************ (12 stars)
Practice – Divisibility Write a program that asks the user for an integer
Then print out all numbers that are divisible by that number from 1 to 1,000
Practice – Divisibility (extension) Extend your divisibility program to check for all
integers from 1 to 10,000 that are divisible by two different integers simultaneously
Print out 10 numbers per line
User Controlled Ranges Sometimes, we need to ask the user to control the
# of iterations within a loop
You can do this by substituting a variable within the range( ) function to control the start, end, and step values of the list that will be generated
User Controlled Ranges x = int(input(“start value: “))
y = int(input(“end value: “))
z = int(input(“step value: “))
range(x, y, z)
User Controlled Ranges We can also just put the input( ) function directly
into the range( ) function
However, we must remember to convert it into an integer