0% found this document useful (0 votes)
30 views22 pages

2A Slides

Uploaded by

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

2A Slides

Uploaded by

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

CSC110 WEEK 2A:

INTRODUCTION TO
FUNCTIONS
Sadia Sharmin, Department of Computer Science

Navigation tip for web slides: press ? to see keyboard navigation controls.
RECAP
THE SEVEN MAIN PYTHON DATA TYPES
Data type Description Operations
int, float Numeric data Arithmetic (e.g. +), comparisons (e.g. ==, <)
bool Boolean (True/False) data and, or, not
str Text data ==, +, in, indexing (s[...])
set Collection, no duplicates, no ==, in
order
list Collection, duplicates allowed, ==, +, in, indexing (s[...])
order matters
dict Collection of association pairs ==, in, key lookup (d[...])
LEARNING OBJECTIVES
In this part of the lecture, you will learn to:
1. Define terminology relating to functions in mathematics and
programming.
2. Name and describe some built-in Python functions.
3. Create sequences of integers in Python using the range function.
4. Recognize and write Python code for function call expressions.
FUNCTIONS IN PYTHON
Code we’ve seen so far:
literals (3, 'hello', [1, 2, 3])
operators (+, -, and)
variables and assignment statements (numbers = {1, 2, 3})
comprehension expressions ({x ** x for x in numbers})
How do we build up code with these elements to perform useful
computations?
Recall a mathematical definition of a function: a mapping of
elements from one set A (called the function’s domain) to a set B
(called the function’s codomain). Notation:

f unction : A → B

Example:

f : R → R

2
f (x) = x

Functions take in inputs and return outputs.


f (5) = 25

f (0) = 0

f (−1.5) = 2.25
FUNCTIONS IN PYTHON
Python comes with many built-in functions that do the same thing:
take in input values and return an output value.
HOW TO USE PYTHON FUNCTIONS
Functions are structured like this: function_name(input)
When you type this in, the function does stuff to the input, and gives
you back an output.
For example, abs(number) is the name of the python function that
takes in a number and returns its absolute value
There are many, many Python functions available to use (and they
aren’t just limited to mathematical applications)!
DEMO: SOME BUILT-IN PYTHON
FUNCTIONS
abs
len
sum
sorted
max/min
type
help
Fun fact: dir(__builtins__) will show you all the built-in
functions available
USEFUL FUNCTION: HELP!
If you remember a function’s name, but forget exactly what it does,
you can ask Python for help like so:

>>> help(abs)

Help on built-in function abs in module builtins: abs(...)

abs(number) -> number


Return the absolute value of the argument.

Type q to get out of the help screen


TERMINOLOGY
>>> abs(-5)
5

abs(-5) is a function call expression – “call” means to use (or


execute) a function
abs(-5) evaluates to 5
abs is the name of the function being called
-5 is an argument (the input to the function; for functions with
multiple arguments, we separate them by commas)
or, “-5 is passed to abs”
abs(-5) returns 5
EXERCISE 1: PRACTICE WITH BUILT-IN
FUNCTIONS
A USEFUL FUNCTION – range: A
SEQUENCE OF NUMBERS
For integers m and n, range(m, n) represents the sequence of
numbers m, m + 1, …, n - 1.
Note: the start of range is inclusive, but the end of range is exclusive.
This ensures the size of range(m, n) is always n - m.
RECAP: COMPREHENSIONS
In Python, we can use set comprehensions to express sets.

>>> nums = {0, 1, 2, 3, 4, 5}

>>> {x ** 2 for x in nums}


{0, 1, 4, 9, 16, 25}

Set builder notation Set comprehension expression


2
{x ∣ x ∈ N}
{x ** 2 for x in nums}
TWO OTHER COMPREHENSION TYPES
List comprehension:

>>> nums = {0, 1, 2, 3, 4, 5}


>>> [x ** 2 for x in nums]
[0, 1, 4, 9, 16, 25]

Dictionary comprehension:

>>> nums = {0, 1, 2, 3, 4, 5}


>>> {x : x ** 2 for x in nums}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
GENERAL COMPREHENSION SYNTAX
Set comprehension:

{ <expression> for <variable> in <collection> }

List comprehension:

[ <expression> for <variable> in <collection> ]

Dictionary comprehension:

{ <key_expr>: <value_expr> for <variable> in <collection>


range WITH COMPREHENSIONS
Problem: compute the reciprocals of the
numbers between 1 and 20, inclusive.

Demo!
COMPREHENSIONS WITH MULTIPLE
VARIABLES
Consider this new set operation, the Cartesian product:

A × B = {(x, y) ∣ x ∈ A and y ∈ B}

Example:

{1, 2} × {10, 20} = {(1, 10), (1, 20), (2, 10), (2, 20)}

We can do this in Python as well: demo!


EXERCISE 2: COMPREHENSIONS AND
range
TODAY YOU LEARNED TO…
In this lecture, you learned to:
1. Define terminology relating to functions in mathematics and
programming.
2. Name and describe some built-in Python functions.
3. Create sequences of integers in Python using the range function.
4. Recognize and write Python code for function call expressions.
CHECKLIST FOR THE WEEK
Be sure to do the following for homework, if you haven’t already
done so :)
Read Chapters 1 and 2
Complete Week 1 Preparation and Week 2 Preparation tasks on
Quercus
Make sure you completed the Week 2 MarkUs practice problems
as well

You might also like