welcome to csc 108108/lectures/l01.pdf · active learning active learning is a well-supported style...

110
Welcome to CSC 108 Introduction to Computer Programming Lecture 1A September 13, 2019 1 / 19

Upload: others

Post on 15-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Welcome to CSC 108

Introduction to Computer Programming

Lecture 1A

September 13, 2019

1 / 19

Page 2: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 3: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 4: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Email

[email protected]

4 / 19

Page 5: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 6: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 7: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 8: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 9: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 10: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 11: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Email

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

Page 12: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 13: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Academic Integrity

Don’t cheat.

. . . we will catch you.

13 / 19

Page 14: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Good Luck! Have Fun!

14 / 19

Page 15: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Questions?

15 / 19

Page 16: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 17: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Next Lecture

1. Python as a calculator.

2. Variables.

17 / 19

Page 18: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Python as a Calculatorand

Variables.

Introduction to Computer Programming

Lecture 1B

September 9, 2019

1 / 1

Page 19: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Using Python as a calculator.

Standard Arithmetic

+ - * /

Other operations

// ** %

2 / 1

Page 20: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 21: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 22: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 23: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 24: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 25: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 26: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 27: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 28: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 29: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 30: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 31: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 32: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 33: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 34: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 35: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 36: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 37: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 38: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 39: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 40: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 41: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 42: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 43: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 44: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 45: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 46: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 47: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 48: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 49: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 50: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 51: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 52: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 53: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 54: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 55: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 56: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Example 1

When x = 17 and y = 3 we have

17÷ 3 is 5 remainder 2

and notice

17 = 5 · 3 + 2.

7 / 1

Page 57: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

1 >>>

17 // 3

2 5

3

4 >>> 17 % 3

5 2

6

7 >>> 3 * (17 // 3) + (17 % 2)

8 17

8 / 1

Page 58: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

1 >>> 17 // 3

2 5

3

4 >>> 17 % 3

5 2

6

7 >>> 3 * (17 // 3) + (17 % 2)

8 17

8 / 1

Page 59: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

1 >>> 17 // 3

2 5

3

4 >>>

17 % 3

5 2

6

7 >>> 3 * (17 // 3) + (17 % 2)

8 17

8 / 1

Page 60: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

1 >>> 17 // 3

2 5

3

4 >>> 17 % 3

5 2

6

7 >>> 3 * (17 // 3) + (17 % 2)

8 17

8 / 1

Page 61: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

1 >>> 17 // 3

2 5

3

4 >>> 17 % 3

5 2

6

7 >>>

3 * (17 // 3) + (17 % 2)

8 17

8 / 1

Page 62: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

1 >>> 17 // 3

2 5

3

4 >>> 17 % 3

5 2

6

7 >>> 3 * (17 // 3) + (17 % 2)

8 17

8 / 1

Page 63: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

1 >>> 17 // 3

2 5

3

4 >>> 17 % 3

5 2

6

7 >>> 3 * (17 // 3) + (17 % 2)

8 17

8 / 1

Page 64: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 65: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 66: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 67: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 68: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 69: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 70: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 71: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 72: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 73: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 74: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 75: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 76: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 77: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 78: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 79: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 80: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 81: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 82: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 83: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Task 6

What is y after entering the following

1 >>> x = 2

2 >>> y = 4

3 >>> x = 1

Answer

y = 4.

17 / 1

Page 84: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Task 6

What is y after entering the following

1 >>> x = 2

2 >>> y = 4

3 >>> x = 1

Answer

y = 4.

17 / 1

Page 85: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 86: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 87: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 88: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 89: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Next Lecture

1. Basic functions.

20 / 1

Page 90: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

In-Built Functions

Introduction to Computer Programming

Lecture 1C

September 11, 2019

1 / 16

Page 91: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 92: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 93: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 94: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 95: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 96: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 97: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 98: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 99: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 100: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 101: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 102: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 103: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 104: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 105: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 106: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 107: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 108: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 109: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

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

Page 110: Welcome to CSC 108108/lectures/L01.pdf · Active Learning Active Learning is a well-supported style of teaching where material is delivered outside the classroom via online resources

Next Lecture

1. Logic crash course.

16 / 16