Cheatography

# Programming - AQA Computer Science Cheat Sheet by [deleted]

### Structured Progra­mming

 The structured approach to progra­mming means that code is more logical and readable, making it easier to debug and maintain. It also makes it more efficient. The most important part of this approach is modula­rised progra­mming, which is the use of subrou­tines. Other components include detailed code annotation and clearly named variables.

### Data Types

 Data Type Definition Example(s) Integer A whole, positive number. 134 Real or Float Any number, including decimals and negatives. -19.21 Character A letter, number, space, etc. that can be typed, in accordance with ASCII or Unicode. @ String A string of charac­ters. "­Hello, world!­" Boolean Logical values based on binary (1 and 0). True, False
Data Type: A specific type of value (and variable) that must be handled in a certain way.

### Relational Operators

 Symbol Meaning = equal to ≠ not equal to < less than > greater than ≤ less than or equal to ≥ greater than or equal to

### Maths (in Python)

 Operation Example Addition Pseudocode: 11 + 2 = 13 Python example: ``11 + 2 = 13`` Subtra­ction Pseudocode: 11 - 2 = 9 Python example: ``11 - 2 = 9`` Multip­lic­ation Pseudocode: 11 x 2 = 22 Python example: ``11 * 2 = 22`` Real or Float Division Pseudocode: 11 DIV 2 = 5.5 Python example: ``11 / 2 = 5.5`` Integer Division Pseudocode: 11 // 2 = 5 Python example: ``11 // 2 = 5`` Remainder of a Division Pseudocode: 11 MOD 2 = 1 Python example: ``11 % 2 = 1``

### Selection

 Selection is when a program makes a decision, usually taking the form of IF statement. For example (pseud­ocode): ``IF x = TRUE THEN`` `` ­PRINT "­Yes."`` ``ELSE`` `` ­PRINT "­No."­``

### Variables

 What is the 'scope' of a variable? The part of the program where a variable is valid and access­ible. Why is it important to give variables distin­ctive identi­fiers? This makes debugging and mainte­nance easier, as it is easier for the programmer to understand the purpose of the variable.

### Robust & Secure Progra­mming

 There are two main ways to make programs more robust. Firstly, using data validation, which is most easily done using an IF/ELI­F/ELSE statement or a TRY/EXCEPT statement. This makes sure the data inputted by the user is valid, preventing a runtime error. Secondly, authen­tic­ation. This primarily takes the form of passwords. However, testing is also key in ensuring that a program is robust. A programmer must test typical (normal), boundary (extreme), and erroneous data to guarantee that the program deals with data correctly.

### Data Structures

 What are data structures? A data structure is a way of storing data. What is an array? An array is a collection of related data (of the same data type). Each piece of data is an element with a specific index. They may also be called 'lists'. What is a two dimens­ional array? Essent­ially, a 2D array is just an array made of arrays. They can be thought of as a matrix. Possible uses include a bitmap for an image. What is a record? A record is very similar to an array, except multiple data types can be stored together.

### Progra­mming Languages

 High-Level Languages vs Low-Level Languages Most programs are initially written in high-level progra­mming languages (e.g. Python) because these are more like human languages, meaning that they are easier for progra­mmers to unders­tand, code in, and debug. They are said to provide a 'higher level of abstra­ction' from machine code. Low-level langages are very different and much more difficult. Despite this, progra­mming in machine code allows for the optimi­sation of code and avoid code having to be transl­ated. Machine Code vs Assembly Language Both machine code and assembly language are low-level languages, with assembly code having a 1:1 corres­pon­dance with machine code. However, each type of processor has its own specific machine code instru­ction set, which is expressed in binary. Assembly language is generally used for software for embedded systems and for contro­lling specific hardware compon­ents. Transl­ation All programs written in high-level languages or assembly language must be translated into machine code before they can be run. Interp­retors, compilers, and assemblers An assembler translates assembly language to machine code. An interp­retator translates high-level languages into machine code. It does so line-b­y-line, which makes debugging easier. A compiler also translates high-level languages to machine code, but it translates the whle program before running. While the compli­ation process is slow, the machine code can then be stored and run quickly in future.

### Types of Iteration

 What is definite iteration? There is a set number of iterat­ions. Example of definite iteration (Python): ``for i in range(5):   x = x + 1`` What is indefinite iteration? There is not set number of iterat­ions, as it is instead dependent on when a certain condition becomes true. Example of indefinite iteration (Python): ``while x < 5:   x = x + 1`` What is nested iteration? A loop (itera­tion) within a loop. Examples of nested iteration (Python): ``while n == False:   for i in range(5):     x = x + 1``
Note that selection statements can also be nested.

### Subrou­tines

 What is are subrou­tines? Named ‘out of line’ blocks of code that are executed (called) by writing its name in a program statement. Advantages of subrou­tines 1. It makes programs more efficient, because blocks of code can be reused. 2. It means that code is easier to debug, because it is shorter. Parameters A value that is passed to a subrou­tine. These may also be called 'argum­ents'. Procedures vs Functions Both take parame­ters, but functions return a value, whereas procedures don't. Local Variables Subrou­tines can declare local variables, the scope of which is limited to the subrou­tine. Using local variables is good practice because it allows the program to be simpler. Global variables, on the other hand, make a program much more complex, as they may change freque­ntly.

### String Handling

 Pseudocode Python Example Purpose LEN() Find the length of a string. POSITION() print(­str­ing­_to­_se­arc­h.f­ind­("a")) To find the position (similar to an index) of a specific character in a string. If the character appears multiple times, only the first instance will be returned. SUBSTR­ING­(In­tExp, IntExp, StringExp) To create a substring. The first paramter indicates the start of the substring, the second the end, and the final parameter being the string itself. + firstname + lastname To concat­enate (join) two strings together. ORD() ASCIIcode = ord('a') To convert a character to character code. CHR() character = chr(97) To convert character code to character. STR() str(bi­rth­date) To convert a string to an integer. INT() int(ph­one­number) To convert an integer to a string.
Note that the pseudocode is not set. Different people will write their psuedocode differ­ently.

### Input, Output, and File Handling (Python)

 In Python, the input() function allows a program to receive data from the user. Similarly, print() allows the program to output inform­ation to the user. File handling is slightly more complex. A typical file-o­pening statement would take this format: ``file_o­bject = open(“­fil­ename”, “mode”)`` The 'mode' could be 'read' ('r' - allows access only), 'write' ('w' - allows access and editing), or 'appen­ding' ('a' - allows inform­ation to be added to the end of the file. Once the program has finished using a file, the "­fil­ena­me".c­lose() function should be used, as this frees up resources.

### Random Number Generation

 In Python, random number generation requires the import­ation of the RANDOM module. For example: ``import random`` ``x = random.ra­ndi­nt(­0,10)`` ``print(x)``