Introduction to Python: Unit Exam B
Instructions
- Time allowed
- 25 minutes
- Total marks
- 25
- Calculator
- No calculator is permitted.
- Answer all the questions.
Questions
Tick one box.
- LD Shows output on the screen
print() displays output.
a = 4
b = 6
print(a * b)- L24
4 x 6 = 24.
first = "Sam"
print("Hi " + first + "!")- LHi Sam!
The strings are joined: Hi, then Sam, then !, giving Hi Sam!
print((2 + 3) * 4)- L20
Brackets first: 2 + 3 = 5, then 5 x 4 = 20.
| int() | float() | str() | |
|---|---|---|---|
| Turn "12" into a whole number | |||
| Turn 7 into "7" so it can be joined to some text | |||
| Turn "2.5" into a decimal number |
Tick one box in each row.
- LTurn "12" into a whole number: int()
- LTurn 7 into "7" so it can be joined to some text: str()
- LTurn "2.5" into a decimal number: float()
int() gives a whole number, float() a decimal number and str() a string.
Tick one box.
- LA print("Score: " + 5)
A string and a number cannot be joined with +. The number has to be turned into a string first with str().
score = int(input("Score: "))
if score >= 50:
print("Pass")
else:
print("Try again")| Input | Output |
|---|---|
| 75 | |
| 50 | |
| 49 |
- LPass
- LPass
- LTry again
>= means greater than or equal to, so 50 passes and 49 does not.
Tick one box.
- LC age >= 13
13 is greater than or equal to 13.
Tick one box.
- LB A mistake in the rules of the language, so the program cannot run
A syntax error breaks the rules of Python, such as a missing bracket, so the program will not run at all.
a = input("First number: ")
b = input("Second number: ")
print(a + b)- Lint
input() gives strings, and + joins strings, so 5 and 7 make 57. int() turns each into a number so + adds them.
# Write your program below
- LAsks for two inputs
- LConverts the inputs to integers with int()
- LPrints the correct total, for example 12 for 5 and 7
Convert each input with int() before adding, otherwise + joins the two strings.
a = int(input("First number: "))
b = int(input("Second number: "))
print(a + b)Tick one box.
- LA 7.0
7.0 has a decimal point, so it is a float.
- LIndentation shows which lines belong to the if (the block)
- LIndented lines only run when the condition is true, so wrong indentation gives an error or the wrong result
Python uses indentation to show which lines belong to the if. The indented lines only run when the condition is true, so moving a line in or out changes what the program does, or causes an IndentationError.
mark = int(input())
if mark >= 70:
print("A")
elif mark >= 50:
print("B")
else:
print("C")| Input | Output |
|---|---|
| 85 | |
| 70 | |
| 69 | |
| 12 |
- L85 and 70: A, A
- L69 and 12: B, C
70 or more prints A, so both 85 and 70 print A. 69 is at least 50, so B. 12 is C.
x = "3"
y = 4
print(int(x) * y)- L12
int("3") is 3, and 3 x 4 = 12.
Tick one box.
- LB high_score
A name cannot start with a digit, cannot contain a space and cannot be a keyword such as class.
print("Total: " + str(3 + 4))- LTotal: 7
3 + 4 is 7, str() turns it into "7", and it is joined to "Total: ".