Python Exercises lab 1

Prerequisites

You must obtain a logbook for this module. This is an essential part of the learning process, and you are expected to get this initialled and dated by your tutor regularly, so that feedback can be given. Your logbook will be used for your hand-written notes while you are carrying out practical exercises and experiments. A substantial part of the assignment will be based upon your adherence to the required logbook procedure.

On completing this lab you should be able to:

Securing the learning objectives for each lab in this study module is expected to require:

Instructions

1.1

Start up the python interpreter using your system. You should be presented with the Python version and platform. Write down the information Python states about its version and platform in your logbook. This information should be followed by the interactive python prompt: >>>

1.2

Use this prompt to calculate the area of a circle of radius 1.5 using the formula àR2 where R is the radius. Confirm using a calculator that the result is correct. Write down the result and how you achieved this and validated it in your logbook.

1.3

Using a text editor as provided by your operating system (e.g. notepad or vi) or within a Python IDE (Integrated Development Environment) create a file containing a python program which prints the area of a circle of radius 1.5 and save it as circle.py . Using your python prompt run this program using import circle .

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.

1.4 Adding and Subtracting in Python

>>> 2+2
4
>>> 3.1+5.6
8.6999999999999993

Question: If 2+2 is exactly 4 why do you think 3.1+5.6 is not exactly 8.7 ?

>>> a=3.1
>>> b=5.6
>>> c=a+b
>>> c
8.6999999999999993
>>> d=b-a
>>> d
2.4999999999999996

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 ?

1.5 Printing out values with a specified precision

>>> "%f" % 8.7
8.6999999999999999993
>>> "%.1f" % 8.7
8.7

Question: What does the .1 between the first % and f do in the above expressions ? Try these using .2 and .3 instead of .1 in Python to confirm whether or not you got the correct answer.

1.6 Evaluation order and precedence of expressions in Python

>>> 3+4-2+5
10

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 ?

>>> 8-5-3
0
>>> 8-(5-3)
6

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?

1.7 Multiplying and dividing using Python

>>> 3*5
15

Question a: What does the * operator do with the operands 3 and 5 ?

>>> 3+4*2
11

Question b: Which operation was carried out first, multiplication or addition ?

>>> 7/3
2
>>> 7%3
1
>>> 7.0/3
2.3333333333333335

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 ?

1.8 Using Python with strings: slices and length

>>> meal="spam and eggs"
>>> meal[5:8]
'and'
>>> meal[3:]
'm and eggs'
>>> meal[:5]
'spam '
>>> len(meal)
13

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.

1.9 Printing variables within strings

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

  >>> name="Eric"
>>> "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 ?