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