TB1 Week 2


print("Hi","there,","how are you?")

Will print “Hi, there, how are you?” because all o-f the words are strings

Python : An Introduction to Programming by James R. Parker. Is a good idea of a book to go through

Commenting is really important and is done using a # before plain text, not only does it let you put in information about your code for you or someone else to use later, it also lets you disable code temporarily to try and bugfix. 

E.g. 

print ("I could have code like this.")

# and the comment after is ignored

Making code understandable and readable to you and others is very important and can be done with both clear defined variables and comments. You cannot do multiline comments using the # method, however you can wrap comments in triple quotes.

E.g.

“””

This comment

Can run

Off several lines

“””

When writing comments, try to avoid repeating myself, or repeating things that the code clearly explains, as it is a waste of time and unnecessary. Additionally, try to not use comments to cover up poorly written and organised code, also don’t write swearwords or anything else that’s rude.

Maths info

 + plus (does addition)

- minus (does subtraction)

/ slash (does division)

* asterisk (does multiplication)

% percent (is a modulo operator and gives the remainder e.g. 34 % 10 == 4 since 34 divided by 10 is three, with a remainder of four.)

< less-than (gives you a true or false as to whether or not a value is less than another)

> greater-than (gives you a true or false as to whether or not a value is greater than another)

<= less-than-or-equal (gives you a true or false as to whether or not a value is less than or equal to another)

>= greater-than-or-equal (gives you a true or false as to whether or not a value is greater than or equal to another)

E.g.

# + plus does addition

print("addition of 1+2 =", 1+2)

# - minus does subtraction

print("subtraction of 2 from 4 =", 4-2)

# / slash does division

print("division of 10 into 2 =", 10/2)

# * asterisk (does multiplication)

print("multiplying 3 by 2 =", 3*2)

# % percent (is a modulo operator and gives the remainder e.g. 34 % 10 == 4 since 34 divided by 10 is three, with a remainder of four.)

print("using % gives the remainder of two values, for example 34 % 10 =", 34%10)

print("this is because 34 divided by 10 is three with a remainder of four")

# < less-than (gives you a true or false as to whether or not a value is less than another)

print("is 5 less than 2?", 5<2)

# > greater-than (gives you a true or false as to whether or not a value is greater than another)

print("is 5 greater than 2?", 5>2)

# <= less-than-or-equal (gives you a true or false as to whether or not a value is less than or equal to another)

print("is 5 less than or equal to 2?", 5<=2)

# >= greater-than-or-equal (gives you a true or false as to whether or not a value is greater than or equal to another)

print("is 5 greater than or equal to 2?", 5>=2)

Code can be used to calculate math conversions

#convert celsius to fahrenheit

Celsius =(25)

Fahrenheit = (Celsius*1.8+32)

print(Fahrenheit)

Variables are a very important part of coding in python, as it is the definition of different values, an example of code using a varied amount of variables in a clear and concise manner.

# this code calculates what is the average number of passengers

# per car

# by dividing the overall number of passengers with the

# overall number of cars driven

 

cars = 100

space_in_a_car = 4.0

drivers = 30

passengers = 90

cars_not_driven = cars - drivers

cars_driven = drivers

carpool_capacity = cars_driven * space_in_a_car

average_passengers_per_car = passengers / cars_driven

 

print("There are", cars, "cars available.")

print("There are only", drivers, "drivers available.")

print("There will be", cars_not_driven, "empty cars today.")

print("We can transport", carpool_capacity, "people today.")

print("We have", passengers, "to carpool today.")

print("We need to put about", average_passengers_per_car, "in each car.")

 Your activity is to write Python code to compute each of the following:

a) the sum of the marks for all modules in year 1;

b) 25% of the overall mark achieved in modules two and three;

c) the average overall mark achieved across all four modules in year 1 (hint: add all marks together and divide by 4);

d) the weighted average overall mark achieved across all modules in year 1, assuming that units one, two and three are worth 20% of the final year mark each, while unit four is worth 40% of the final mark.

module1 = 40

module2 = 50

module3 = 70

module4 = 90

print("overall marks across all module are", module1+module2+module3+module4)

print(" 25% of the overall mark achieved in modules two and three is", (module2+module3)/4)

print("the average overall mark achieved across all four modules in year 1 equals",(module1+module2+module3+module4)/4)

print("the weighted average overall mark achieved across all modules in year 1 is", (module1*0.2)+(module2*0.2)+(module3*0.2)+(module4*0.4))

#modules 1, 2, and 3 are worth 20% whilst module 4 is worth 40%

#This means they have weighting factors of 0.2 and 0.4

#multiply grades by weighting factors then add up

In python the = sign has two distinctive meanings based on whether or not it is doubled.

A single = will assign a value to a variable, whilst a double == will compare two variables values.

E.g.

a=10

b=30

c=10

a=10 means that a has the value of ten

a==b is false whilst a==c is true, this is because the value assigned to a and c is the same.