Please enable JavaScript.
Coggle requires JavaScript to display documents.
Programming (Data Types (Numbers
Integers - Whole Numbers only (1,2,3)
…
Programming
Data Types
Numbers
Integers - Whole Numbers only (1,2,3)
Real/float - Numbers that cab be decimals (0.23)
-
Character
Single letter, number, symbol (A,B,C)
String
Used for text, can include character ("Hello World!")
-
Data Storage
Terms of Storage
Array - Data set stored together of same type
Assignment - Setting/resetting value to data location
(e.g. would be a variable)
Constant - Named storage location, contains value
that doesn't change during program execution
Record - Data structure, stores related data of diff. types
Variables
Data store given a name (total = 0, total is identifier)
Data can be changed; data stored thru assignment (=)
Choose name carefully - meaningful, recognisable
No spaces in variable names - computer can't read properly
Variable Types
Local - Declared w/in function or procedure, can only
be used in said function/procedure - not recognised outside
Global - Variable visible thru-out whole program
Constants
Data store given a name; won't be changed in
program execution; we can set/store value in constant,
can change constant values in all uses all at once
Stored like a variable
Hardcoding
Value needed use repeatedly in program: possibly
value is hard-coded into program w/ each use
= bad practice (lose marks in exams)
= To change value would mean finding all instances again
Data Exchange
Inputs and Outputs
When we data to be input into program
we create input for user
When we want to output data to user
we create data output
Array
Data set of same data type
e.g. names list (string); each item = element
Each element accessible by reference to array position
-
1D vs. 2D
1 - only has one row, a list of data, no columns
2 - a data table is stored, two part reference ([0][1])
First is column, second is row
Records
Array elements = same data type (age = integers etc.)
Record stores diff. data types & info
Once defined/initialised, can be used many times for
multiple people
Errors
-
Logic
Error in program produces unexpected results
Program will still run (if a > 17): a = "Underage")
Translator
Translates program written in hi level to machine code
Line by line (understood by computer)
Syntax - translator doesn't understand text
Runtime
Error occurs during program execution
Program will run, then stop (e.g. divide by 0)
Can input wrong data type, error can be caused
by user rather than programmer
Validation
Automatic checking, occurs during
program execution (while not name.isalpha:)
Subprograms
Uses
For when an instruction sequence needs to be used
again, but not in a loop - thru-out coding
Create subprogram, refer to it when needed
Reduce code size, incr. reliability; more efficient, neater
To pass data into program - parameter (variable type)
Argument (value in parameter, passed into subprogram
-
Functions
Named instruction set that perform task, must return value
Similar to procedure, except it needs a value returned
Library Code
Most programming languages have built-in code
L.code shorten program length, incr. efficiency
L.code has been pretested; 65 in Python
Java, Python allow L.code importing for graphics
GUIs etc.
Programming
Constructs
Sequence
Order for instructions to be executed
Wrong order = wrong results; In designing
consider order of instructions to be executed
Selection
Programs sometimes need to have diff. paths
Path taken depends on questions & condition
Condition - algorithm feature that can be met
causing diff. actions to be taken
if-then-else statements - take paths based on
conditions (if x, do y, else do z) elif w/ multi conditions
(or switch-case statements)
Iteration
Instructions may need to be repeated
Can have set no. of repetitions (counting loop)
Can repeat until condition met (condition loop)
Loop - Instruction sequence repeated set
no. of times or until condition is met
for-to-next loop - counting loop
(if we know how many repeats)
while-endwhile loop - condition loop
(if we don't know how many repeats)
Operators
Arithmetic Operators
/ - gives float/real in division
MOD - gives remainder from division
DIV gives only integer in division
DIVMOD = DIV + MOD returned
Relational Operators
==, is equal to (= is for assignment only)
<> or !=, not equal to
<, less than; >, more than
<=, less than/equal to >=, vice versa
Logical Operators
AND - requires both arguments to be True
OR - requires both arguments to be True
NOT - inverts Boolean (True = False, vice versa)
String
Manipulation
Before
String - list of characters; letters, numbers, symbols
Written usually inside " " / ' ' ; can be joined into new strings
Concatenation - Joining together character in string (+)
Normally spaces needed (" ") between concatenations
String functions
.lenth or len(), give number of characters in string
.upper(), changes all characters to upper case
.lower() does vice versa; [n], picks spec. character
.substring() selects start of new string w/in string
(e.g. x = "Hello World" x.substring(2,2) = "ll"
[n:n] python vers. of substring x[2:5] = "llo"
str() Converts any data into string type
Read/Write
File Data
Meaning
Take data from variable, array in external file
Most common types - .txt & .csv
(comma separated variables)
Putting data in = writing data
Main commands
Open = to access external file
aFile = open ("file.txt", "r+") as
Close = after we've finished reading
aFile.close()
Read = Using data in program
.read() or .readline() or .readlines()[3]
Write = Putting data in file
.write() or .insert(place,new data) or .pop()