In [4]:
name = "Computational Thinking"
code = "CM6111"
credits = 20
print(credits)
In [5]:
from nose.tools import assert_equal
assert isinstance(name, str)
assert isinstance(code, str)
assert isinstance(credits, int)
assert_equal(credits, 20)
assert_equal(code, "CM6111")
assert_equal(name, "Computational Thinking")
Rewrite the above code to Multi-Variable assignment
In [ ]:
In [ ]:
assert_equal(credits, 20)
assert_equal(code, "CM6111")
assert_equal(name, "Computational Thinking")
Look at the code below and fix the string in order to ensure it can be printed.
In [7]:
my_string = "'Who's been eating my porridge?' said Papa Bear";
print(my_string)
In [12]:
assert_equal(my_string, "Who\'s been eating my porridge? said Papa Bear")
Using operators, change the string below to print it 400 times.
In [16]:
to_print = "Me, but 400 more times"
print(to_print) #do not change or repeat this line
In [17]:
assert_equal(to_print, "Me, but 400 more times"*400)
Use the variables below to assign the length of the string and convert the average word length to a string and assign the concatenated string of both variables to the final var
In [21]:
string_sample = "Hello my name is Gary and I love to write sentences with no punctuation and this makes it really deifficult to read and follow"
length_sample = #calc length here
string_avg = length_sample / len(string_sample.split(" "))
print(string_avg)
to_print = #print average word length AND string_sample here, like this <Average><String> (NO SPACES!)
print(to_print)
In [ ]:
assert_equal(string_avg, 5.25)
assert_equal(to_print, "5.25Hello my name is Gary and I love to write sentences with no punctuation and this makes it really deifficult to read and follow")
Why is the code below valid?
From your GCSE Maths knowledge, why are the 2 answers different?
What is the output of int(false)
?
In [4]:
print(True + 3 * 100)
print((True + 3) * 100)
It is valid because the boolean type can be cast to an int
value, allowing one to perform operations across types.
The brackets make up BODMAS, meaning that the order of operations changes based on the brackets. The first is: 3100 (300) + 1. The second is 1+3 (4) 100.
The output is 0