Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
61 views

Lab-Python 2

This document provides instructions for Lab 2 of the COS4015-B Technical and Professional Skills course. It includes downloading code files, running them in Jupyter Notebook, and completing exercises on Boolean expressions, if/else statements, if/elif/else statements, finding errors using print statements, pretty printing, and defining functions.

Uploaded by

a.isaku23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Lab-Python 2

This document provides instructions for Lab 2 of the COS4015-B Technical and Professional Skills course. It includes downloading code files, running them in Jupyter Notebook, and completing exercises on Boolean expressions, if/else statements, if/elif/else statements, finding errors using print statements, pretty printing, and defining functions.

Uploaded by

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

COS4015-B Technical and Professional Skills (TPS)

Lab 2

For today’s lab, you need this document, plus the various python files included in the zipped
code-2.zip.

Download code-2.zip to Desktop and unzip it. This should create a new directory (folder)
called code-2 with some Python files we’ve created for you. Now, open up Anaconda
Navigator, and choose Jupyter Notebook. In order to make sure you are working on the
right directory, run this:

cd ~/Desktop/code-2

Please note that the path could be different depending on where you download the folder. In
order to use the accurate path, find the location of the folder, hold down the Shift key and
right click the folder; select ‘copy as path’; then paste the path to the Jupyter Notebook.

1 Boolean Expressions

For each example in this section, write down what you think is the value of the expression.
Then use Python in interactive mode to find out if your answer is OK or not. If not, try
to figure out why Python gave the answer that it did. Ask a staff member for clarification if
necessary.

1.1 Numerical Examples

Assume that x,y and z are initialized as follows


>>> x = 1
>>> y = 5
>>> z = 10

No complete this table:

Expression I Think the Value Is Python Says Notes

1 x < z True True


2 2*y >= z
3 2*y < z
4 (x>1) or (z!=7)
5 y != (z/2)
6 (x>0) or ((y>0)
and (z<0))
7 ((x>0) or (y>0))
and (z<0)
1
1.2 String Examples

Assume that x, y and z are initialized as follows


>>> x = 'Cornell'
>>> y = 'Harvard'
>>> z = 'Yale'

No complete this table:

Expression I Think the Value Is Python Says Notes

1 x != z True True
2 x == 'cornell'
3 len(x) > len(y)
4 y[1:] > z[1:]
5 len(x+z) >
len(y)

2 The If-Else Construction

Assume that s is initialized with a string. Complete the following so that it prints Plural if s
ends with “es” and Not Plural if it does not.
if_________:
print('Plural')
else:
print('Not Plural')

Try this out in Python interactive mode to see if you are correct. This will involve doing:
assign a string ending in “es” to variable s; type in the entirety of the if-else statement
you have completed above; and see what the answer is. Then do the same thing, except
first assigning s to a string that ends in, say, “ss”.

Assume that s is initialized with a string of digits, e.g., '12345'. Complete the following
(write code in the blank spaces) so that it prints the value of the middle digit if s has odd
length. Thus, if s is the string '12945', then 9 should be displayed. If s has even length
then it should print the value of the last two digits. Thus, if s is '1246' then 46 should be
printed.

if_________:
_________
else:
_________

In working on this problem, you may find it easier to enter your code in a new python
program in a text editor, so you can make changes more easily. Remember to run your new
program in the command shell.
2
Use print statements to discover the sources of any errors.

3 If-Elif-Else

Consider the following code:

x = input('Enter x')
y = input('Enter y')

if 1<=x<=3 and 1<=y<=3:


print('A')
elif x>3:
print('B')
elif y<1:
print('C')
elif y>3:
print('D')
else:
print('E')

Complete the following table:

x y Output
2 2 A
1 0
4 1
1 5
0 3
3 0

4 Finding Errors Using Print Statements

Read the docstring for Hyphenator_broken.py (the comment in triple quotes). This
program is supposed to put a hyphen in the middle of a given string if it has an even length,
or put a hyphen around the middle character if it has an odd length (For example, 'abcdef'
becomes 'abc-def'; and 'abcde' becomes 'ab-c-de').

Run program Hyphenator_broken.py and give it inputs 'abcde' and 'abcdef' to see
that it works correctly for odd length strings, but not for even length strings.

Open the file Hyphenator_broken.py in a text editor, and analyse it why it does not
generate the correct output. Fix this program to make sure it works for both types of inputs.

5 Pretty Printing

3
Another file we provided us is FormatPlay.py. It produces the following output:

Numerator Denominator Quotient Error

355 113 3.141592920353983 2.667642e-07

Modify the last line in FormatPlay.py so that the following output is reproduced:

Numerator Denominator Quotient Error

355 113 3.1415929 2.67e-07

In other words, center the numbers under the column headings, display the Quotient
through seven decimal places, and display the Error with three significant digits. Do this
by playing with the format specifications and blanks in the string ‘%3d %3d %22.15f
%10.6e’. What does your new print statement look like?

In other words, center the numbers under the column headings, display the Quotient
through seven decimal places, and display.
Change the first print statement to
print('\n\n\n\nNumerator Denominator Quotient Error')

What does \n seem to do?

6 Functions

6.1. Write a function decimal which returns the decimal part of a number. If decimal part is
zero function should return this string: "integer".

6.2. Write a function max(a,b,c) that returns the greatest of a, b, c.

You might also like