$30
ITI 1120
Lab # 2
numbers, expressions, assignment statements, functions … bit of strings
1
Objec/ves
Ge3ng familiar with Python’s expressions, func/on calls,
assignment statements, func/on design via:
• two exercise sets (one on paper, on one the web)
• four programming exercises
2
Star/ng Lab 2
• Open a browser and log into Blackboard Learn
• On the leM hand side under Labs tab, find lab2 material
contained in lab2-students.zip file
• Download that file to the Desktop and unzip it.
3
Task 1
• Open the pdf file called in lab2-exercises.pdf
• Read the instruc/ons 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.
4
Task 2
• Open a browser and go to
hUps://class.coursera.org/programming1-002
• If you have not opened an coursera account open one, under
your own name, and remember your account for the rest of
this semester.
• Log in and click on “go to course” if you are not already in the
course area
• On the leM hand side click on Exercises
• A page en/tled “Homeworks” will open.
• Click on Week 1
• Click on AUempt Homework
• Answer all 15 ques/ons and click Submit Answer to see how
well you did. The coursera will automa/cally grade you. It will
give you some understanding of how well you understand the
material. You can pick a random answer for Ques/on 2 since
we did not cover that material yet. 5
Strings In addition to integer, float (i.e. number) and boolean objects. Python has
string objects. (For now think of objects as just values)
• A string is a sequence of characters between single quotes, double quotes
or triple quotes.
‘This is a string’
Note that these are also strings:
“ “ this is a string that is comprised of one blank space
‘257’ this is a string unlike 257 which is an integer
• Strings can be assigned to variables. Examples:
s1=‘Harry’
s2=“Potter”
• There are many operations that can be applied on strings. For example
when the + operator is applied to two strings, the result is a string that is the
concatination of the two. For example, s1+s2, would result in a string ‘HarryPotter’
Note that “The year is “+ 2525 would cause a syntax error since the +
operator can be applied to two numbers or two strings but not the mix
of the two. This however is a valid expression 'The is year “+ “2025”
Python also has * operator for strings. It can be applied to a string
and an integer. Eg: 4 * “la” gives ‘lalalala’ 6
Programming Exercises
Pretend that the following programming ques/ons are your Assignment 1. And
solve the following ques/ons as if they were assignment 1. In other words, write
all the following ques/ons in one file called lab2_prog_solu/ons.py
IMPORTANTE NOTE: for this LAB and the ASSIGNMENT(s):
If a ques/on specifies the func/on name and the names of its parameters, then
that same func/on name and func/on parameter names must be used when
defining the func/ons. That is the case in every ques/on in your assignment 1.
For example in the ques/on on the next page, your func/on defini/on MUST
start with:
def repeater(s1, s2, n):
as that is specified as a part of the question
7
Ques/on 1:
Write a func/on 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 /mes, then ends with an underscore. (For those who know
loops: you may not use loops to solve this ques/ons.)
Tes/ng your code:
Here is what the output of your func/on should look like when you make the
following func/on calls:
8
Ques/on 2:
Read the first paragraph of this page on quadra/c equa/on and finding its roots (it.
solu/ons)
hUps://en.wikipedia.org/wiki/Quadra/c_equa/on
Write a func/on called roots(a, b, c) that given three coefficients a and b and c prints a
nicely formaUed message displaying the equa/on and its two roots (the two roots maybe
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 posi/ve number. (Do you know why we are making this
assump/on?)
Tes+ng your code:
9
Ques/on 3:
Think back on the previous ques/on …
Write a func/on called real_roots(a, b, c) that returns True if the quadra/c equa/on with
the given three coefficients a and b and c has real roots. Otherwise it returns False.
Recall that roots of a quadra/c equa/on are real if and only if b2-4ac is a non-nega/ve
number.
Tes+ng your code:
10
Ques/on 4:
Write a func/on called reverse(x) that given a two digit posi/ve integer x
returns the number with reversed digits. (You may assume that x is a two
digit posi/ve integer)
Hints: Think of mod and div operators and how they can help. What number
should you div x with to get the 1st digit.
Tes+ng your code:
11