Loops, Operators & Strings Flashcards
What is a “while loop”?
Give an example
Used to repeat a block of code as long as a certain condition is true
while condition:
# code to be executed while the condition is true
This loop will continue as long as the “condition” is TRUE
Ex.
num = 1
while num <= 20:
print(num)
num += 1 (used to increment by 1 in each iteration of the loop)
What is a “for loop”?
Give an example
Used to iterate over a collection of items (e.g. a list, tuple, or string) or other iterable object
To go over the collection, the for loop can be used in conjunction with the “in” operator
for item in collection :
# code to be executed for each item in the collection
In each iteration of the loop, the “item” variable is assigned the next value
Ex.
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
Ex.
animals = [‘dog’, ‘cat’, ‘horse’, ‘hamster’, ‘bat’]
for x in animals:
print(x)
How does the “range()” function in the “for” loops?
Used to generate a sequence of numbers that can be used in a for loop
Can be used to generate a sequence of numbers based on a starting point, ending point, and step size
For the “range()” function..
If we don’t use the start parameter, the default start will be “__”
If we don’t use the step parameter, the default step will be “__”
Default start: 0
Default step: 1
For the “range()” function…
The starting point of the sequence is always “_________”
The ending point of the sequence is always “_________”
Give an example to show this
Starting point = inclusive
Ending point = exclusive
Ex.
for x in range(5):
print(x)
The above code will print:
0 (inclusive)
1
2
3
4 (exclusive)
Print the numbers 1 through 5
Given this example
for num in range (,)
print(num)
What would be the range of numbers?
(1,6)
Have to put 6 because the end is exclusive
Print the odd numbers from 1 to 9
Given this example
for num in range (,,_):
print(num)
What would be the range of numbers?
(1,10,2)
Print the numbers 5 through 1 in reverse order
Given this example
for num in range (,,_):
print(num)
What would be the range of numbers?
(5,0,-1)
Here is a slightly more complex example:
sum = 0
for n in range(1,6):
print(n)
sum = sum + n
print(“The sum = “, sum)
What is the result of this code?
Make a chart…
n. sum.
- 0 + 1 =1
- 1 + 2 =3
- 3 + 3 =6
- 6 + 4 =10
- 10 + 5 =15
1
2
3
4
5
The sum = 15
A “for” loop is used for what?
Used to iterate over a sequence of values
(ex. a list, tuple, or range)
Executes a set number of times based on the length of the sequence
Syntax: for item in sequence
A “while” loops is used for what?
Used to execute a set of statements repeatedly while a condition is true
Executes an unknown number of times based on the condition
Syntax: while condition:
In general, “for” loops are typically used when you “_____” the number of times you need to iterate
In general, “while” loops are used when you “_____ _____” the exact number of times you need to iterate & need to “_______” iterating until a certain condition is met
Know
Don’t know; continue
What are nested control structures?
This allows you to perform more complex tasks & solve more complicated problems
Why is indentation in python so important?
Changing indentation = can change program output
Give an example of a nested “for” loop
for i in range(3):
for j in range(3):
print(i, j)
What would print in this example?
x= 10
y = 5
If x > y:
If x > 15:
print(“x>y and greater than 15”)
else:
print(“x>y but less than or equal to 15”)
This will print whichever condition is true
Because the indentation is correct
What would print in this example?
x= 10
y = 5
If x > y:
If x > 15:
print(“x>y and greater than 15”)
else:
print(“x>y but less than or equal to 15”)
This will not print anything because the else is not properly indented
What will be printed?
(Go through each part)
sum = 0
for i in range(3):
for j in range (9,1,-3)
sum += j
print(“sum =“, sum)
for i in range(3) —> [0,1,2]
for j in range(9,1,-3) —> [9,6,3…]
sum += j —> [9+6+3 = 18]
print(“sum =“, sum) —> [18x3 = 54]
***multiply by 3 at the end because the range is = 3
In the example…
range(9, 1, -3)
What values would this include?
It creates a sequence STARTING at 9
Ending JUST BEFORE 1 (does not include 1)
And MOVING BY -3 in each step
Values for j would be [9, 6, 3]
Give an example of a “while” and a “for” loop that are identical
Example using a for loop
In a “for” loop:
for i in range(5):
print(“iteration”, i)
What would give an equivalent output, but as a “while” loop?
i = 0
while i < 5:
print(“iteration”, i)
i += 1
BOTH give the same output which is [0,1,2,3,4]
PEDMAS rule applies in python
What is this?
P: parentheses ex. (2 + 3) + 4 =20
E: exponents ex. raising to a power (**)
- evaluates from RIGHT —> LEFT
MD: multiplication & division (from left to right)
- (*), (/), and modulus (%) operators
AS: addition & subtraction (from left to right)
Strings are immutable, what does this mean?
They cannot be modified once created
Msg[0] = “h”
How do you break out of an “infinite” loop?
Forgetting to update the loop index is a common error
Need to “interrupt execution” to break out of the loop
Do this by Ctrl+C