Description
What is in this lab?
Objective of this lab is to get familiar with Python’s expressions,
function calls, assignment statements and function design via:
1. 2 Tasks (each with a number of questions). You should try to do
these tasks at home on a paper.
2. and 4 programming exercises
The slides that are included here but do not display either Tasks or
Programming exercises are there for you as a reminder of some
relevant material that is needed for this lab.
2
Starting Lab 2
• Open a browser and log into Brightspace
• On the left hand side under Labs tab, find lab2 material
contained in lab2-students.zip file
• Download that file and unzip it.
3
Before starting, always make sure you
are running Python 3
This slide is applicable to all labs, exercises, assignments … etc
ALWAYS MAKE SURE FIRST that you are running Python 3
That is, when you click on IDLE (or start python any other way)
look at the first line that the Python shell displays. It should say
Python 3
If you do not know how to do this, read the material provided
with Lab 1. It explains it step by step
4
div // and mod % operators in Python
// is called div operator in Python. It computes integer division
% is called mod operator in Python. It computes the remainder of integer division
If uncertain, here is how to compute a//b and a%b
1. Compute first x=a/b
2. a//b is then equal to the whole (i.e integer) part of x. More precisely a//b
is equal the integer that is closest to a/b but not bigger than a/b
3. a%b is equal to a – (a//b) * b
5
Task 1
• Open the pdf file called in Task1-lab2.pdf
• Read the instructions and complete all the exercises
Note: If you have not printed this document beforehand or do not
have a tablet with a pen, just take a piece of blank paper and
write your answers on that paper.
6
Task 2
• Go to coursera webpage and log in.
• Go to this link:
https://www.coursera.org/learn/learn-to-program/home/welcome
• Go to Week 1, Assessments and complete Quiz 1 (online)
7
Task 2 (in case coursera does not
work)
• Only do this if coursera web page is giving you difficulties. The quizin the following file Task2-lab2.pdf is the same as on coursera
webpage.
• Open the pdf file called in Task2-lab2.pdf
• Read the instructions and complete all the questions
8
Strings
!”#$%%&’&(“#'( &”‘)*)+, -.($’# /&0)0#”123)+4#$”% 3((.)$”#(35)6’70#89’:(“#:$7#
7’+&”*#(35)6’70#/;(+#”(<#':&"=#(-#(35)6'7#$7#517'#>$.1)74
•?#7’+&”* &7#$#7)@1)”6)#(-#6:$+$6′)+7#3)’<))"#7&"*.)#@1(')7,#%(13.)#@1(')7#(+#
'+&A.)#@1(')70#
BC:&7#&7#$#7'+&"*D
E(')#':$'#':)7)#$+)#$.7(#7'+&"*7F#
G#G#######':&7#&7#$#7'+&"*#':$'#&7#6(2A+&7)%#(-#(")#3.$"=#7A$6)
BHIJD###':&7#&7#$#7'+&"*#1".&=)#HIJ#<:&6:#&7#$"#&"')*)+
•K'+&"*7#6$"#3)#$77&*")%#'(#>$+&$3.)70#LM$2A.)7F
7NOBP$++9D
7HOG8(”)+Q
•C:)+)#$+)#2$”9#(A)+$’&(“7#’:$’#6$”#3)#$AA.&)%#(“#7’+&”*70#;(+#)M$2A.)#<:)"#
':)#R (A)+$'(+#&7#$AA.&)%#'(#'<(#7'+&"*7,#':)#+)71.'#&7#$#7'+&"*#':$'#&7#':)#
6("6$')"$'&("#(-#':)#'<(0#;(+#)M$2A.),#7NR7H,#<(1.%#+)71.'#&"#$#7'+&"*# BP$++98('')+D
E(')#':$'#GC:)#9)$+#&7#GR#HIHI#<(1.%#6$17)#$#79"'$M#)++(+#7&"6)#':)#R#
(A)+$'(+#6$"#3)#$AA.&)%#'(#'<(#"123)+7#(+#'<(#7'+&"*7#31'#"('#':)#2&M#
(-#':)#'<(0#C:&7#:(<)>)+#&7#$#>$.&%#)MA+)77&(“#SC:)#&7#9)$+#GR#GHTHIQ#
!”#$%&'()*%’$(*’+ %,-.(#%.’/%.’*#.0&1*2’3#’4(&’5-‘(,,)0-6’#%'(‘*#.0&1′
(&6′(&’0&#-1-.2’718’9’+’:)(;’10<-*'=)()()()(> 9
Programming Exercises
Pretend that the following 4 programming questions are your Assignment 1.
Write all your solutions to the following 4 questions in one file called
lab2_prog_solutions.py
You will be instructed to do a similar thing in your Assignment 1.
IMPORTANTE NOTE: for this LAB and the ASSIGNMENT(s):
If a question specifies the function name and the names of its parameters, then
that same function name and function parameter names must be used when
programming your functions. That will be the case in every question in your
assignment 1. For example in the question on the next page, your function
definition MUST start with:
6-/’.-,-(#-.?*@A’*BA’&C8′
(*’#$(#’0*’*,-40/0-6′(*'(‘,(.#’%/’#$-‘DE-*#0%&
10
Programming exercises 1
Write a function called repeater(s1, s2, n) that given two strings s1 and s2
and an integer n returns a string that starts with an underscore, then s1 and
s2 alternate n times, then ends with an underscore. (For those who know
loops: you may not use loops to solve this questions.)
Testing your code:
Here is what the output of your function should look like when you make the
following function calls:
11
Programming exercises 2
Read the first paragraph of this page on quadratic equation and finding its roots (it.
solutions)
https://en.wikipedia.org/wiki/Quadratic_equation
Write a function called roots(a, b, c) that given three coefficients a and b and c prints a
nicely formatted message displaying the equation and its two roots (the two roots may be
the same number). You may assume that a is a non zero number, and that a and b and c
are such that b2-4ac is a positive number. (Do you know why we are making this
assumption?)
Testing your code:
12
Programming exercises 3
Think back on the previous question …
Write a function called real_roots(a, b, c) that returns True if the quadratic equation with
the given three coefficients a and b and c has real roots. Otherwise it returns False.
Recall that roots of a quadratic equation are real if and only if b2-4ac is a non-negative
number. (Do not use if statements nor loops)
Testing your code:
13
Programming exercises 4
Write a function called reverse(x) that given a two digit positive integer x
returns the number with reversed digits. (You may assume that x is a two
digit positive integer). (Do not use if statements nor loops)
Hints: Think of mod and div operators and how they can help. What number
should you div x with to get the 1st digit.