Term 2 Python Flashcards
Is the next line a valid print statement?
print(“Hello World”)
Yes, it prints ‘Hello World’ on screen
Is the next line a valid print statement?
print()
Yes, it prints a blank line (useful for spacing information out on screen).
Are the next 4 lines a valid print statement? print(''' Hello world ''')
Yes, using triple quotes at the start and the end keeps the formatting (layout) so this prints as:
Hello
world
Is the next line a valid print statement?
print(Hello World)
No - it is missing speech marks and so will not print.
Would this code allow the user to enter the radius of a circle which could be used in a calculation?
radius = input(“Please enter a number”)
No - the value input would be a string and would not work in a calculation.
Would this code allow the user to enter the radius of a circle which could be used in a calculation?
radius = value(input(“Please input a number”))
No - value is not understood by Python
Would this code allow the user to enter the radius of a circle which could be used in a calculation?
radius = float(input(“Please input a number”))
Yes - the value input by the user is “cast” (changed) to a floating point number (number with a decimal).
Would this code allow the user to enter the radius of a circle which could be used in a calculation?
radius = number(input(“Please input a number”))
No - number is not understood by Python
What data type is used for several characters in a row, e.g. “value34” or “End of Game!”?
string
What data type is used for whole numbers, e.g. 78 or -99?
integer (int for short)
What data type is used for numbers with a decimal point, e.g. 3.14 or 66.9?
floating point number (float for short)
What data type is used to hold values that can only be True or False?
boolean (bool for short)
Can the command below be used to allow the user to
enter data which can be saved into a variable?
enter
No enter is not a command word recognised in Python
Can the command below be used to allow the user to enter data which can be saved into a variable?
input
Yes:
variable = input(“please give me some data “)
Can the command below be used to allow the user to enter data which can be saved into a variable?
print
No this just prints out but does not allow entry of data
Can the command below be used to allow the user to enter data which can be saved into a variable?
float
No a floating point number is one with a decimal point in it e.g. 3.4
Does the statement below test to see if the size is 5?
if size is 5:
Yes!
Does the statement below test to see if the size is 5?
if size = 5:
No, a single = is used to assign a value to a variable e.g. score = 10 sets the variable ‘score’ to have a value of 10
Does the statement below test to see if the size is 5?
if size == 5:
Yes!
Does the statement below test to see if the size is 5?
if size != 5:
No, =! is the boolean operator for “is not equal” so this tests whether size is not 5
What happens when you run the following code?
print(“\tHello World”)
it will print Hello World after a tab (usually 4 spaces) like this:
Hello World
Which of these are ways of repeating sections of code?
a) during loop
b) while loop
c) repeat loop
d) for loop
while and for are loops e.g.
for counter in range (0, 5):
print(counter) # this prints the values 0, 1, 2, 3, 4
while score == 10:
code_here # runs the code while score is 10
What are the mathematical operators for Python?
In Python the mathematical operator for: addition is + e.g. 2 + 2 = 4 subtraction is - e.g. 6 - 3 = 3 multiplication is * e.g. 2 * 5 = 10 division is / e.g. 6 / 2 = 3 orders is ** e.g. 3 ** 2 = 9 modulus (remainder after division) is % e.g. 10 % 3 = 1 (there is 1 left when we divide 10 by 3)
How do we make our code take decisions in Python?
use the if statement e.g.
if score > 10: # if score is greater than 100
print(“You score is more than 10!”)
elif score > 8: # if score less than 10 & more than 8
print(“Your score is 9”)
else: # if the score is not more than 8 (is 8 or less)
print(“Your score is 8 or less”)
What will the print when this Python code is run?
if True:
print(“This one is correct”)
This one is correct (the if statement is True)
What will the print when this Python code is run?
value = 5
if value == 5:
print(“This one is correct”)
This one is correct (the if statement checks whether value is 5 and it is so this is true)
What will the print when this Python code is run?
value = 5
if value = 5:
print(“This one is correct”)
An error message will print as the line
if value = 5:
is trying to assign the number 5 to value NOT check whether value is 5. This is neither True nor False so the if statement cannot work
What will the print when this Python code is run?
value = 5
if value is 5:
print(“This one is correct”)
This one is correct
What happens when the following lines of code are run?
for number in range(2, 7):
print(number)
The following will print on screen: 2 3 4 5 6
How is a comment written in Python?
using a 'hash symbol' at the front e.g. # this is a comment
Why are comments used in programming languages like Python?
They are lines that the computer does not try to run. They are used to help people (usually the programmer) understand what the code does.
What prints out when the following code is run?
print(“This is a string”, “and this is another string”)
This is a string and this is another string (there is a space between string & and)
What prints out when the following code is run?
print(“This is a string” + “and this is another string”)
This is a stringand this is another string (no space is put between string & and)