A construct which allows us to store data with an associated identifier
Select the rules you must follow when creating a variable:
It must not start with a number It must not contain a space It cannot contain any symbols other than “_”
Select the valid variable identifiers:
_Message MESSAGE message
Select the invalid variable identifiers:
first name 1stname 1st name
In Python, which of the following best describes a constant?
A value that is not supposed to change throughout the program
Which convention is commonly used in Python to indicate a variable should be treated as a constant?
Naming the variable in all uppercase letters
What Python construct allows you to execute code based on certain conditions?
If statements
What will happen if an 'if' statement's condition is True?
The indented block of code underneath will execute
How do you start an 'else if' block in Python?
elif
What does the '>' operator do in Python?
Checks if the left value is greater than the right value
In Python, what does the '<=' operator mean?
Less than or equal to
What is the output of the expression '7 < 8' in Python?
TRUE
When is the code under an 'else' statement executed?
When the preceding 'if' statement condition is False
Which statement is true about 'else' in Python?
It provides an alternative block of code that runs if the 'if' condition fails
What must follow an 'if' keyword in Python?
A condition
What is one of the benefits of using selection in programming?
Allows the program to make decisions
What might you use to compare a variable to multiple different values in Python?
Multiple 'elif' blocks
In Python, how can you check if a number stored in a variable 'num' is not 10?
if num != 10:
What does '!=' mean in an if statement?
It checks if two values are not equal
Which term describes repetition of a section of code?
Iteration
What is meant by a count-controlled loop?
A loop that repeats a section of code a set number of times
What is meant by the range in a for loop?
The set of values that the loop variable takes
Which term describes a value that tells a for loop how much to increase or decrease by each time?
Step
Which type of loop is designed to run a set number of times?
A count controlled loop such as a for loop
How many times does the loop body run in: for i in range(1, 8)?
7 times
Which numbers does this loop print: for i in range(1, 10, 2)?
1, 3, 5, 7, 9
Which numbers does this loop print: for i in range(10, 1, -1)?
10, 9, 8, 7, 6, 5, 4, 3, 2
What does a for loop do when it is used with a list?
It repeats once for each item in the list
Which loop correctly prints each score in the array scores?
for s in scores: print(s)
When would a for loop be a better choice than a while loop?
When the exact number of repetitions is known in advance
What is the purpose of the loop variable i in for i in range(5)?
It takes each value in the range in turn
Which numbers does this loop print: for i in range(4)?
0, 1, 2, 3
What does this program print?
10
Which loop correctly prints every item in the array testResults using the position of each item?
for i in range(len(testResults)): print(testResults[i])
What is meant by the body of a for loop?
The statements that are repeated by the loop
What is tested at the beginning of a while loop?
The condition that decides whether the loop runs
Which type of loop should be used when the number of repetitions is not known in advance?
A condition controlled loop
A program keeps asking for a password while the password entered is not equal to 'rE5Bh9dP'. The user enters the correct password on the first attempt. What happens?
The loop body does not run at all
What is an infinite loop?
A loop whose condition never becomes false
In a while loop, which statement is true about the code the loop repeats?
It must eventually change the value used in the condition
What does a while loop do when its condition is tested and found to be false?
It ends the loop and continues with the code after it
A program uses a while loop to let a user enter numbers until they enter zero. Which statement is true?
The number of iterations depends on user input
A while loop starts with count set to 1 and adds 1 to count each time it repeats. Which condition makes the loop count from 1 up to and including 10?
while count <= 10
What is meant by iteration?
One complete pass through the statements in a loop
What is meant by a condition controlled loop?
A loop that repeats while a condition is true, so the number of repetitions can vary
Which term describes repeating a block of code while a condition remains true?
While loop
What is meant by a variable number of iterations?
The loop repeats a number of times that depends on the condition
A while loop repeats while score < 10. Nothing inside the loop ever changes the value of score. What happens when the program runs?
The loop never ends because the condition stays true
A program must keep asking the user to type a password until the correct password 'jd8UpP2+d' is entered. Which condition repeats the loop while the password entered is wrong?
while password != 'jd8UpP2+d'
A while loop starts with count set to 1 and repeats while count < 4. The body of the loop prints count and then adds 1 to count. How many times does the body run?
Three times
A while loop keeps reading values until its condition becomes false. Why is the condition tested again at the start of every repetition?
Because the value it tests may have changed inside the loop body