PythonIntermedTut

Download Report

Transcript PythonIntermedTut

CSC1018F:
Intermediate Python
(Tutorial)
Diving into Python Ch. 4
Think Like a Comp. Scientist Ch. 1-6
Mini-Test (1)
The precedence of Python mathematical operators from
highest to lowest is:
a.
b.
c.
d.
Parentheses, Addition/Subtraction, Multiplication/Division,
Exponentiation
Parentheses, Exponentiation, Multiplication/Division,
Addition/Subtraction
Parentheses, Exponentiation, Addition/Subtraction,
Multiplication/Division
Always from left to right in the expression
The following sequence of Python statements
>>> minute = 59
>>> minute / 60.0
will return:
a.
b.
c.
0.983333333333
0
a syntax error
Mini-Test (2)
The python statement: 0 or "" and "weird" will
return:
a.
b.
c.
d.
e.
True
False
""
"weird"
0
Which of the following statements does NOT apply
to lambda functions?
a. Lambda functions require parentheses around the
arguments
b. Lambda functions may take any number of arguments
c. Lambda functions may contain multiple expressions
d. The return keyword is implied
Mini-Test(3)
Which of the following statements does apply to the
getattr built-in function:
a. gettatr only works on native datatypes
b. getattr(li, "pop") will execute pop on the list li
c. getattr can take an optional third argument which is a default
value
d. The value of the arguments passed to getattr must be known
at compile time
The Python statement x = "CSC" + str(x) + "F" will:
a.
b.
c.
d.
Be rejected by the Python interpreter
Be accepted by the Python interpreter only if x is an immutable
datatype
Be accepted by the Python interpreter only if x is an int, float,
dictionary, string, list or tuple
Always be accepted by the Python interpreter
Mini-Test (4)
Given a function defined as follows:
def opt(first, second = 0, third = {}):
In order to assign 1 to first, 2 to second, and {} to
third, you could call opt as:
opt(1, 2)
opt(second = 2, first = 1)
opt(third = {}, 1, 2)
opt(1, 2, {})
Tut Q1: Image Thresholding
Assume that an image is represented:
With greyscale pixel values in the range 0 (black) to 255
(white)
As a list of lists of integers
Each sublist represents a row of pixels
Write functions to perform the following operations:
Threshold(image, threshval) - return an image which
has all values below threshval converted to 0 and all those
equal or above to 255. Threshval should default to 100
Challenge: try and compress the body of the function into
a single line
Tut Q2: More Image
Manipulation
Create image functions to:
CheckValidity(image) - return true if every row
of an image is of the same length
Expand(image) - return an image with double
the horizontal resolution of the input image
created by adding an equal amount of white
pixels to left and right of the image. Assume that
the horizontal resolution is an even number.