Chapter 1: Programming Concepts Flashcards
Python-oriented
Identifier
A unique name for something (e.g. variable, constant, procedure, etc.) within the program
Variable
An identifier associated with a specific memory location, used to store data. Its value can change while a program runs
Constant
A named value within a program with a fixed value that does not change while the program runs
What are the benefits of declaring a constant? (2)
- When its value changes, only the declaration has to be changed
- It makes your code easier to read, debug and maintain
Variable names in Python can… (2)
- Be of any length
2. Use any upper- or lowercase letters, the underscore and the digits 0—9
Variable names in Python cannot… (2)
- Begin with a digit
2. Contain special characters (e.g. +, =), as they already have reserved uses
Why aren’t variable names beginning with underscores encouraged?
A considerable number of names beginning with underscores are already reserved for use by the system
How would you write “Pascal case” in Pascal case?
PascalCase
How would you write “camel case” in camel case?
camelCase
Data type
The type of data stored in a variable
What does the data type of a variable define? (3)
- The type of data stored in a variable
- How much memory is needed to store it
- The operations that can be performed on it
Integer
Data type for whole numbers
Real OR Float
Data type for fractional numbers
Character
Data type for a single character
String
Data type for text (over 1 character). Sometimes limited to a length of 255 characters
Boolean
A true or false value
How much memory is typically needed to store:
- An integer?
- A float?
- A single character?
- A boolean?
- 2 or 4 bytes
- 4 or 8 bytes
- 1 byte
- 1 byte, although 1 bit would suffice
Assignment
When a variable is given a value
Initialisation
Explicitly setting the starting values of variables
Algorithm
A series of instructions that solves a specific problem
What are the three main parts of program flow control?
- Sequence
- Selection
- Iteration
Sequence
Where instructions are executed one after another in series
Selection
Where the program executes certain instructions based on a condition
The two basic selection constructs are…
- If/else statements
- Case statements*
*Do not exist in Python
Boolean expression
An expression that evaluates to true or false
Logical operators
NOT, AND, OR and XOR. Used in conditions and Boolean expressions.
Relational operators
Operators that compare two values
Iteration
Where a program executes a group of instructions zero or more times based on a condition
What are the three types of iteration loop constructs?
- While
- Repeat/until*
- For
*Do not exist in Python
How many times does each loop construct run?
While loops: zero or more times
Repeat/until loops*: at least once
For loops: a specific number of times
*Do not exist in Python
Condition
A Boolean expression that controls an iteration or selection statement
Structure chart
A diagram showing how a code module breaks down into smaller modules through sequence, selection and iteration
Flowchart
A diagram using commonly defined symbols to express an algorithm
Pseudocode
A way of writing an algorithm using programming constructs, but not in an actual language
Data abstraction
The process of taking away characteristics from a program in order to reduce it to a set of essential characteristics
Procedure
A self-contained section of code that performs a specific task and doesn’t return a value
Function
A self-contained section of code that always returns a value
What are the benefits of using functions? (6)
- Different people can work on different modules at the same time
- Modular structure is easier to follow, making the code easier to read
- Code modules can be reused elsewhere in the program but only need writing once
- Modular code is easier to write and debug, reducing programmer error
- Modules are easier to update and maintain in the future - you only have to change one part
- Modules can have test suites, making them easier to debug
Built-in functions
Pre-written functions which are provided by the programming language to give standard functionality
What are the benefits of using built-in functions? (3)
- They are pre-written, thus saving time
- They have been extensively tested already, which means that they are less likely to contain erroneous code
- They are written by expert users, and are therefore likely to use a more efficient algorithm
Method
A function associated with a particular data type
Parameter
A variable name declared in the function definition. It is passed a value when the function is called
Argument
A value passed to a function when it is called
Return value
The result that is returned from a function
Variable scope
The amount of the program in which a variable is recognised and can be used
Global variable
A variable declared in the main part of the program that can be accessed anywhere in the program
Local variable
A variable declared within a function and which can only be accessed inside said function
What are the rules when it comes to variable scope? (3)
- Always declare variables as locally as possible, as local variables are used over global variables
- Only declare variables globally if they are used in more than one function
- Declare as few variables globally as possible, as they can be hard to keep track of
What is the scope of variables used to control loops in Python?
Local to the function the loop is written in
What are the three main types of errors?
- Syntax
- Logic
- Runtime
Syntax error
An error in the format of program statements
Syntax
A set of rules defining how program statements must be written in order for them to be understood by the translator
What are some examples of common syntax errors? (4)
- Mistyping a keyword
- Missing keywords out from constructs
- Leaving brackets open
- Using the incorrect number of parameters
Logic error
An error in the algorithm logic that means that it does not work as expected, even though it runs
What are some examples of common logic errors? (3)
- Missing brackets out of mathematical calculations
- Loops that run the incorrect number of times
- Initialising variables in the wrong place
Runtime error
An unhandled error raised in runtime, which is outside of the control of your program
What are some examples of common runtime errors? (4)
- The user entering input of the wrong data type
- Trying to open a file that no longer exists
- Trying to read from an empty file
- The computer running out of memory
Error handling
Coding that protects the program from runtime errors by pre-empting possible errors triggered by issues beyond the scope of the program code
What is the syntax used for error handling in Python?
try:
except:
Trace table
A manual way of tracking an algorithm by following the changing values of variables throughout the code
IDE
Integrated Development Environment: a programming environment which provides features such as code editing, debugging help and runtime diagnostics
How can a programmer check to make sure their code is error-free? (3)
- Use trace tables to manually track variable changes
- Set break points using an IDE to step into the code and track variable changes
- Create a test suite to ensure the code works as expected
Text file
A file that is structured in lines and only contains ASCII characters
Comma Separated Value file
A text file with multiple values on each line of text, which are separated by commas
AQA Pseudocode to do with text files:
- Reading from a file
- Writing to a file
- Going through a file
- Opening a file
- Closing a file
- text ← READLINE(filename, lineNo)
- WRITELINE(filename, lineNo, text)
3*. WHILE NOT eof(filename) … - OPEN filename to read/write
- CLOSE filename
*eof = End Of File
What are the steps to read from a text file? (4)
- Give the program a file address
- Open the file to read from it (opens the file with a pointer pointing at the first line)
- Read a line/lines of text from the file (the pointer is automatically moved down to the next line)
- Close the file as soon as possible
What are the steps to write to a text file? (4)
- Open the file to write to
- The pointer reads all lines until the End of File so that the file can be added to
- When editing a line, the new line is written over the old one
- Close the file at the end
What kind of error is caused by an unclosed file?
A runtime error
Database
A structured collection of related data
State whether the following can be accessed using a text editor:
- A text file
- A CSV file
- A database
- Yes
- Yes
- No
State whether the following could be accessed using certain spreadsheet or database programs:
- A text file
- A CSV file
- A database
- No
- Yes - each line is a record, and each value between commas a field
- Yes
When would using a text file be a better choice than using a relational database? (4)
- When the data is fairly simple and has to be edited by novice users
- When the data is simple and the features of a relational database would just take up more space
- To avoid compatibility issues - text files are universally readable
- When file size is a concern
API
Application Programming Interface: the interface provided by operating systems to help programmers create applications that run on it (e.g. library programs)
Library program
The systems software provided by the operating system so that application programmers don’t have to rewrite basic functionality
What are the benefits of using library programs? (4)
- They perform standard functionality in a way users expect it to
- Saves time - already written
- They have already been tested extensively
- Can be called anywhere in your program
Open Source program
A program that is created using free software that allows free access to the source code
The Open Source Initiative License states that… (3)
- Software is licensed for use, but there is no charge i.e. it can be used by anyone
- Software must be distributed with the source code so anyone can modify it
- Any derived software must also be distributed under the same license
Pros and Cons of using Open Source software (2 | 2)
Pros
- The software is free to use
- Saves time - the code just has to be adapted accordingly
Cons
- You cannot guarantee that the code works
- They may not be much support for the program you are adapting
Data structure
A temporary structure in main memory which stores related data items together to form a set of data while a program runs
Array
A collection of data items of the same data type, grouped under a single identifier
2-dimensional array
An array made up of 1-dimensional arrays. Used to add rows
What is the syntax used to access nested values within a 2-dimensional array?
array_name[iexp1][iexp2]
Examples of sequence data structures include… (4)
- Arrays
- Strings
- Tuples
- Lists
*#3 onwards is Python-specific
What are the benefits of using sequence data structures? (3)
- Makes your code easier to read, debug and maintain
- Easily processed using a loop, especially a for loop
- Allows more efficient access to the data
Subscript OR Index
The numerical reference to a single data item within a sequence data structure
What is the syntax used to access values in a sequence data structure?
sequence[iexp]
What number does indexing start at in:
a. Pseudocode?
b. Python?
a. 1
b. 0
Python data structures include… (4)
- Lists
- Tuples
- Dictionaries
- Classes
State the kind of brackets used to denote the following Python data types:
- Lists
- Tuples
- Dictionaries
- Square brackets
- Parentheses
- Curly braces
List
A list of values, not necessarily all of the same data type, which is mutable
Tuple
Similar to lists, but are immutable
Why might someone use a tuple instead of a list? (2)
- Tuples are faster
2. They use up less memory space
Dictionary
An index of unique keys, where each key is associated with a value
Class
A user-defined prototype for a data structure, which can include functions
Why might you use a class?
Classes help to enclose functions and their relevant data, aiding data abstraction and readability. This becomes more crucial as a program gets larger
State whether the following are inherently ordered:
- List items
- Tuple items
- Dictionary items
- Class attributes
- Yes
- Yes
- No
- No
Mutable
Describes an object whose value can change
Immutable
Describes an object whose value is unchangeable after being created
State whether the following are mutable or immutable:
- Lists
- Tuples
- Strings
- Numbers (Integers and Floats)
- Dictionaries
- Mutable
- Immutable
- Immutable
- Immutable
- Mutable