Download presentation
Presentation is loading. Please wait.
1
CSC1018F: Intermediate Python (Tutorial)
Diving into Python Ch. 4 Think Like a Comp. Scientist Ch. 1-6
2
Mini-Test (1) The precedence of Python mathematical operators from highest to lowest is: 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 syntax error
3
Mini-Test (2) The python statement: 0 or "" and "weird" will return:
True False "" "weird" Which of the following statements does NOT apply to lambda functions? Lambda functions require parentheses around the arguments Lambda functions may take any number of arguments Lambda functions may contain multiple expressions The return keyword is implied
4
Mini-Test(3) Which of the following statements does apply to the getattr built-in function: gettatr only works on native datatypes getattr(li, "pop") will execute pop on the list li getattr can take an optional third argument which is a default value The value of the arguments passed to getattr must be known at compile time The Python statement x = "CSC" + str(x) + "F" will: 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
5
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, {})
6
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
7
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.