welcome to csc 108108/lectures/l01.pdf · active learning active learning is a well-supported style...
TRANSCRIPT
Welcome to CSC 108
Introduction to Computer Programming
Lecture 1A
September 13, 2019
1 / 19
Active Learning
Active Learning is a well-supported style of teaching where
material is delivered outside the classroom via online resources.
Lectures are instead made practical. Students come to campus to
practice and work rather than to passively absorb material.
2 / 19
Active Learning
A typical week for CSC108 students is:
Prepare Watch PCRS and answer questions. Due Sundays at
22:00.
Practice Write code in lecture, get feedback from peers and
teaching sta↵.
Perform Take a weekly online quiz. Due Fridays at 22:00.
First PCRS homework is due September 20th.
3 / 19
This is a first year computer science course for people who have
never programmed. That’s OK!
Course-Coordinator
Andrew Petersen
Instructors
Michael Liut
Paul Vrbik
Vincent Maccio
4 / 19
What you will learn
1. Basic programming assuming no past programming
knowledge.
2. How to trace programs, write functions, and analyze basic
algorithms.
Concepts
•Variables •Boolean logic •Functions • If statements •For andWhile loops •Lists •Dictionaries •Larger program design
•Basic complexity analysis • Sorting algorithms • Introduction to
classes.
5 / 19
Course Organization
This is a very large course with over 1000 students.
There are seven di↵erent lecture sections.
Four instructors (Liut, Maccio, Petersen, and Vrbik).
Website: mcs.utm.utoronto.ca/~108
6 / 19
Marking Scheme
Assignment 0 2%
Assignment 1 5%
Assignment 2 10%
Assignment 3 10%
Lab Participationa 8%
Prep and Reviewb 5%
Midterm 20%
Final Examc 40%
a0.5% eachbDue Sunday and Friday weekly.c� 40% required to pass course.
7 / 19
Assignments
1. All assignments are done individually (no groups).
2. Assignments are graded 100% on correctness.
2.1 No grading on style or e�ciency (to a point).
2.2 Grading is done automatically.
3. All assignments are submitted online.
4. After you submit your assignment you will receive a grade.
4.1 You can resubmit your assignment up to 2 days afterwards
with a 20% penalty.
4.2 The maximum of the two grades is the final grade.8 / 19
Labs
1. Lab attendance is mandatory.
2. You must attend the lab your are assigned to (for now).
3. Labs start September 16th.
9 / 19
Piazza
1. Piazza is an online discussion board which we will be using
for the course.
2. Students can ask or answer questions.
3. If you send a content question via email to an instructor,
they may ask you to post it to Piazza instead.
4. If you choose to post anonymously, you are only anonymous
to other students, not to the instructors and TAs.
10 / 19
Email is reserved for personal issues like illness or requesting
(legitimate) accommodation.
Emails must. . .
3. be sent from a @utoronto.ca address.
4. include “CSC108” in subject.
5. include your student number.
6. be brief (or expect TL;DR).
11 / 19
Class Etiquette
1. Please be respectful especially to other students.
2. Do not inform us you are not going to attend class. Class
attendance is your prerogative.
3. Do not project sensitive or inappropriate material to the
screen.
4. Do not put liquid on the table.
5. Professors are people too and make mistakes.
12 / 19
Academic Integrity
Don’t cheat.
. . . we will catch you.
13 / 19
Good Luck! Have Fun!
14 / 19
Questions?
15 / 19
First Task: Install
Task 1
Install Python 3.7.4 from python.org/downloads/.
Task 2
Open Idle (the icon looks like: ) where you should see >>>_
Task 3
Type 2+2 then (the return key). You should get 4 back.
Task 4
Help those around you.
16 / 19
Next Lecture
1. Python as a calculator.
2. Variables.
17 / 19
Python as a Calculatorand
Variables.
Introduction to Computer Programming
Lecture 1B
September 9, 2019
1 / 1
Using Python as a calculator.
Standard Arithmetic
+ - * /
Other operations
// ** %
2 / 1
Warmup
1 >>>
2 + 2
2 4
3 >>> 2 - 3
4 -1
5 >>> 2 * 3
6 6
7 >>> 2 + 3 * 2 - 1
8 7
9 >>> (2 + 3) * 2 - 1
10 9
3 / 1
Warmup
1 >>> 2 + 2
2 4
3 >>> 2 - 3
4 -1
5 >>> 2 * 3
6 6
7 >>> 2 + 3 * 2 - 1
8 7
9 >>> (2 + 3) * 2 - 1
10 9
3 / 1
Warmup
1 >>> 2 + 2
2 4
3 >>>
2 - 3
4 -1
5 >>> 2 * 3
6 6
7 >>> 2 + 3 * 2 - 1
8 7
9 >>> (2 + 3) * 2 - 1
10 9
3 / 1
Warmup
1 >>> 2 + 2
2 4
3 >>> 2 - 3
4 -1
5 >>> 2 * 3
6 6
7 >>> 2 + 3 * 2 - 1
8 7
9 >>> (2 + 3) * 2 - 1
10 9
3 / 1
Warmup
1 >>> 2 + 2
2 4
3 >>> 2 - 3
4 -1
5 >>>
2 * 3
6 6
7 >>> 2 + 3 * 2 - 1
8 7
9 >>> (2 + 3) * 2 - 1
10 9
3 / 1
Warmup
1 >>> 2 + 2
2 4
3 >>> 2 - 3
4 -1
5 >>> 2 * 3
6 6
7 >>> 2 + 3 * 2 - 1
8 7
9 >>> (2 + 3) * 2 - 1
10 9
3 / 1
Warmup
1 >>> 2 + 2
2 4
3 >>> 2 - 3
4 -1
5 >>> 2 * 3
6 6
7 >>>
2 + 3 * 2 - 1
8 7
9 >>> (2 + 3) * 2 - 1
10 9
3 / 1
Warmup
1 >>> 2 + 2
2 4
3 >>> 2 - 3
4 -1
5 >>> 2 * 3
6 6
7 >>> 2 + 3 * 2 - 1
8 7
9 >>> (2 + 3) * 2 - 1
10 9
3 / 1
Warmup
1 >>> 2 + 2
2 4
3 >>> 2 - 3
4 -1
5 >>> 2 * 3
6 6
7 >>> 2 + 3 * 2 - 1
8 7
9 >>>
(2 + 3) * 2 - 1
10 9
3 / 1
Warmup
1 >>> 2 + 2
2 4
3 >>> 2 - 3
4 -1
5 >>> 2 * 3
6 6
7 >>> 2 + 3 * 2 - 1
8 7
9 >>> (2 + 3) * 2 - 1
10 9
3 / 1
Warmup
1 >>> 2 + 2
2 4
3 >>> 2 - 3
4 -1
5 >>> 2 * 3
6 6
7 >>> 2 + 3 * 2 - 1
8 7
9 >>> (2 + 3) * 2 - 1
10 9
3 / 1
1 >>>
4 / 2
2 2.0
3 Notice the decimal.
4
5 >>> type(2)
6 <class ‘int’>
7
8 >>> type(2.0)
9 <class ‘float’>
10
11 >>> 6 / 2 * (1 + 2)
12 9.0
4 / 1
1 >>> 4 / 2
2 2.0
3 Notice the decimal.
4
5 >>> type(2)
6 <class ‘int’>
7
8 >>> type(2.0)
9 <class ‘float’>
10
11 >>> 6 / 2 * (1 + 2)
12 9.0
4 / 1
1 >>> 4 / 2
2 2.0
3 Notice the decimal.
4
5 >>>
type(2)
6 <class ‘int’>
7
8 >>> type(2.0)
9 <class ‘float’>
10
11 >>> 6 / 2 * (1 + 2)
12 9.0
4 / 1
1 >>> 4 / 2
2 2.0
3 Notice the decimal.
4
5 >>> type(2)
6 <class ‘int’>
7
8 >>> type(2.0)
9 <class ‘float’>
10
11 >>> 6 / 2 * (1 + 2)
12 9.0
4 / 1
1 >>> 4 / 2
2 2.0
3 Notice the decimal.
4
5 >>> type(2)
6 <class ‘int’>
7
8 >>>
type(2.0)
9 <class ‘float’>
10
11 >>> 6 / 2 * (1 + 2)
12 9.0
4 / 1
1 >>> 4 / 2
2 2.0
3 Notice the decimal.
4
5 >>> type(2)
6 <class ‘int’>
7
8 >>> type(2.0)
9 <class ‘float’>
10
11 >>> 6 / 2 * (1 + 2)
12 9.0
4 / 1
1 >>> 4 / 2
2 2.0
3 Notice the decimal.
4
5 >>> type(2)
6 <class ‘int’>
7
8 >>> type(2.0)
9 <class ‘float’>
10
11 >>>
6 / 2 * (1 + 2)
12 9.0
4 / 1
1 >>> 4 / 2
2 2.0
3 Notice the decimal.
4
5 >>> type(2)
6 <class ‘int’>
7
8 >>> type(2.0)
9 <class ‘float’>
10
11 >>> 6 / 2 * (1 + 2)
12 9.0
4 / 1
1 >>> 4 / 2
2 2.0
3 Notice the decimal.
4
5 >>> type(2)
6 <class ‘int’>
7
8 >>> type(2.0)
9 <class ‘float’>
10
11 >>> 6 / 2 * (1 + 2)
12 9.0
4 / 1
1 >>>
1 / 3
2 0.3333333333333333
3 Notice the finite number of digits.
4
5 >>> 1 / 0
6 Traceback (most recent call last):
7 File "<stdin>", line 1, in <module>
8 ZeroDivisionError: division by zero
9
10 >>> 1 / float(‘inf’)
11 0.0
5 / 1
1 >>> 1 / 3
2 0.3333333333333333
3 Notice the finite number of digits.
4
5 >>> 1 / 0
6 Traceback (most recent call last):
7 File "<stdin>", line 1, in <module>
8 ZeroDivisionError: division by zero
9
10 >>> 1 / float(‘inf’)
11 0.0
5 / 1
1 >>> 1 / 3
2 0.3333333333333333
3 Notice the finite number of digits.
4
5 >>>
1 / 0
6 Traceback (most recent call last):
7 File "<stdin>", line 1, in <module>
8 ZeroDivisionError: division by zero
9
10 >>> 1 / float(‘inf’)
11 0.0
5 / 1
1 >>> 1 / 3
2 0.3333333333333333
3 Notice the finite number of digits.
4
5 >>> 1 / 0
6 Traceback (most recent call last):
7 File "<stdin>", line 1, in <module>
8 ZeroDivisionError: division by zero
9
10 >>> 1 / float(‘inf’)
11 0.0
5 / 1
1 >>> 1 / 3
2 0.3333333333333333
3 Notice the finite number of digits.
4
5 >>> 1 / 0
6 Traceback (most recent call last):
7 File "<stdin>", line 1, in <module>
8 ZeroDivisionError: division by zero
9
10 >>>
1 / float(‘inf’)
11 0.0
5 / 1
1 >>> 1 / 3
2 0.3333333333333333
3 Notice the finite number of digits.
4
5 >>> 1 / 0
6 Traceback (most recent call last):
7 File "<stdin>", line 1, in <module>
8 ZeroDivisionError: division by zero
9
10 >>> 1 / float(‘inf’)
11 0.0
5 / 1
1 >>> 1 / 3
2 0.3333333333333333
3 Notice the finite number of digits.
4
5 >>> 1 / 0
6 Traceback (most recent call last):
7 File "<stdin>", line 1, in <module>
8 ZeroDivisionError: division by zero
9
10 >>> 1 / float(‘inf’)
11 0.0
5 / 1
1 >>>
2 ** 3
2 8
3
4 >>> 2 ** 3.0
5 8.0
6
7 >>> 2 ** -1
8 0.5
9
10 >>> 2 ** 0
11 1
6 / 1
1 >>> 2 ** 3
2 8
3
4 >>> 2 ** 3.0
5 8.0
6
7 >>> 2 ** -1
8 0.5
9
10 >>> 2 ** 0
11 1
6 / 1
1 >>> 2 ** 3
2 8
3
4 >>>
2 ** 3.0
5 8.0
6
7 >>> 2 ** -1
8 0.5
9
10 >>> 2 ** 0
11 1
6 / 1
1 >>> 2 ** 3
2 8
3
4 >>> 2 ** 3.0
5 8.0
6
7 >>> 2 ** -1
8 0.5
9
10 >>> 2 ** 0
11 1
6 / 1
1 >>> 2 ** 3
2 8
3
4 >>> 2 ** 3.0
5 8.0
6
7 >>>
2 ** -1
8 0.5
9
10 >>> 2 ** 0
11 1
6 / 1
1 >>> 2 ** 3
2 8
3
4 >>> 2 ** 3.0
5 8.0
6
7 >>> 2 ** -1
8 0.5
9
10 >>> 2 ** 0
11 1
6 / 1
1 >>> 2 ** 3
2 8
3
4 >>> 2 ** 3.0
5 8.0
6
7 >>> 2 ** -1
8 0.5
9
10 >>>
2 ** 0
11 1
6 / 1
1 >>> 2 ** 3
2 8
3
4 >>> 2 ** 3.0
5 8.0
6
7 >>> 2 ** -1
8 0.5
9
10 >>> 2 ** 0
11 1
6 / 1
1 >>> 2 ** 3
2 8
3
4 >>> 2 ** 3.0
5 8.0
6
7 >>> 2 ** -1
8 0.5
9
10 >>> 2 ** 0
11 1
6 / 1
Example 1
When x = 17 and y = 3 we have
17÷ 3 is 5 remainder 2
and notice
17 = 5 · 3 + 2.
7 / 1
1 >>>
17 // 3
2 5
3
4 >>> 17 % 3
5 2
6
7 >>> 3 * (17 // 3) + (17 % 2)
8 17
8 / 1
1 >>> 17 // 3
2 5
3
4 >>> 17 % 3
5 2
6
7 >>> 3 * (17 // 3) + (17 % 2)
8 17
8 / 1
1 >>> 17 // 3
2 5
3
4 >>>
17 % 3
5 2
6
7 >>> 3 * (17 // 3) + (17 % 2)
8 17
8 / 1
1 >>> 17 // 3
2 5
3
4 >>> 17 % 3
5 2
6
7 >>> 3 * (17 // 3) + (17 % 2)
8 17
8 / 1
1 >>> 17 // 3
2 5
3
4 >>> 17 % 3
5 2
6
7 >>>
3 * (17 // 3) + (17 % 2)
8 17
8 / 1
1 >>> 17 // 3
2 5
3
4 >>> 17 % 3
5 2
6
7 >>> 3 * (17 // 3) + (17 % 2)
8 17
8 / 1
1 >>> 17 // 3
2 5
3
4 >>> 17 % 3
5 2
6
7 >>> 3 * (17 // 3) + (17 % 2)
8 17
8 / 1
Division Algorithm
Let x and y be positive integers. There is a unique quotient q
and unique remainder r with 0 r < y satisfying:
x = q · y + r.
In Python this means we have
x == (x//y)*y + (x%y).
9 / 1
Task 1
Predict the following output without using your computer.
Expression Result Type of Result
9/3 3.0 float
9//3
9/3
10 / 1
Task 2
Predict the following output without using your computer.
Expression Result Expression Result
9//2 9%2
-9//2 -9%2
9//-2 9%-2
-9//-2 -9%-2
11 / 1
Task 3
Notice that since
1 >>> 1 + 2 * 3 + 4
2 11
we can conclude * is evaluated before +.
Write similar statements to deduce the order of operations for:
+, -, *, **, /, //, and %.
12 / 1
Task 4
With your table, design the simplest arithmetic expression that
you believe will be incorrectly evaluated by your classmates.
Task 5
Evaluate the expressions generated from the class.
13 / 1
Variables
Sometimes it is expedient to give values a name. These names
are called variables.
For instance,
1 >>> x = -2
2 >>> y = 3
3 >>> q = x // y
4 >>> r = x % y
5 >>> x - (q * y + r)
6 0
14 / 1
Variables
Sometimes it is expedient to give values a name. These names
are called variables.
For instance,
1 >>> x = -2
2 >>> y = 3
3 >>> q = x // y
4 >>> r = x % y
5 >>> x - (q * y + r)
6 0
14 / 1
Equal in Python is for assignment and does not denote equality
as it does in mathematics.
1 >>>
x = 1
2 >>> x = x + 1
3 >>> x
4 2
Notice in mathematics that
x = x+ 1 =) 0 = 1
and so is an “illegal” statement.
15 / 1
Equal in Python is for assignment and does not denote equality
as it does in mathematics.
1 >>> x = 1
2 >>>
x = x + 1
3 >>> x
4 2
Notice in mathematics that
x = x+ 1 =) 0 = 1
and so is an “illegal” statement.
15 / 1
Equal in Python is for assignment and does not denote equality
as it does in mathematics.
1 >>> x = 1
2 >>> x = x + 1
3 >>>
x
4 2
Notice in mathematics that
x = x+ 1 =) 0 = 1
and so is an “illegal” statement.
15 / 1
Equal in Python is for assignment and does not denote equality
as it does in mathematics.
1 >>> x = 1
2 >>> x = x + 1
3 >>> x
4 2
Notice in mathematics that
x = x+ 1 =) 0 = 1
and so is an “illegal” statement.
15 / 1
Equal in Python is for assignment and does not denote equality
as it does in mathematics.
1 >>> x = 1
2 >>> x = x + 1
3 >>> x
4 2
Notice in mathematics that
x = x+ 1 =) 0 = 1
and so is an “illegal” statement.
15 / 1
1 >>>
1 = 1
2 1 = 1
3 ^
4 SyntaxError: can’t assign to literal
5
6 >>> 1 == 1
7 True
8
9 >>> x = y
10 NameError: name ‘y’ is not defined
16 / 1
1 >>> 1 = 1
2 1 = 1
3 ^
4 SyntaxError: can’t assign to literal
5
6 >>> 1 == 1
7 True
8
9 >>> x = y
10 NameError: name ‘y’ is not defined
16 / 1
1 >>> 1 = 1
2 1 = 1
3 ^
4 SyntaxError: can’t assign to literal
5
6 >>>
1 == 1
7 True
8
9 >>> x = y
10 NameError: name ‘y’ is not defined
16 / 1
1 >>> 1 = 1
2 1 = 1
3 ^
4 SyntaxError: can’t assign to literal
5
6 >>> 1 == 1
7 True
8
9 >>> x = y
10 NameError: name ‘y’ is not defined
16 / 1
1 >>> 1 = 1
2 1 = 1
3 ^
4 SyntaxError: can’t assign to literal
5
6 >>> 1 == 1
7 True
8
9 >>>
x = y
10 NameError: name ‘y’ is not defined
16 / 1
1 >>> 1 = 1
2 1 = 1
3 ^
4 SyntaxError: can’t assign to literal
5
6 >>> 1 == 1
7 True
8
9 >>> x = y
10 NameError: name ‘y’ is not defined
16 / 1
1 >>> 1 = 1
2 1 = 1
3 ^
4 SyntaxError: can’t assign to literal
5
6 >>> 1 == 1
7 True
8
9 >>> x = y
10 NameError: name ‘y’ is not defined
16 / 1
Task 6
What is y after entering the following
1 >>> x = 2
2 >>> y = 4
3 >>> x = 1
Answer
y = 4.
17 / 1
Task 6
What is y after entering the following
1 >>> x = 2
2 >>> y = 4
3 >>> x = 1
Answer
y = 4.
17 / 1
Task 7
What are x and y after entering the following?
1 >>> x = 1 + 4
2 >>> y = x - 1
3 >>> x = y + 1 * x
4 >>> x = y // x
Answer
(x, y) = (0, 4).
18 / 1
Task 7
What are x and y after entering the following?
1 >>> x = 1 + 4
2 >>> y = x - 1
3 >>> x = y + 1 * x
4 >>> x = y // x
Answer
(x, y) = (0, 4).
18 / 1
Task 8
What is x, y, z after entering the following
1 >>> x = 1
2 >>> y = x + x
3 >>> z = y + y + 1.0
4 >>> y = y // x
5 >>> x = z // x
6 >>> z = x + x / 4
7 >>> y = y ** ((y * y) % 3)
Answer
(x, y, z) = (5.0, 2, 6.25).
19 / 1
Task 8
What is x, y, z after entering the following
1 >>> x = 1
2 >>> y = x + x
3 >>> z = y + y + 1.0
4 >>> y = y // x
5 >>> x = z // x
6 >>> z = x + x / 4
7 >>> y = y ** ((y * y) % 3)
Answer
(x, y, z) = (5.0, 2, 6.25).
19 / 1
Next Lecture
1. Basic functions.
20 / 1
In-Built Functions
Introduction to Computer Programming
Lecture 1C
September 11, 2019
1 / 16
Functions
Unlike variables, Python functions are directly analogous to that
of mathematics and can also be named.
For instance, a parabola in mathematics is given by the function
f(x) = x2
and in Python we write
1 >>> def f(x):
2 ... return x**2
3 >>> f(3)
4 9
2 / 16
In Python four spaces/indents are significant and are used to
associate lines of code with control structures (in this case
function definition).
1 >>> def f(x):
2 ... return x**2
If you get errors along the lines of
1 IndentationError: unexpected indent
check your formatting.
3 / 16
In Python four spaces/indents are significant and are used to
associate lines of code with control structures (in this case
function definition).
1 >>> def f(x):
2 ... return x**2
If you get errors along the lines of
1 IndentationError: unexpected indent
check your formatting.
3 / 16
Question 1
What is the value of x after entering the following?
1 >>> def f(x, y):
2 ... return x % y
3 >>> x = f( -f(5, 2) - 2, f(20, 7) )
Answer
x = 3.
4 / 16
Question 1
What is the value of x after entering the following?
1 >>> def f(x, y):
2 ... return x % y
3 >>> x = f( -f(5, 2) - 2, f(20, 7) )
Answer
x = 3.
4 / 16
Return Statement
return is a reserved word (one that cannot be assigned by us).
It is used to designate what value the function should return
while also terminating the function itself.
Definition 1 (return)
The return statement causes a function to exit and hand back a
value to its caller.
5 / 16
Question 2
What outputs?
1 >>> def example(x):
2 ... print(1*x)
3 ... print(2*x)
4 ... return 3*x
5
6 >>> a = example(1)
7
8
9 >>> a
10
6 / 16
Question 3
What outputs?
1 >>> def example(x):
2 ... print(1*x)
3 ... return 3*x
4 ... print(2*x)
5
6 >>> a = example(1)
7
8
9 >>> a
10
7 / 16
Question 4
What outputs?
1 >>> def example(x):
2 ... return 3*x
3 ... return 2*x
4 ... print(1*x)
5
6 >>> a = example(1)
7
8
9 >>> a
10
8 / 16
Question 5
What outputs?
1 >>> def example(x):
2 ... print(2*x)
3 ... return
4
5 >>> a = example(1)
6
7 >>> a
8
9 >>> type(a)
10
9 / 16
Task 1 (Area of Triangle)
The area of 4ABC given by
A
C
a
B
b
c
isps(s� a)(s� b)(s� c) where s =
1
2(a+ b+ c).
Write a function which computes the area of a triangle from the
side lengths a, b, c.
10 / 16
Improving our function (Pretend type-checking)
In mathematics we would define a mapping
triangle area : Z⇥ Z⇥ Z ! R.
In Python we can write:
1 >>> def triangle_area(a:int, b:int, c:int) -> float:
2 ... s = a+b+c
3 ... s = s/2
4 ... return (s*(s-a)*(s-b)*(s-c))**0.5
5 though no actual type-checking is performed automatically
6 >>> triangle_area(1.1, 2.2, 3.3)
7 1.194989539703172
11 / 16
Improving our function (Pretend type-checking)
In mathematics we would define a mapping
triangle area : Z⇥ Z⇥ Z ! R.In Python we can write:
1 >>> def triangle_area(a:int, b:int, c:int) -> float:
2 ... s = a+b+c
3 ... s = s/2
4 ... return (s*(s-a)*(s-b)*(s-c))**0.5
5 though no actual type-checking is performed automatically
6 >>> triangle_area(1.1, 2.2, 3.3)
7 1.194989539703172
11 / 16
Improving our function (Pretend type-checking)
In mathematics we would define a mapping
triangle area : Z⇥ Z⇥ Z ! R.In Python we can write:
1 >>> def triangle_area(a:int, b:int, c:int) -> float:
2 ... s = a+b+c
3 ... s = s/2
4 ... return (s*(s-a)*(s-b)*(s-c))**0.5
5 though no actual type-checking is performed automatically
6 >>>
triangle_area(1.1, 2.2, 3.3)
7 1.194989539703172
11 / 16
Improving our function (Pretend type-checking)
In mathematics we would define a mapping
triangle area : Z⇥ Z⇥ Z ! R.In Python we can write:
1 >>> def triangle_area(a:int, b:int, c:int) -> float:
2 ... s = a+b+c
3 ... s = s/2
4 ... return (s*(s-a)*(s-b)*(s-c))**0.5
5 though no actual type-checking is performed automatically
6 >>> triangle_area(1.1, 2.2, 3.3)
7 1.194989539703172
11 / 16
Improving our function (Doc Strings)
1 >>> def triangle_area(a:int, b:int, c:int) -> float:
2 ... """
3 ... Computes the area of a triangle given its sides.
4 ... """
5 ... s = a+b+c
6 ... s = s/2
7 ... return (s*(s-a)*(s-b)*(s-c))**0.5
12 / 16
Definition 2 (Python Function)
1 >>> def function_name(arg0:type, arg1:type, ... ) -> type:
2 ... """
3 ... Short description of function for documentation.
4 ... Be concise and precise.
5 ... """
6 ......
7 ... function body
8 ......
9 ... return
13 / 16
Task 2
1. Write functions
cel_to_fahr and fahr_to_cel
which convert between Celsius to Fahrenheit.
2. Test that
cel_to_fahr(fahr_to_cel(a)) == a
for various values of a.
14 / 16
Python provides a max and min function that, given a sequence
of numbers, returns the largest or smallest number:
1 >>> min(1,2,3)
2 1
3 >>> max(1,2,3)
4 3
Task 3
Write a function second_largest which takes three inputs and
returns the second largest number.
15 / 16
Next Lecture
1. Logic crash course.
16 / 16