2.2 Programming Flashcards
Variable
Name/ label used to identify a memory location used to store a value that can change while the program is running
e.g. age = 12
Constant
Name/ label used to identify a memory location used to store a value that cannot change while the program is running
e.g. const PI = 3.14
Basic programming constructs
-sequence
-selection
-iteration
Sequence
Execution of statements one after the other in order
Selection
Construct used to make decisions in a program based on the result of a boolean condition
e.g. if else
-case =
Iteration
Construct used to repeat sections of code - looping
-count controlled iteration: repeats code a defined number of times - for loop
-condition controlled iteration - checks condition each time around the loop and decides whether to repeat the code again or continue the rest of the program - while/ do…until
Arithmetic operators
Used to carry out basic mathematical operations on numerical values
Arithmetic operators - addition, subtraction, multiplication, division, modulus, floor division, exponent (OCR/ Python)
+ +
- -
* *
/ /
MOD %
DIV //
^ **
Comparison operators
Used to evaluate expressions to a boolean True or False condition
Comparison operators - equal to, not equal to, less than, less than or equal to, greater than, greater than or equal to
==
!=
<
<=
>
>=
Boolean operators
Allow multiple conditions to be evaluated
AND - both conditions to be true
OR - one or the other or both to be true
NOT - reverses true or false outcome from a condition
Data types
-integers - whole numbers, + or -
-real (float) numbers - decimals
-boolean variables - true/ false
-character - single item from a character set
-string - collection of characters (word/ sentence)
Casting
Conversion of one data type to another
str()
int()
float()
bool()
String manipulation
Allows string to be sliced, concatenated, modified
Slicing - extract individual characters from a string
Concatenation - join strings together
String manipulation code
.upper()
.lower()
x = len()
x = y[z] - position of character to extract from (z)
x = y [:z] - return characters up to z in string
x = y[-z:] - return characters to right of string
x = y[w:z] - extract from middle of string
Basic file handling operations
-files must be open before use and closed once operations are completed
To open and read a file:
file = open(“songlist.txt”,”r”)
Contents can be stored in a variable:
content = file.read()
Print the contents:
print(content)
Close the file:
file.close()
To open and write a file: (override existing data)
file=open(“songlist.txt”,”w”)
To add to a file: (append)
file=open(“songlist.txt”,”a”)
SQL
Structured Query language - language used to access data stored in a database
SELECT - fields to return
FROM - which table data will return from
WHERE - include criteria
SELECT * - return all fields
1D array
Allows programmer to store multiple items of data under a single identifier
-item can be accessed through a single index number
e.g.
colours[2] - returns third item
colours[1] = “purple” - replace second item with “purple”
2D array
Allows programmer to store multiple items of data under a single identifier in a table structure and access each element using two index numbers
-read question: is it [row,column] or [column, row]
e.g.
colours[1][2] - returns item at row 1, column 2
colours[3][1] = purple - replace item at row 3 column 1 with “purple”
Iterate/ loop through arrays
-e.g. 1D array - Calculate total and output result
total = 0
for x in range(0,5):
>total += numbers[x]
print(total)
-e.g. 2D array - reset each colour in array with empty string
for x in range(0,5):
>for y in range(0,3):
»colours[x][y] = “”
Subprograms
Programs split into multiple sections
-makes code easier to read and maintain
-reduces size of code
-reuse code without copy and pasting
Procedures
Subprogram that does not return a value to the main program
e.g.
def add(num1,num2)
>total = num1+num2
>print(total)
Functions
Subprogram that does return a value to the main program
-return keyword must be used when defining a function
e.g. def add(num1,num2):
>total = num1+num2
>return total
-if you call a function, make sure you have a variable ready to store value returned
answer = add(x,y)
Random number generation
number = random.randint(1,100)