Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 77

Information Technology Concepts

Functions and Files

School of Information Technology and Mathematical Sciences


Sometimes we want to perform the one
task many times in different places.
For example, converting measurements.
lengthInMetres = input('Length in metres: ')
lengthInMetres = float(lengthInMetres)
lengthInFeet = lengthInMetres * 3.28084
lengthInFeet = round(lengthInFeet, 2)

widthInMetres = input('Width in metres: ')


widthInMetres = float(widthInMetres)
widthInFeet = widthInMetres * 3.28084
widthInFeet = round(widthInFeet, 2)

areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Converting measurements.
lengthInMetres = input('Length in metres: ')
lengthInMetres = float(lengthInMetres)
lengthInFeet = lengthInMetres * 3.28084
lengthInFeet = round(lengthInFeet, 2)

widthInMetres = input('Width in metres: ')


widthInMetres = float(widthInMetres)
widthInFeet = widthInMetres * 3.28084
widthInFeet = round(widthInFeet, 2)

areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Input.
lengthInMetres = input('Length in metres: ')
lengthInMetres = float(lengthInMetres)
lengthInFeet = lengthInMetres * 3.28084
lengthInFeet = round(lengthInFeet, 2)

widthInMetres = input('Width in metres: ')


widthInMetres = float(widthInMetres)
widthInFeet = widthInMetres * 3.28084
widthInFeet = round(widthInFeet, 2)

areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Convert to numbers
lengthInMetres = input('Length in metres: ')
lengthInMetres = float(lengthInMetres)
lengthInFeet = lengthInMetres * 3.28084
lengthInFeet = round(lengthInFeet, 2)

widthInMetres = input('Width in metres: ')


widthInMetres = float(widthInMetres)
widthInFeet = widthInMetres * 3.28084
widthInFeet = round(widthInFeet, 2)

areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Converting to feet and round off.


lengthInMetres = input('Length in metres: ')
lengthInMetres = float(lengthInMetres)
lengthInFeet = lengthInMetres * 3.28084
lengthInFeet = round(lengthInFeet, 2)

widthInMetres = input('Width in metres: ')


widthInMetres = float(widthInMetres)
widthInFeet = widthInMetres * 3.28084
widthInFeet = round(widthInFeet, 2)

areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Calculate area and round off


lengthInMetres = input('Length in metres: ')
lengthInMetres = float(lengthInMetres)
lengthInFeet = lengthInMetres * 3.28084
lengthInFeet = round(lengthInFeet, 2)

widthInMetres = input('Width in metres: ')


widthInMetres = float(widthInMetres)
widthInFeet = widthInMetres * 3.28084
widthInFeet = round(widthInFeet, 2)

areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Output
Each time we enter the formula, there is a risk of making
an error.
Moving it to a function means we only need to
write the formula once.
def convertToFeet(distance) :
distance = float(distance)
distanceInFeet = distance * 3.28084
distanceInFeet = round(distanceInFeet, 2)
return distanceInFeet

lengthInMetres = input('Length in metres: ')


lengthInFeet = convertToFeet(lengthInMetres)
widthInMetres = input('Width in metres: ')
widthInFeet = convertToFeet(widthInMetres)
areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Converting measurements.
def convertToFeet(distance) :
distance = float(distance)
distanceInFeet = distance * 3.28084
distanceInFeet = round(distanceInFeet, 2)
return distanceInFeet

lengthInMetres = input('Length in metres: ')


lengthInFeet = convertToFeet(lengthInMetres)
widthInMetres = input('Width in metres: ')
widthInFeet = convertToFeet(widthInMetres)
areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Tells Python that we are defining a function.


def convertToFeet(distance) :
distance = float(distance)
distanceInFeet = distance * 3.28084
distanceInFeet = round(distanceInFeet, 2)
return distanceInFeet

lengthInMetres = input('Length in metres: ')


lengthInFeet = convertToFeet(lengthInMetres)
widthInMetres = input('Width in metres: ')
widthInFeet = convertToFeet(widthInMetres)
areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Conduct the conversion


def convertToFeet(distance) :
distance = float(distance)
distanceInFeet = distance * 3.28084
distanceInFeet = round(distanceInFeet, 2)
return distanceInFeet

lengthInMetres = input('Length in metres: ')


lengthInFeet = convertToFeet(lengthInMetres)
widthInMetres = input('Width in metres: ')
widthInFeet = convertToFeet(widthInMetres)
areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Return the final value to whatever asked.


def convertToFeet(distance) :
distance = float(distance)
distanceInFeet = distance * 3.28084
distanceInFeet = round(distanceInFeet, 2)
return distanceInFeet

lengthInMetres = input('Length in metres: ')


lengthInFeet = convertToFeet(lengthInMetres)
widthInMetres = input('Width in metres: ')
widthInFeet = convertToFeet(widthInMetres)
areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Call the new function and save the value returned


This is procedural programming.
The function is a procedure.
In the program, we call the procedure as needed.
Modular programming saves the functions
in a separate file.
This makes them easier to reuse.
def convertToFeet(distance) :
distance = float(distance)
distanceInFeet = distance * 3.28084
distanceInFeet = round(distanceInFeet, 2)
return distanceInFeet

Save in file "Conversion.py"


import Conversion

length = input('Length in metres: ')


lengthInFeet = Conversion.convertToFeet(length)

width = input('Width in metres: ')


widthInFeet = Conversion.convertToFeet(width)

areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

New code.
import Conversion

length = input('Length in metres: ')


lengthInFeet = Conversion.convertToFeet(length)

width = input('Width in metres: ')


widthInFeet = Conversion.convertToFeet(width)

areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Import the module we just made.


import Conversion

length = input('Length in metres: ')


lengthInFeet = Conversion.convertToFeet(length)

width = input('Width in metres: ')


widthInFeet = Conversion.convertToFeet(width)

areaInFeet = round(widthInFeet * lengthInFeet, 2)

print('Length:\t', lengthInFeet, 'feet')


print('Width:\t', widthInFeet, 'feet')
print('Area:\t', areaInFeet, 'square feet')

Call the function in the module.


There's another approach called Object Oriented
Programming.
There's another approach called Object Oriented
Programming.
We'll revisit this later in the course.
File Handling
Read and Write, manipulate data.
Two types: binary and text
Two types: binary and text
Focus today is on text.
Three steps:
Open the file
Write to the file, or read the data.
Close the file
Opening: Three options
Opening: Four options
r: Read file
w: Write file
a: Append file
r+: Read and write file
file = open('test.txt', 'w')

Opens a "test.txt" file to write.


file = open('test.txt', 'w')

If the file does not exist, it creates it first.


file = open('test.txt', 'a')

Opens an existing text file to append text to the end.


file = open('test.txt', 'r')

Opens a file to read in data.


file = open('test.txt', 'r+')

Allows both read and write.


Writing to a file
file = open('test.txt', 'w')
print('Hello')

Works just like print did.


file = open('test.txt', 'w')
file.write('Hello')

Works just like print did.


file = open('test.txt', 'w')
file.write('Hello')
file.write('World')

Well, almost. It doesn't start a new line each time.


file.write('Hello')
file = open('test.txt', 'w')
file.write('Hello')
file.write('World')

HelloWorld

Well, almost. It doesn't start a new line each time.


file.write('Hello')
file = open('test.txt', 'w')
file.write('Hello\n')
file.write('World\n')

Hello
World

Use \n to start a new line.


file.write('Hello')
To print multiple lines at once, use a list and writelines
animals =
['Dog','Tadpole','Axolotl','Turtle','Chicken']
file = open('test.txt', 'w')
file.writelines(animals)

DogTadpoleAxolotlTurtleChicken

Useing writelines
Reading from a file
Method one: Read in the whole file.
file = open('test.txt', 'r')

Open file for read.


file = open('test.txt', 'r')
fileContents = file.read()

Read in the contents of the file


file = open('test.txt', 'r')
fileContents = file.read()
print(fileContents)

Line 1
Line 2
Line 3

Print the contents


Method two: Read in the lines into a list.
file = open('test.txt', 'r')
fileContents = file.readlines()
print(fileContents[1])

Line 2

Read in all the lines into a list


file = open('test.txt', 'r')
fileContents = file.readlines()
print(fileContents[1])

Line 2

Print the one of the lines


Method three: Read lines one at a time.
file = open('test.txt', 'r')
line = file.readline()
print(line)

Line 1

Read the first line ...


file = open('test.txt', 'r')
line = file.readline()
print(line)

Line 1

... and print it.


Using iteration, we can read through all of the lines.
file = open('test.txt', 'r')
for line in file :
print(line)

Line 1

Line 2

Line 3

Iterates through the file and prints each line


Closing a file
Always close the file
file = open('test.txt', 'r')
line = file.readline()
print(line)
file.close()

Line 1

Closing the file


Using with means that you don't need to close the file
with open('test.txt') as file:
for line in file :
print(line)

Line 1

Line 2

Line 3

With opens the file and closes it when done.


CSV files
CSV files open easily in spreadsheets.
Each element is separated with a ","
Test needs quotation marks at the beginning and end.
animals =
['Dog','Tadpole','Axolotl','Turtle','Chicken']
file = open('test.csv', 'w')
for animal in animals:
file.write('"' + animal + '"' + ',')
file.close()

"Dog","Tadpole","Axolotl","Turtle","Chicken"

Writes one row, each element in a new column.

You might also like