📦 Variables and input
A variable stores a value. input() always returns a string, so use int() or float() to change its type (this is casting). print() shows output.
Python powers websites, science and games. Just a few lines can already make a program that talks back.
A variable stores a value. input() always returns a string, so use int() or float() to change its type (this is casting). print() shows output.
if, elif and else choose what runs. Python uses indentation to mark the blocks. Use == to compare and = to assign.
In Python, input() returns:
To turn "7" into a whole number, use:
Which tests whether x equals 5?
Python marks a block of code with:
x = 4 y = x * 3 + 1
What is the value of y?
Enter an exact answer, without rounding.print(17 % 5)
What does this print?
Enter an exact answer, without rounding.print(2 ** 3) outputs:
print(7 // 2) outputs:
What is the type of 3.5?
Which is a valid variable name?
If mark = 60, the program above prints:
Computers never get bored. Loops repeat work, lists hold data and functions let you reuse your best ideas.
A for loop repeats a set number of times; a while loop repeats while a condition is true. A list stores many values, and its first index is 0.
A function is a named block of code. def defines it, parameters receive values and return sends a result back. A local variable exists only inside its function.
range(3) gives the numbers:
A while loop keeps going:
Which word defines a function?
nums = [4, 7, 9]
What is nums[1]?
Enter an exact answer, without rounding.nums = [4, 7, 9] nums.append(1)
What is len(nums)?
Enter an exact answer, without rounding.total = 0 for i in range(1, 5): total = total + i
What is total after the loop?
Enter an exact answer, without rounding.The first item of a list is at index:
return in a function:
A loop that never ends is called:
def double(x): return x * 2. double(5) gives:
A local variable can be used:
Before coding, plan. After coding, try to break it. That is how reliable software gets made.
Pseudocode sets out an algorithm in structured English, independent of any language. A trace table records each variable’s value at each step.
Validation checks that data is sensible: range, length, type, presence, format and check digit. Verification checks that data was entered correctly: double entry or a visual check. Test with normal, abnormal, extreme and boundary data.
Pseudocode is:
A range check tests whether a value:
Double entry of a password is:
For a valid range of 13 to 19, 40 is:
Total ← 0. FOR Count ← 1 TO 4, Total ← Total + Count. NEXT Count.
What is Total at the end?
Enter an exact answer, without rounding.A valid range is 13 to 19.
What is the largest extreme value that should be accepted?
Enter an exact answer, without rounding.Which check makes sure a field is not left empty?
A check digit is mainly used to detect:
A trace table is used to:
Boundary data tests values:
← in Cambridge-style pseudocode means:
Questions from every topic in this course. Pick an answer for each one, then hit Check My Answers.
A # in Python starts a:
int(input()) is an example of:
scores[2] gets which item?
Which adds an item to the end of a list?
A type check tests whether data:
Validation shows that data is: