ITI 1120 Lab # 2: Numbers, Expressions, Assignment Statements, Functions Bit of Strings
ITI 1120 Lab # 2: Numbers, Expressions, Assignment Statements, Functions Bit of Strings
ITI 1120 Lab # 2: Numbers, Expressions, Assignment Statements, Functions Bit of Strings
Lab # 2
1
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:
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
3
Before starting, always make sure you
are running Python 3
This slide is applicable to all labs, exercises, assignments … etc
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
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
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
5
Task 1
6
Task 2
7
Task 2 (in case coursera does not
work)
• Only do this if coursera web page is giving you difficulties. The quiz
in the following file Task2-lab2.pdf is the same as on coursera
webpage.
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.
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.)
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?)
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)
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.
14