Why do we use loops?
There may be instances in programming where you may have to write the same statements over and over again.
This process can become pretty tedious, therefore rather than write it out several times or even several thousand times, we would like to be able to write our statements once and then just repeat them. For example, we should be able to specify to repeat 5 times or repeat 5,000 times, or keep repeating until told to stop.,
It is code used to repeat a sequence of commands
What is the problem with creating while loops?
Creating loops is actually easy. The main issue with any loop is not when to loop, it’s when to stop. If we need to loop 5,000 times, who’s keeping track? If we need to loop until told to stop, where’s the code to figure out how to stop? To stop a loop, we write code that must contain conditions that control how long they loop.
What is a while loop?
a while loop is a control flow statement that allows code to be executed repeatedly based on a Boolean condition. The while loop can be thought of as a repeating if statement
while is a keyword
they will repeat as long as the conditional statemnts within them are True, it replaces the counting portion of a for loop with a conditional statement
What is an iteration?
a += 1
This statement increments “a” by 1 after every iteration. An iteration is one cycle through the block of code. The variable “a” must increment or we will have an infinite loop. Adding counter += 1 makes sure it isn’t an infinite loop as it would always be less than the number and increases counter
one cycle of a loop
What is a = 1?
a = 1 is a control variable: controls the amount of times loop repeats
What does it mean to trace code?
Trace code: means going through each line at a time
What does a <= 10 do in a while loop?
a <= 10 checks if a is less than or equal to 10 and then runs the code and it keeps checking the value of a until the condition is false, the loop will stop will continue with code after the loop
What is the counter for loop and the index of the loop?
for loop is a loop that will iterate a specific number of times. The i in the first line is an integer variable that is not declared. It is called the index of the loop. The index can be any variable name, it does not have be “i”.
i is the control varaible
used to repeat a specific block of code a known number of times
When do you use a while loop compared to a for loop?
If you dont know how many times you need to iterate, you use a while loop
But if you know how many times you need to iterate, you a for loop
What do you include in the paratheses for the for loop?
In parentheses it accepts two arguments, first is where it starts and second where it ends but it does not include that number (1, 11) → only 1-10
If you want to count by 2s in a for loop, how do you do it?
(0, 22, 2) ← range from 0 to 20 and count by 2 (22 because it has to be 2 more than where you want it to end)
What is the len function?
phraseOne = “This is a simple phrase.”;
length = len(phraseOne)
print(“‘This is a simple phrase’ is”, length, “characters long.”)
The len function returns the number of characters in a string including spaces
How do you convert strings to uppercase?
phraseTwo = “hello world!”
print(phraseTwo.upper())
The upper() function changes the text to uppercase.
How do you convert strings to lowercase?
str1 = “hello”;
str2 = “Hello”;
if str1.lower() == str2.lower():
print(“Yes, they are equal.”)
How do you find charcaters in strings?
phraseThree = “Strings are cool.”
position = phraseThree[2]
print(position)
How do you split string?
phraseFour = “Python is cool”
print(phraseFour[0:6])
print(phraseFour[7:9])
print(phraseFour[10:14])
String indexing allows you to access individual characters in a string using their position (index), while slicing lets you extract a range of characters. Indexing uses square brackets with a single number text[0] , while slicing uses a range text[start:end] or text[start:end:step] .
What is a counter vraiable for the for loop?
Counter is variable you used to count only certain numbers iterations to be done only. In this example, counter incremented every time one.
i
What is the range function in for loop?
It will count from the number in the brackets up to, but not including, the number listed in the functions parentheses
It defines where a counter starts, ends and what number it should count by
range(start, stop, step) <– the start and stop parameters name the start and stop numbers for the counter while the step parameter tells how much the counter should count by
What gets created every time you create a for loop?
You also create a new varaible that counts the number of time steh loop is repeated. that means the vraible can be used withinn the repeated code.
For example, i was used and printing teh value i before a string shows how the values increases each time the loop is run