Write down what happened in your logbook.
Write the answers to all the following questions and make other observations in your logbook as you use Python to carry out the experiments suggested below. Change the values suggested below so that you are sure that you understand what is happening, and write the results using your chosen values in your logbook.
Question: If 2+2 is exactly 4 why do you think 3.1+5.6 is not exactly 8.7 ?
Question: What advantages might a programmer get by using references called a and b to the number objects 3.1 and 5.6 when carrying out the calculations above ?
Question a: In which of the following order(s) was it possible for the 2 plusses and 1 minus to be evaluated to get 10 ? In which was it not possible to get ten ?
Question: b. What effect did the round () brackets have on the order of evaluation ?
c. How might you use brackets to get the same result you would get without brackets?
Question a: What does the * operator do with the operands 3 and 5 ?
Question b: Which operation was carried out first, multiplication or addition ?
Questions: c. If you asked someone who had learned about division, but not about fractions yet, "what is 7 divided by 3 ?" which result would they give for the dividend and the remainder ? (Warning: in Python version 3 the meaning of 7/3 will change.)
d. How do you get Python to divide in the same way giving a whole number (integer) result and a remainder ?
e. How do you get Python to give the answer as a single result rather than as 2 results, i.e, as a floating point number which can have a fractional part ?
>>> meal="spam and eggs"
'm and eggs'
Questions: a. Describe in your own words how the slicing operator [start:end] applies to extracting parts of the string object called: meal where start and end are starting and ending indexes for the slice.
b. What effect does leaving out start or end have on the slice ?
c. How would you specify a slice of meal that prints 'egg' ?
d. What index value does start use for the first letter (s) ?
e. What do you think the len() function does ? Use the Python interpreter to check whether your answer is right, using len() on other string objects e.g. len("123") .
f. Run Python to assign your full name (e.g. "Josiah Haddock Cheese") to a string object called name , and use a slice operator to output just your middle name. If you have not got a middle name, make one up for the purpose of the exercise. See if it is possible on your system to cut and paste the lines of Python code you used to do this job into a text file. In either case save a text file which you can run as a Python program "midname.py" which extracts and prints your middle name.
g. Experiment with the slice [:-1] applied to your full name and write down what happens. Try again with [:-2] . Try this operation on a string of different length. Write down your conclusions.
>>> a="spam and eggs"
>>> "a meal of %s with chips" % a
'a meal of spam and eggs with chips'
>>> b="sausages and spam"
>>> "a meal of %s with chips" % b
'a meal of sausages and spam with chips'
>>> "a meal of %d eggs with chips" % 3
'a meal of 3 eggs with chips'
>>> "I like %f spoonfuls of sugar in my coffee" % (3/2.0)
'I like 1.500000 spoonfuls of sugar in my coffee'
>>> "I like %.1f spoonfuls of sugar in my coffee" % (3/2.0)
'I like 1.5 spoonfuls of sugar in my coffee'
Question a: How would you print out the number of spoonfuls to exactly 2 decimal places ?
>>> "My name is %s" % name
'My name is Eric'
Question b: Explain in your own words the difference between %f, %d and %s when substituting different types of variable object within strings. Use the Python interpreter to check what happens when you use the wrong letter after % for the object you want to substitute and write what happens in your logbook.
>>> VAT =17.5
>>> "The price includes VAT of %.1f%%" % VAT
'The price includes VAT of 17.5%'
Question c: Why were 2 % symbols needed after .1f to print out a single % symbol ?