Program Design Notes
Program Design Notes
13 %= 158 calculates remaining whole numbers(I think “=” makes it only whole numbers)
-------------------------------------------------------------------------------------------------------------------------
Print”
2.4:Python Syntax:
UNIT 3-ARRAYS
1-D arrays:
When keeping position holders (Term 4), write code where code is written for marks)
Highest = arr[x]
HighestTerm = x
2-D arrays:
when deleting from a 2-D array, be very specific(del myArray[0][3])…row 0, column 3).
For 2-D arrays, first say for column then say for row
Print(column, end = " ") the (“end = “ “) turns printing from 1 value per line to all in 1 line
11
12 -> 11 12 13
13
An array is a data structure that consists of related data items (called elements) of the same data
type(string, int). cannot contain a mixture of both. Contains a fixed number of items/elements.
Locating element no.8 is the same time as locating element no.0(starts at 0). Arrays are used to store
data. Arrays need to be declared but lists do not need to be declared(they are part of python
syntax)
index() Returns the index of the first element with the specified value
Variable.METHOD(“Data name”)
An array index tells us the position or address of a particular element in the array. The array
index is enclosed in square brackets…. Marks [0] = 60…..marks[1]=85
2-DIMENSIONAL ARRAYS
For row in array
For column in row
vxb
23 27 11
12
6 10 14 5
25 10 17
18
8 9 15 12
USING METHODS AND 2-D ARRAYS
Arrayname.method(position/function of method[values to be used])
Write rest of code normally
2 arrays =
For row in array1
For clomun in row
Updating
myArray[2] = [15,14,9,2]…#wiill reassign the values on array index 2
f.open(“poes.txt”, “w”)
f.write(data + “\n”) – write variable(data) to the new file. “\n” indicates the end of a lin
f.write(poespoespoes)
f.close
f = open(“poes.txt”, “r”)
print(f.read())…read() method prints the whole text
print(f.read(12))…12 specifies how many characters to return
print(f.readline())…returns 1 line using readline() method
for line in f:
print(line)…by looping through lines of the file, you can read the whole file, line by line
f.close…always fucken close the fuke when done using it
bra please read file, close file then open file using append, write or create to then edit it
+ “\n” … indicates the end of a line
Sorting algorithms
Bubble sort is a sorting algorithm that works repeatedly stepping through lists that need to be
sorted, comparing each pair of adjacent items and swapping them if they are in the wrong
The steps to follow when sorting are:
Step 1: declare array (“arr = [64, 34, 25, 12, 22]”)
Step 2 : count how many values are in array (“n = len(arr”)
Step 3: first for loop for values before n (“for i in range(n):”)
Step 4: loop again for previous n values: (“for x in range(n-1)”)
Step 5: see if x is greater than [x+1](“if arr[x] > arr[x+1]”)
Step 6: rearrange [x], [x+1] to [x+1], [x] (“arr[x], arr[x+1] = arr[x+1], arr[x]”)
SEARCHING ALGORITHMS
Linear searching:(0(n))
1. Start from the leftmost element of arr[] and one by one compare x with each element of arr[].