Unit 4 Python Loops and Lists
Show how to use "break" and "continue" statements to halt a loop or loop iteration early
If you use the break the code will stop, if you use the continue the code will continue and proceed as normal
When is the logical expression tested in a "while" loop?
When its true
What happens if the "while" loop's logical expression is False when first tested? Will the loop run at all?
Yes, it will only run when false though
How would you write a "for" loop to iterate over all of the values in a list or tuple?
for var in range: print(var)
Know how to use the "for" loop to iterate over the contents of a list, tuple or string or a specific numeric range
if var != "STOP" then the code will continue
What is the result of the "in" logical expression? How do you use it in an "if" statement?
if x is in y: it is there just to tell you what is in the code and if it is then it executes the code
How do you access the individual values of the list or tuple from within the loop body?
index
insert()
Adds an element at the specified position
append()
Adds input to the end of a list
How do you call those three functions with an input parameter?
After you have the input with the variable, you then go down another line and apply what you want to the variable
What is the purpose of the "continue" statement and how does it affect a "for" loop?
It makes the code keep going, and you then have to put more stuff in it.
How do ranges allow you to read a sequence of list elements?
It only shows you what the range is it shows you the little number but stops at the big number so if you have (1, 4) it will print out 1, 2, 3 and not show 4.
How do you use the "in" keyword to form a logical expression with a list?
It shows for what's "in" the code
What is the purpose of the "break" statement and how does it affect a "for" loop?
It stops the rest of the code
What is an infinite loop and why is it a bad thing?
It will crash your computer
Know how to use the "in" keyword to search an input list, tuple or string for a value
It will show you whatever you have to be a Boolean if its true or false.
How do index values work to access specific list elements?
Its in numbers
Understand the need for lists to manage many values at once
Lists are there for if you need many answers for a variable, but don't want a lot of variables, or need to just choose one choice and there be no point in all the other variables.
Does each element need to be the same data type?
No
When using a range, does the output contain the element at the ending index?
No
What kinds of data can you store in lists?
Numbers or words, or even letters.
reverse()
Reverses the order of the list
What is the purpose of the "else" statement and when does the "else" body execute? When would it be skipped?
The else statement is there if the thing isn't true you use it as a backup, and it would be skipped if the for in the loop is true.
Know how to use a final "else" block to execute logic after the loop ends successfully
The final else is there so the code will end and you won't have a infinite loop, because you eventually will get to the else statement
Know how to initialize a list with values
The lists start off with the number 0 and work there way up to whatever number, or how much stuff you have in the list.
What is the expected output from each of the three functions?
The numbers?
What is the biggest difference between a tuple and a list?
The parenthesis
How is a "while" loop different than a "for" loop?
The while loop is there for user input, for loops don't need user input.
Know how to print lists
There are two ways, you can either just print out the lit name, or you can do the list name and the number that you want to print out.
What is the purpose of each of the following list functions? What are the required input parameters?
They are their to make stuff easier
Understand how loops are used in programs
They are used to keep looping, if the conditional isn't right
How do you identify the statements in the body of a "while" loop?
They will be indented, and will only run when its true, if its not then it will just run.
sort()
To arrange data alphabetically, numerically, or chronologically.
Why would you need to store a group of items in a single variable?
To make it to where you don't need to make a tone of variables, also to make your life easier
How do you declare a tuple instead of a list?
Tuples are surrounded by, ( ) while lists are [ ]
How do you ensure that your loops are not infinite?
You add a else statement in you conditional, to make it then stop
Know how to identify and avoid "infinite loops"
You avoid the infinite loops, by making the eventual out statement
Know how to use functions specific to the list and tuple data types to add, remove and modify list elements
You can us the list function that best fits what you want to do to your list
Know how to create at tuple
You create a tuple by adding a comma. It makes it easier to make for lists, and adding lists to your str()
How do you use a "for" loop to iterate over all of the characters in a string?
You print the list
Understand the difference between a list and a tuple and when to use each
You use a list to make multiple things in a line, and you use a tuple to add the list to the string, or output that you want.
What is the purpose of a "for" loop?
a for loop is used to perform a program loop a fixed number of times
clear()
clears the whole list
How do you use a "for" loop to iterate over all of the numeric integers in a range?
count
count()
counts how many items are in the list
pop()
deletes the specific item
Which three Python functions will work on any list or tuple?
index, count, and in
Know how to create a "do-while" (bottom-tested) loop using "while" syntax
it will only do that piece of code while the thing is true
Know how to use len(), min() and max() functions on input lists and tuples
len() shows how many things you have in the list, min() will show you the closest letter to a, and max() will show you the closest letter to z.
index()
lists the stuff in alphabetical order
For any given list size, what is the minimum and maximum valid index value?
minimum is 0, and max is to whatever you have, it keeps going.
Know how to access individual elements and ranges with index values
name.index(number)
How do you print list contents?
print(var)
How do you specify the lower and upper bounds of a numeric range? Is the upper bound inclusive?
range or in
remove()
removes items from list
Demonstrate the syntax for a Python "for" loop
the for is there to help you with your code better, it is also part of the thin that makes the code loop
How do you create a list in Python?
var = ["thing" , "that", "goes", "in", "list"]
Define a "while" loop that will execute as long as a logical expression is true
while True:
Know how to apply "break", "continue" and "else" statements to a "while" loop
you break by putting the break in the for loop, same for the continue and else statements in the for loop