Python Flashcards
What it is the syntax for a print statement when using a variable
print(variable name)
What it is the syntax for a print statement when using a string
print(“string”)
what is the command for the user to input data
input()
what is the command to allow the user to input a hole number value
int(input())
What is the command for allowing the user to enter a decimal value
float(input())
what are the two types of loop
While loop
For loop
What is the syntax for a while loop
while counter
What is the syntax for a for loop
For counter in range(0,5):
print(counter)
Define Pseudocode
Pseudocode is an informal high-level description of the operating principle of a computer program or other algorithm. It uses the structural conventions of a programming language, but is intended for human reading rather than machine reading.
How is Pseudocode started and ended in a python shell
’’’
’’’
What is the command for calling a function
functionname()
What is string slicing and what is it used for
String slicing is used for either removing locating items in a string
State the commands used in string slicing
x = y[w:z]
Purpose: To extract characters from the middle of a string.
x = y[-z:]
Purpose: To return characters to the right of a string.
x = y[:z]
Purpose: To return characters to the left of a string
Name the nine Logical operators that can be used in if elif and else statements
== equals != does not equal greater than >= greater than or equal to and both conditions must be true or one condition must be true not condition is not true
Name the 7 arithmetic operations
\+ addition - subtraction * multiplication / division ** power of // integer division % modulus (remainder after division)
What is the syntax for creating a list
x = []
What is the command for inserting to a list
listname.append(item)
What is the command for removing an item from a list
listname.Remove(item)
What is the command for sorting a list
listname.sort()
What is the command for reverses the order of the items in a list
listname.reverse
What is the command for removing all items from a list
del listname[:]
What is the command for returning a list if it contains a specific value or item
if x in listname:
Name the two types of variables
Global
Local
Give the syntax for a global variable
global=0
Give the syntax for a local variable
def function: variable_name=variable_info
How to begin a serial file
text_file = open(“filename”, “w”)
Name the command for input the data of a serial file into a variable.
x = textfile.read()
Name the command for Reads a single line of data from an open file into x.
variable = LineInput(1)
Name the command for writing a single item of data to an open file.
Example code: textfile.write(“data to write”)
Name the command that commits any data in the file buffer to storage and closes the file. If a file is not closed, data will not be written.
textfile.close
Define the try keyword
‘Try’ is the keyword to put before a run time error is likely to occur
Define the except keyword
The code following ‘except’ is the code
to execute if an error occurs.
What is the syntax for try
try:
…
What is the syntax for except
except:
…
Give the syntax for a if statement
if …. :
Give the syntax for an else statement
else:
…
Give the syntax for ending an if statement in pseudocode
Endif
Give the syntax for ending a function in pseudocode
Endfunction_name
Name the three types of error in programming
Syntax errors when you mistype a keyword, the program doesn’t recognise it as a valid
command and will not run.
Logic errors where the program runs but you get the wrong result, perhaps not always
consistently! Logic errors are called bugs.
Run-time errors that occur in exceptional circumstances such as division by zero and
incorrect data type input. These should be trapped with exception handling commands
Name the types of validation
Presence checks: did the user actually enter any data?
Range checks: does the input fall within a valid range of numbers?
Format check: does the input match a recognised format, e.g. LLNN NLL for postcode
Length check: is the input the required length of characters, e.g. minimum 8 character
password