Download presentation
Presentation is loading. Please wait.
Published byCuthbert Stanley Gibson Modified over 9 years ago
1
Count Controlled Loops Look at the little children … Why is the sun’s face features orange …
2
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
3
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
4
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
5
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)
6
Count Controlled Loop Example: counter = 0 while counter < 5: print (“this will print 5 times!”) counter +=1
7
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]
8
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]
9
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”)
10
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
11
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
12
The ‘’for’’ loop for x in [1, 2, 3, 4, 5]: >> 1 print (x) 2 3 4 5
13
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
14
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
15
Practice – Mechanics Rewrite the following loop as a “while” loop: for x in [10, 20, 30, 40]: print (x)
16
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
17
The range( ) Function for x in range(5): >> iteration # 0 print(“iteration #”, x) iteration # 1 iteration # 2 iteration # 3 iteration # 4
18
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
19
The range( ) Function range(5) [ 0, 1, 2, 3, 4] range (10) [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
20
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]
21
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
22
The range( ) Function Lastly, you can ask the range( ) function to count backwards by passing in a negative step count
23
Practice – Countdown Write a program that counts down from 10 and then print out “HAPPY NEW YEAR!”
24
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
25
Import ‘’time’’ import time for x in range(10, 0, -1): print(x) time.sleep(1) print(“HAPPY NEW YEAR!”)
26
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
27
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
28
Practice – Stair Master Write a program that prints out the following: ** (2 stars) **** (4 stars) ****** (6 stars) ******** (8 stars) ********** (10 stars) ************ (12 stars)
29
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
30
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
31
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
32
User Controlled Ranges x = int(input(“start value: “)) y = int(input(“end value: “)) z = int(input(“step value: “)) range(x, y, z)
33
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
34
User Controlled Ranges range(int(input(“start: “)), int(input(“end: “)), int(input(“step: “))) Just be careful, as this can be confusing and you need to keep count of how many parentheses you use
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.