Data type
indicates the type of data that can be stored in a field
Typical memory usage: integer
2 bytes
Typical memory usage: float/real
4 bytes
Typical memory usage: character
1 byte
Typical memory usage: string
1 byte per character
Typical memory usage: boolean
theoretically 1 bit, usually 1 byte in high level languages
Constant
a value that does not change in a program
How are constants written?
in capitals, if space, use underscore
Snake case
the way constants are written
How do we declare constants in Python?
(no way to make unchangeable variable)
[IDENTIFIER] = [value]
How do we declare constants in pseudocode?
CONSTANT [IDENTIFIER] ← [value]
Why declare a constant? [2]
- prevents value from being accidentally changed by code
- shows programmer that value should remain the same
Can a constant change its value? [2]
- not while the program is running
- can be changed before program is compiled or translated
Change a value's datatype in pseudocode [2]:
- STRING_TO_INT(x)
- x ← x as INT
Output length of string (pseudocode, Python)
string = [any string]
- OUTPUT(LEN(string))
- print(len(string))
Output substring (pseudocode, Python)
string = [any string]
- OUTPUT(SUBSTRING([start], [end], string))
- print(string[[start]:[end]:[step]])
Output position of character (pseudocode, Python)
string = [any string]
- OUTPUT(POSITION(string, [character]))
- print(string.rfind([character]))
Concatenate string (pseudocode, Python)
string1 = [any string]
string2 = [any string]
new_string = [combination of string1 and string2]
- new_string ← string + string2
- new_string = string + string2
Concatenate
link together (strings)
Convert character to ASCII function (pseudocode)
CHAR_TO_CODE(a)
Convert to uppercase (Python)
string = [any string]
print(string.upper())
Find datatype of variable (Python)
print(type([identifier]))
Uses of comments [3]:
- describe the purpose of programs
- state program's author
- explain what code does
Common data types [5]
- integer
- float/real
- character
- string
- boolean
(check if it is) equal to
==
(check if it is) not equal to
!=
less than
<
greater than
>
less than or equal to
<=
greater than or equal to
>=
Declare a variable
defining it for the first time
Reference a variable
use the identifier in code to have in calculations or lists
Nested statements
a statement inside another statement
AND
returns True if both values are True
OR
returns True if at least one value is True
NOT
returns True if value is False
Random integer (pseudocode, Python)
- RANDOM_INT(0, 100)
- import random
random.randint(1, 100)
Definite iteration
a process that repeats a set number of times
Indefinite iteration
a process that repeats until a certain condition is met
Example of definite iteration
for loops
Example of indefinite iteration
while loops
WHILE loops have the condition tested at the top/bottom
top
REPEAT...UNTIL loops have the condition tested at the top/bottom
bottom
Array
data structure that allows you to hold many items of data which is referenced by one identifier
Qualities of array [2]:
- fixed length
- declare length when created
Qualities of list [2]:
- variable length
- can append onto list
Output length of array (pseudocode, Python)
- OUTPUT(LEN(array))
- print(len(array))
2D array
an array of an arrays arranged in a grid format, accessed using two index values
Reference an item in a 2D array
array[i][j]
Record
data structure consisting of a number of fields which can be of different types