Session 2 - Part 2 Flashcards
What is the difference between a ‘for’ loop and a ‘while’ loop?
‘for’ loops iterate over a list of values for a fixed number of times, while ‘while’ loops continue iterating as long as a certain condition is met.
What caution is given regarding ‘while’ loops?
‘while’ loops can lead to infinite loops if not properly controlled, potentially causing the program to become unresponsive.
How can one escape an infinite loop?
To escape an infinite loop, one can use the Ctrl-C keyboard shortcut (holding down the Ctrl button and pressing C simultaneously) or use the stop button available in coding environments like Colab or Spyder.
What is a while loop in Python? - (2)
A while loop is a control flow statement that repeatedly executes a block of code as long as a specified condition is true.
It continues to execute the block until the condition becomes false.
While loops are useful when the
number of iterations is not known in advance.
Explain the purpose of the while loop in the provided code snippet - (5).
The while loop continuously executes the block of code inside it as long as the condition a < 5 remains true.
Inside the loop, a is incremented by 1 each time.
The loop stops when a becomes equal to or greater than 5.
After the loop finishes, “Done” is printed.
Note the += syntax here: this is the same as writing a = a + 1.
What is output of this code?
1
2
3
4
5
Done
What is the crucial aspect to ensure in a while loop? - (2)
The crucial aspect is to ensure that the condition being tested will eventually evaluate to True.
If this condition never becomes False, the loop will continue indefinitely.
2.5.1 Quick Exercise
Create a list containing the words apple, banana, cherry and damson. Write a while loop which iterates over the list and prints out the words one at a time. You will need to have the while loop count over the indices and exit when you get to the end of the list.
A conditional statement is one that allows us to make..
The programme can change what it does - (2)
a decision when the program is running.
next depending on what is happening now.
A conditional statement allow the program to execute different blocks of code depending on whether
a condition is true or false.
The conditional statement is expressed in Python using key word
if
The conditional statement is expressed using the keyword if.
In other words,
if is used to start a conditional statement in Python
In Python, ‘if’ keyword can be extended with
else and elif
‘else’ keyword is used in conjunction with
‘if’
‘else’ provides an
alternative block of code to execute when the condition specified with if is false.
If you mentally expand elif to else if, these statements almost translate to
what they would mean if you read them out in natural language.
If you mentally expand elif to else if, these statements almost translate to what they would mean if you read them out in natural language.
e.g. ‘elif’ means
“else if”
‘elif’ allows you to check- (3)
for multiple conditions sequentially after an initial if statement.
If the condition associated with if is false, it checks the condition associated with elif and executes the corresponding block of code if true.
You can have multiple elif statements.
Write a piece of that that:
variable ‘a’ is storing number 10
Create if statement where if a > 10 it prints:
I’m in the first ‘if’ block
a is greater than 10
Create another if statement that if a is 10 then print
I’m in the second ‘if’ block
a is equal to 10
Create another if statement that if a is less than 10 then prints
I’m in the third ‘if’ block
a is less than 10
What is output of code below if a is 10?
What do we see on line 1?
On line 1, we set a variable a to the value 10.
What do we see on line 3? - (2)
On line 3, we ask the question “is a greater than 10”?
This is represented by the conditional if and the comparison a > 10 which returns a bool of False
Explain this code - (4)
a = 10: This line assigns the value 10 to the variable a.
if a > 10:: This line starts the first if statement. It checks if the value of a is greater than 10. If this condition is true, the code block below it is executed. However, since a is equal to 10, this condition is false, and the code block inside this if statement is skipped.
if a == 10:: This line starts the second if statement. It checks if the value of a is equal to 10. Since a is indeed equal to 10, this condition is true, and the code block below it is executed. The statements inside this if block are printed.
if a < 10:: This line starts the third if statement. It checks if the value of a is less than 10. Since a is not less than 10, this condition is false, and the code block inside this if statement is skipped.
‘if’ statement in Python evaluates - (2)
a condition, which can be any expression that results in a boolean value (True or False).
This condition determines whether the subsequent code block associated with the if statement is executed.
The general form of ‘if’ statement in Python - (2)
if CONDITION.
CONDITION can be anything which evaluates to a bool - i.e. True or False.
The general form of if is: if CONDITION. CONDITION can be anything which evaluates to a bool - i.e. True or False. Here we are - (2)
we are comparing numbers
e.g., ‘if a > 10’: this condition evaluates whether value of ‘a’ is greater than 10
What do we find with line 3 of code?
a is not greater than 10.
Lines 4 and 5 of code below are…
We note that lines 4 and 5 are indented:
As with for loops, blocks of statements which are inside an if need to be
indented
Because a is not greater than 10, we do not run…
we do not run lines 4 and 5; instead, we skip straight down to line 7.
On line 7, we check whether a is equal to 10 (a == 10: try it in a console and you will get False). Because this is
False, we jump to the last block…
Since we skip to last block,
We then continue down to line 11 where we check whether a is less than 10. It is! So we go into the block and
execute the code there.
ften we use if statements to check for a ‘special’ condition. For example, - (2)
e normally want our code to do one thing but there is a special case where it should do some other thing.
Here, the else statement becomes useful:
Example image of else statement used in conjunction with if CONDITION
You do not have to have a
else statement (i.e., with if statement or in any loops like for and while)