0% found this document useful (0 votes)
43 views23 pages

01D Programming Part 2 (Homework)

This document provides an overview and roadmap for a computational thinking course covering algorithm design and analysis, fundamental data structures, and computational intractability and heuristic reasoning over 13 weeks. It discusses writing user-defined functions in Python, including function definitions, parameters, return values, and docstrings. It also covers conditional execution using if/elif/else statements, Boolean expressions, and built-in data types like strings. Exercises are provided to practice these concepts.

Uploaded by

Isabelle Seet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views23 pages

01D Programming Part 2 (Homework)

This document provides an overview and roadmap for a computational thinking course covering algorithm design and analysis, fundamental data structures, and computational intractability and heuristic reasoning over 13 weeks. It discusses writing user-defined functions in Python, including function definitions, parameters, return values, and docstrings. It also covers conditional execution using if/elif/else statements, Boolean expressions, and built-in data types like strings. Exercises are provided to practice these concepts.

Uploaded by

Isabelle Seet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

SMU Classification: Restricted

COR-IS1702: COMPUTATIONAL THINKING


WEEK 1D: PROGRAMMING PART 2

2022/23 Term 1
SMU Classification: Restricted

Road Map

Algorithm Design and Analysis


✦ Week 1: Introduction, Counting, Programming
✦ Week 2: Programming
Homework for Wk 1
✦ Week 3: Complexity
✦ Week 4: Iteration & Decomposition
✦ Week 5: Recursion

Fundamental Data Structures


(Weeks 6 - 10)

Computational Intractability and Heuristic Reasoning


(Weeks 11 - 13)
SMU Classification: Restricted

Writing Your Own Functions


• You have used the sqrt() function. Someone wrote it, and you called it.
• Now you are going to write (or define) your own function for others
(or yourself) to call.
• You can define a function called celsius like this:

• And then use it:

• Sometimes, we say we invoke or call the celsius function.


SMU Classification: Restricted

…Writing Your Own Functions


There are 2 perspectives:
From the function user's point-of-view: From the function writer's point-of-view:
• you are the service consumer. • you are the service provider
• the function is a "black box"; you do • you need to know how to how to write
not need to know what goes on inside the code inside the function
it  the details of how the function • the function's name and inputs form the
works is abstracted from you "contract" between your function & the
• you simply call/invoke the function function user.
using its name and pass in any • you should document "what" the
necessary inputs function does for your function's user.
• you expect the function to return the "how" it is done may not be important
correct output to you to the user.
SMU Classification: Restricted

In-class Ex 4: T31-40
• The next Tutorial Project requires you to write two functions:
celsius()
countertop()

• Try Tutorial Project T31-40 (p.49 of the PDF or p.35 of


textbook)
– Go to
https://colab.research.google.com/drive/1jPE4-QT8nhubE9gl1LhWx
QWzxslULMNN
SMU Classification: Restricted

Reflecting on Tutorial Project T31-40


Arguments (values passed into a function)
function definition
def f1(x):
return x * 2
when calling it:
f1(10)

A function can take in multiple values:


function definition x corresponds to 10,
y corresponds to 20
def f2(x, y):
return x * y
when calling it:
f2(10, 20)
SMU Classification: Restricted

…Reflecting on Tutorial Project T31-40


Alternatively, when calling a function, you may specifically state which
argument(value) is to be assigned to which parameter. This works as well:
function definition
def f1(x):
return x * 2
when calling it:
f1(x=10)

function definition
def f2(x, y):
return x * y
when calling it:
f2(x=10, y=20)
SMU Classification: Restricted

…Reflecting on Tutorial Project T31-40


• You can have multiple statements in a function. The statements in the function will run
sequentially. E.g.
def f3(x, y):
z=x*y
return z
• return and def are "reserved words" or "keywords" in Python. You cannot choose
keywords as variable or function names.
• In Python, the start and end of functions are not demarcated by symbols. The way you
indent the statements will tell Python where the function starts and ends.
• You can have a function that takes in nothing
• You can have a function that doesn't have a return statement (and hence return
None).
SMU Classification: Restricted

…Reflecting on Tutorial Project T31-40


• You can insert "docstrings" in a function. Docstrings are helpful
hints to programmers who use your function as to what it
does.
– Use help(<function_name>) to see the docstring for that function.
– Try: help(sqrt) docstring
SMU Classification: Restricted

Boolean Expressions
• A Boolean expression is a combination of values, variable names and operators,
and
– Always evaluates to either True or False.
• E.g. of Boolean expressions:
– 10 > 5
– 10 < 10
– 10 <= 10
– x == 10
• These are Boolean operators:
<, <=, >, >=, ==, !=
• Important:
== is not the same as =
SMU Classification: Restricted

Conditional Execution
• New keywords: if, else
• tax_rate is a function to compute tax rate depending on income
level:
– People who earn less than $10k will be taxed at 0%, but
– People who earn $10k or more will be taxed at 5%
SMU Classification: Restricted

…Conditional Execution
• New keyword: elif (else if)
• Let's say the tax rate calculation rules have changed to 3 tiers:
– People who earn less than $10k will be taxed at 0% (same), but
– People who earn $10k to below 20k will be taxed at 5%, and
– People who earn $20k or more will be taxed at 7%.
SMU Classification: Restricted

In-class Ex 5: T41-46
• Try Tutorial Project T41-46 (p.52 of the PDF or p.38 of
textbook)
– Go to
https://colab.research.google.com/drive/19wneP3Hm4VovweLAZWF
9VqEpwpxXoB_H
SMU Classification: Restricted

Reflecting on Tutorial Project T41-46


• An if block may contain multiple statements. E.g. this is OK:
def f1(x):
if x > 10:
print("bigger than 10")
return True
else:
print("smaller or equal 10")
return False

• When calling a function, once the program reaches a "return" statement, the
function terminates immediately.
SMU Classification: Restricted

…Reflecting on Tutorial Project T41-46


• This function doesn't make sense because lines 4, 5 and 8 will
never get a chance to run. But will not cause an error:
SMU Classification: Restricted

…Reflecting on Tutorial Project T41-46


• It is possible to use if without else or elif:
SMU Classification: Restricted

…Reflecting on Tutorial Project T41-46


• It is possible to use if without else or elif:

To be viewed as 1 statement.

When line 2 is executed, and the Boolean


expression is False (i.e. x>10 is False), the
program will move to line 4.
SMU Classification: Restricted

Strings
• We have seen integers, floats and booleans. Data can be stored as strings as
well.
• A string is a set of characters (letters, digits, punctuation marks, spaces etc.) enclosed in quotes.
• We can store strings in variables:
>>> s = "hello"
• We can do interesting things with strings:
>>> "pine" + "apple" String concatenation

>>> s + "world"
>>> s * 3 Returns a Boolean depending on whether
>>> "hell" in s "hell" is a substring of s

>>> len(s) Returns the no. of characters in s

>>> str.count(s,"l")
>>> s.count("l")
SMU Classification: Restricted

In-class Ex 6: T47-65
• Try Tutorial Project T47-65 (p.55 of the PDF or p.41 of textbook)
– Go to
https://colab.research.google.com/drive/1NvGDjZn4xAdCacqL6epTYoGwBGJPnrM9

• Typos in textbook (already rectified in colab):


– T48:
• Code given for plural.py line 1: should be "w" instead of "word"
– T49:
• len((s + '! ') * 2)
Exclamation mark and a space
SMU Classification: Restricted

Reflecting on Tutorial Project T47-65

• How do you include a quotation mark in a string?


– Search for "python escape characters"
SMU Classification: Restricted

In-class Ex 7: T66-71
• Try Tutorial Project T66-71 (p.60 of the PDF or p.46 of textbook)
– Go to https://colab.research.google.com/drive/18PdggEz01YXoV0LZoH1mIVUjjtsuVi0n
– You will get to use the type() function.

– Typo in textbook (already rectified in colab)


• T68 should be:
x ** 2
Instead of
s ** 2
SMU Classification: Restricted

Reflecting on Tutorial Project T66-71


• In Python, a variable can be used to store a value
of any type:
– A variable used to store an integer may be reassigned to store a string
– You can check if a variable is of a particular type by using the is keyword -->
– In order to write a more robust function, you may want to check if the
value that the caller has passed in is of a certain type.
SMU Classification: Restricted

Summary
• Programming part 1 (week 1 class time):
– Arithmetic operations (operand, operator)
– Integers vs. floats
– Calling a function (e.g., sqrt)
– Using variables
• Programming part 2 (this slide deck)
– Writing a function for others to call
– Conditionals (if, elif, else)
– Strings
– type() function
• In week 2, we will look at other Python features such as loops and lists.

You might also like