Computing Coursework: 1 Variable Assignment and Algebraic Manipula-Tion
Computing Coursework: 1 Variable Assignment and Algebraic Manipula-Tion
Nirav Chikhal
April 2020
"""
The Import math tag allows the use of code from the math library
"""
a = 5
b = 10
c = 4/3
"""
Here we have assigned each letter a designated value
"""
print((b + a)/ c)
"""
the Print function produces an output which states the value corresponding to the equation i
case is 11.25
"""
"""
The import random tag allows the use of code from the random library
"""
1
"""
This assigns the word age with a random number between 0 and 100
"""
else:
print("access denied")
"""
If age is assigned a value greater or equal to 18 then the output will be 'access allowed' o
will be 'access denied'
"""
"""
The initial amount (number of years = 0) is assigned as 1000, the target amount is assinged
is assigned 1.05
"""
"""
The while loop will multiply the amount by the intrest multiplyer and add 1 to the numbe
amount is greater than the the target multiplyer. The output will print the amount after
surpasses the target which is roughly £ 2078 after 15 years
"""
2
return n ** 2
samples = []
for n in range(10):
samples.append(square(n))
print(samples)
"""
the list samples is a list of square numbers from 1-9 so the output will print the list samp
"""
5 Iteration
import math
squared_numbers = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
"""
the import math tag allows the use of code from that math library. squared_numbers has been
square numbers from 0-81
"""
for number in squared_numbers:
result = (math.sqrt(number))
print(result)
"""
for the iterations we use a for loop which will square root every number in the list square_
which will give us an output which we should expect to be the numbers from 1-9
"""
6 Recursion
def pascal(n):
"""
Returns the nth fibonacci number using recursion.
"""
if n == 1:
return [1]
else:
line = [1]
line_above = pascal(n-1)
for i in range(len(line_above)-1):
line.append(line_above[i] + line_above[i+1])
line += [1]
return line
print(pascal(8))
3
"""
this code will output a line from the pascals triangle for the nth line. the code will sum 2
row above until all of the adjacent numbers have been added and one is added to the end of t
return the nth line of the pascals triangle. The expected out put should be the 8th line fro
"""
"""
below is the class atribute
"""
type = 'fast car'
"""
below are some attributes for the instance fast car
"""
def __init__(self, model, colour):
self.model = model
self.colour = colour
"""
Below i have created two create objects A and B
"""
A = Car("Ford", "silver")
B = Car("Audi", "white")
print("A is a {}".format(A.__class__.type))
print("B is also a {}".format(B.__class__.type))
"""
The output will state the class attributes(fast cars) and the instance atributes(model and c
"""