Term 2 Python Flashcards

1
Q

Is the next line a valid print statement?

print(“Hello World”)

A

Yes, it prints ‘Hello World’ on screen

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is the next line a valid print statement?

print()

A

Yes, it prints a blank line (useful for spacing information out on screen).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
Are the next 4 lines a valid print statement?
print('''
Hello
world
''')
A

Yes, using triple quotes at the start and the end keeps the formatting (layout) so this prints as:
Hello
world

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is the next line a valid print statement?

print(Hello World)

A

No - it is missing speech marks and so will not print.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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”)

A

No - the value input would be a string and would not work in a calculation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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”))

A

No - value is not understood by Python

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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”))

A

Yes - the value input by the user is “cast” (changed) to a floating point number (number with a decimal).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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”))

A

No - number is not understood by Python

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What data type is used for several characters in a row, e.g. “value34” or “End of Game!”?

A

string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What data type is used for whole numbers, e.g. 78 or -99?

A

integer (int for short)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What data type is used for numbers with a decimal point, e.g. 3.14 or 66.9?

A

floating point number (float for short)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What data type is used to hold values that can only be True or False?

A

boolean (bool for short)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can the command below be used to allow the user to
enter data which can be saved into a variable?
enter

A

No enter is not a command word recognised in Python

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can the command below be used to allow the user to enter data which can be saved into a variable?
input

A

Yes:

variable = input(“please give me some data “)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can the command below be used to allow the user to enter data which can be saved into a variable?
print

A

No this just prints out but does not allow entry of data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can the command below be used to allow the user to enter data which can be saved into a variable?
float

A

No a floating point number is one with a decimal point in it e.g. 3.4

17
Q

Does the statement below test to see if the size is 5?

if size is 5:

A

Yes!

18
Q

Does the statement below test to see if the size is 5?

if size = 5:

A

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

19
Q

Does the statement below test to see if the size is 5?

if size == 5:

A

Yes!

20
Q

Does the statement below test to see if the size is 5?

if size != 5:

A

No, =! is the boolean operator for “is not equal” so this tests whether size is not 5

21
Q

What happens when you run the following code?

print(“\tHello World”)

A

it will print Hello World after a tab (usually 4 spaces) like this:
Hello World

22
Q

Which of these are ways of repeating sections of code?

a) during loop
b) while loop
c) repeat loop
d) for loop

A

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

23
Q

What are the mathematical operators for Python?

A
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)
24
Q

How do we make our code take decisions in Python?

A

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”)

25
Q

What will the print when this Python code is run?
if True:
print(“This one is correct”)

A

This one is correct (the if statement is True)

26
Q

What will the print when this Python code is run?
value = 5
if value == 5:
print(“This one is correct”)

A

This one is correct (the if statement checks whether value is 5 and it is so this is true)

27
Q

What will the print when this Python code is run?
value = 5
if value = 5:
print(“This one is correct”)

A

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

28
Q

What will the print when this Python code is run?
value = 5
if value is 5:
print(“This one is correct”)

A

This one is correct

29
Q

What happens when the following lines of code are run?
for number in range(2, 7):
print(number)

A
The following will print on screen:
2
3
4
5
6
30
Q

How is a comment written in Python?

A
using a 'hash symbol' at the front e.g.
# this is a comment
31
Q

Why are comments used in programming languages like Python?

A

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.

32
Q

What prints out when the following code is run?

print(“This is a string”, “and this is another string”)

A

This is a string and this is another string (there is a space between string & and)

33
Q

What prints out when the following code is run?

print(“This is a string” + “and this is another string”)

A

This is a stringand this is another string (no space is put between string & and)