0% found this document useful (0 votes)
31 views

Computing Coursework: 1 Variable Assignment and Algebraic Manipula-Tion

This document summarizes computing coursework covering various Python programming concepts including: 1) Variable assignment and algebraic manipulation 2) Single conditional statements 3) Repeated conditional statements 4) Functions and lists 5) Iteration 6) Recursion 7) Object oriented programming Each section includes code examples and explanations of the concepts and expected output.

Uploaded by

Nnc 29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Computing Coursework: 1 Variable Assignment and Algebraic Manipula-Tion

This document summarizes computing coursework covering various Python programming concepts including: 1) Variable assignment and algebraic manipulation 2) Single conditional statements 3) Repeated conditional statements 4) Functions and lists 5) Iteration 6) Recursion 7) Object oriented programming Each section includes code examples and explanations of the concepts and expected output.

Uploaded by

Nnc 29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computing Coursework

Nirav Chikhal
April 2020

1 Variable assignment and algebraic manipula-


tion
import math

"""
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
"""

2 Single conditional statements


import random

"""
The import random tag allows the use of code from the random library
"""

age = random.randint (0, 100)

1
"""
This assigns the word age with a random number between 0 and 100
"""

if age >= 18:


print ("access allowed")

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'
"""

3 Repeated conditional statements


Amount = 1000
numOfYears = 0;
Target_amount = 2000
intrest_multiplier = 1.05

"""
The initial amount (number of years = 0) is assigned as 1000, the target amount is assinged
is assigned 1.05
"""

while Amount < Target_amount:


Amount = Amount * intrest_multiplier
numOfYears = numOfYears + 1
print('After', numOfYears, 'years, the Amount = £', Amount)

"""
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
"""

4 Functions and lists


def square(n):
"""
The square function will return the nth square number
"""

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
"""

7 Object oriented programming


class Car:

"""
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))

print("{} is the colour {} ".format( A.model, A.colour))


print("{} is the colour {} ".format( B.model, B.colour))

"""
The output will state the class attributes(fast cars) and the instance atributes(model and c

"""

You might also like