File Handling - Parallel Arrays Subroutines Standard Algorithms - Parallel Arrays
Writing a String to a line in a plain text file Defining a subprogram
Linear Search
with open�"newfile.txt","w"� as writefile: def addnumbers��:
found = False
writefile.write�"Python is great fun"+ '\n'�
Returning a value from a subprogram choice = input�"Please enter the name: "�
for counter in range�len�names��:
def getInput��: if names�counter� == choice:
Reading all of a plain text file (in one operation) number1 = int�input�"Enter Number"�� found = True
with open�"samplefile.txt"� as readfile: number2 = int�input�"Enter Number"�� position = counter
filecontents = readfile.read�� return number1,number2 if found == False:
print�"No item found in list"�
Defining formal parameters for a subprogram else:
Reading a CSV File into two parallel arrays
def addnumbers�val1,val2�: print�"Item details: ",names�position��
counter = 0 answer = val1+val2 Find Min/Max (value)
names = �""� * 50 return answer
marks = �0� * 50 minimum = marks�0�
with open�"marks.csv"� as readfile: Calling a subprogram (and passing actual parameters) for counter in range�1,len�marks��: < to find the min
line = readfile.readline��.rstrip�'\n'� File contents: addnumbers�myval1,myval2� if marks�counter� < minimum:
while line: > to find the max
minimum = marks�counter�
items = line.split�","� Joe,42
Assigning returned values from a subroutine/function print�"Lowest score is", minimum�
names�counter� = items�0� Anna,90
marks�counter� = items�1� number1,number2 = getInput��
line = readfile.readline��.rstrip�'\n'� Find Min/Max (position)
counter += 1
minpos = 0
Pre Defined Functions for counter in range�1,len�marks��:
if marks�counter� < marks�minpos�:
Creating a CSV File minpos = counter
Modulus Function (Operator in Python)
for counter in range�5�: print�"Lowest score:", marks�minpos�,"by",names�minpos��
name = input�"Enter name: "� remainder = 5 % 2
age = input�"Enter age: "� print�remainder� Finds the remainder
>>> 1
Counting Occurrences
with open�"names.txt","w"� as writefile:
writefile.write�name + ","+ str�age� + '\n'� Convert from Character to ASCII Code occurrences = 0
for counter in range�len�marks��:
print�ord�"a"�� if marks�counter� >=50:
>>>97 occurrences += 1
print�"There were",occurrences,"passes"�
Convert from ASCII code to Character
print�chr�65��
>>>A
Convert floating point numbers to Integers
original = 24.54
newnumber = int�original�
print�newnumber�
>>>24
M Hay @ RGC
String Handling
Records
0 1 2 3 4 5 6 7 8 9 10 11 12
Usual String Indexes Creating an array of records (and populating data)
I a m a s t r i n g Declaring a Record
from dataclasses import dataclass myBMI = �BMI�� for x in range �40��
�13 �12 �11 �10 �9 �8 �7 �6 �5 �4 �3 �2 �1
I a m a s t r i n g
Negative Indexes (if right of string is needed)
�dataclass for x in range �len�myBMI��:
class BMI:
myBMI�x�.height = float�input�"Enter Height: "��
height : float = 0.0
Extracting a Substring Example 1 Checking a single character myBMI�x�.weight = float�input�"Enter Weight: "��
weight : float = 0.0
print�original�2:4�� for x in range�len�original��: BMI : float = 0.0
am if original�x� == " ":
>>> count += 1
print�count�
>>>
Standard Algorithms - Records
Linear Search
Extracting a Substring Example 2 Can also be
print�original�7:len�original��� print�original�7:�� found = False
string string choice = input�"Please enter name: "�
>>> >>> for counter in range�len�pupils��:
if pupils�counter�.name == choice:
found = True
Selecting characters from the end of a string Extracting every second character position = counter
if found == False:
print�original��4�� print�original�::2�� print�"No item found in list"�
r Ia tig else:
>>> >>> print�"Item details: ",pupils�position�.name�
Find Min/Max (value)
File Handling - Records minimum = pupils�0�.score
for x in range�1,len�pupils��:
Reading a CSV File into an array of records if pupils�x�.mark < minimum: < to find the min
minimum = pupils�x�.score
counter = 0 Record Structure Used print�"Lowest mark is: ", minimum� > to find the max
pupils = �pupil�� for x in range �40�� All record examples will
with open�"marks.csv"� as readfile: use this record structure Find Min/Max (position)
line = readfile.readline��.rstrip�'\n'�
while line: �dataclass minpos = 0
items = line.split�","� for x in range�1,len�pupils��:
File contents: class pupil:
pupils�counter�.name = items�0� if pupils�x�.mark < pupils�minpos�.mark :
pupils�counter�.mark = items�1� name : str = “”
minpos = x
line = readfile.readline��.rstrip�'\n'� Joe,42 mark : int = 0 print�"Lowest mark:", pupils�minpos�.mark ,"by", pupils�minpos�.name�
counter += 1 Anna,90
Counting Occurrences
Creating a CSV File using an array of records occurrences = 0
for x in range�len�pupils��:
with open�"names.txt","w"� as writefile: if pupils�x�.mark >= 50:
for x in range�len�pupils��: occurrences += 1
writefile.write�pupils�x�.name + "," + str�pupils�x�.mark� + "\n"� print�"There were",occurrences,"passes"�
M Hay @ RGC