15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112...

81
Anil Ada [email protected] January 12, 2016 David Kosbie [email protected] 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming

Transcript of 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112...

Page 1: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Anil [email protected]

January 12, 2016

David [email protected]

15-112 Fundamentals of Programming

Lecture 1:Introduction + Basic Building Blocks of Programming

Page 2: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Why are you here?

To learn programming.

AND to be the next Mark Zuckerberg.

Page 3: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Reality

We actually live in a basement

in our parents’ house!

Page 4: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Actually, programming is cool.

Page 5: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

What is programming (coding) ?

What is computer programming ?

Page 6: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

What is a computer?

Any device that manipulates/processes data (information)

Usually

DeviceInput Output

We call this process computation.

Calculation: processing/manipulating numbers.(computation restricted to numbers)

Page 7: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Examples

Page 8: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Examples: Nature (?)

Evolution

Page 9: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

The computational lens

Computational biology

Computational physics

Computational chemistry

Computational neuroscience

Computational finance

Page 10: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A more refined definition of “computer”

- Restricted to electronic devices

Page 11: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A more refined definition of “computer”

- Restricted to electronic devices

- “Universal”programmable to do any task.

Page 12: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A more refined definition of “computer”

- Restricted to electronic devices

- “Universal”programmable to do any task.

Page 13: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

An electronic device that can be programmed tocarry out a set of basic instructions in order toacquire data, process data and produce output.

Page 14: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A set of instructions that tells the computer how to manipulate data (information).

What is a computer program ?

Who is a computer programmer ?

The person who writes the set of instructions.

Page 15: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Example of program: Joe the robot

Joe

coin

Page 16: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Example of program: Joe the robot

Move 1 step forward

Page 17: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Example of program: Joe the robot

Move 1 step forward

Move 1 step forward

Page 18: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Example of program: Joe the robot

Move 1 step forward

Move 1 step forward

Move 1 step forward

Page 19: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Example of program: Joe the robot

Move 1 step forward

Move 1 step forward

Move 1 step forward

Move 1 step forward

Page 20: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Example of program: Joe the robot

Move 1 step forward

Move 1 step forward

Move 1 step forward

Move 1 step forward

Turn right

Page 21: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Example of program: Joe the robot

Move 1 step forward

Move 1 step forward

Move 1 step forward

Move 1 step forward

Turn right

Move 1 step forward

Page 22: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Example of program: Joe the robot

Move 1 step forward

Move 1 step forward

Move 1 step forward

Move 1 step forward

Turn right

Move 1 step forward

Move 1 step forward

Page 23: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Example of program: Joe the robot

Move 1 step forward

Move 1 step forward

Move 1 step forward

Move 1 step forward

Turn right

Move 1 step forward

Move 1 step forward

Pick up coin

Page 24: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Example of program: Joe the robot

Repeat 4 times:

Move 1 step forward

Turn right

Repeat 2 times:

Move 1 step forward

Pick up coin

Page 25: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Another example: cooking

Melt butter with olive oil.

Add garlic.

Cook until lightly browned.

Stir in green beans.

Season with salt and pepper.

Cook until beans are tender.

Sprinkle with parmesan cheese.More appropriate to call

this an algorithm.

Page 26: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

In this course:

This course is about learning to write programs for these:

You will be their master.

Page 27: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Wait a minute!Are you telling me Angry Birds is just a set of instructions?

Page 28: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Examples of Programs

There are thousands (sometimes millions) of lines of code (instructions) that tell the computer exactly

what to do and when to do it.

Operating SystemsWindowsMacOSUnix

Web SitesFacebookTwitter

Wikipedia

ApplicationsInternet Explorer

iTunesWarcraft

Page 29: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

What you will learn in this course:

We will lay the foundations of programming.

2. Principals of good programming.

1. How to think like a computer scientist.

3. Programming language: Python

Page 30: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

What you will learn in this course:

1. How to think like a computer scientist.

Solving problems

Finding an efficient (preferably most efficient) solution.

Page 31: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

What you will learn in this course:

We will lay the foundations of programming.

2. Principals of good programming.

1. How to think like a computer scientist.

3. Programming language: Python

Page 32: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

What you will learn in this course:

2. Principals of good programming.

Is your program (code) easy to read? easy to understand?

Is it easy to fix errors (bugs)?

Can it be reused easily? extended easily?

Are there redundancies in the code?

Does your program work correctly?

Is it efficient?

These are not the only important things:

Page 33: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

What you will learn in this course:

We will lay the foundations of programming.

2. Principals of good programming.

1. How to think like a computer scientist.

3. Programming language: Python

Page 34: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

What you will learn in this course:

3. Programming language: Python

There are many human languages.Can give instructions in English or Spanish or French, etc.

Similarly, there are many programming languages.

There are a lot of similarities, but also important differences.

Page 35: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Programming is fun!

Sky is the limit.

It is a creative process.

When your program does what it is supposed to do: When it doesn’t:

Page 36: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

The destination

Term Projects

Page 37: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Keys to success in this course

How do you learn programming? By doing.

Understand the challenge. Embrace the challenge.

Time management!

Help us help you.

Ask questions in class, in office hours, on Piazza.

You will learn the most from your CAs. Use them.

Understand the method: learning by immersion.

Page 38: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Keys to success in this course

Most importantly:

Have fun!

Page 39: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A typical week

Sun Mon Tue Wed Thu Fri Sat

Lecture:

Cover main content for the week

Page 40: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A typical week

Sun Mon Tue Wed Thu Fri Sat

Recitation:

Go over practice problems.

Page 41: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A typical week

Sun Mon Tue Wed Thu Fri Sat

Lecture:

Go over more problems/examples.

Page 42: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A typical week

Sun Mon Tue Wed Thu Fri Sat

“Recitation”:

Quiz:

Start working on the homework.

a subset of the problems seen on Wed

questions related to previous week’s material.+

Page 43: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A typical week

Sun Mon Tue Wed Thu Fri Sat

Work on the homework.

Page 44: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A typical week

Sun Mon Tue Wed Thu Fri Sat

Homework due at 10pm.

Page 45: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

This week

Sun Mon Tue Wed Thu Fri Sat

Lecture:

We don’t have time to cover everything in lecture.

Go through the notes yourself.

Page 46: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Course Webpage

www.cs.cmu.edu/~112

Page 47: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

What we know so far:

A programmable device that manipulates data/information

Usually

DeviceInput Output

What is a computer?

A set of instructions that tells the computer how to manipulate data/information.

What is a computer program ?

Page 48: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

This Lecture (and Next (and Next...))

How do these instructions look like?(What kind of instructions are allowed?)

How can I use these instructions to write programs?(How do I approach programming, where do I start?)

Page 49: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Basic Building BlocksStatements

Data Types

Variables

Operators

Functions

Conditional Statements

Tells the computer to do something.

Data is divided into different types.

Allows you to store data and access stored data.

Allows you to manipulate data.

Executes statements if a condition is satisfied.

Mini self-contained programs.

Page 50: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Basic Building BlocksStatements

Data Types

Variables

Operators

Functions

Conditional Statements

Tells the computer to do something.

Data is divided into different types.

Allows you to store data and access stored data.

Allows you to manipulate data.

Executes statements if a condition is satisfied.

BASICS OF HANDLING

DATA

Mini self-contained programs.

Page 51: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Basics of handling data

print(“Hello World”)

print(911)

print(3.14, “is not an integer”)

print(1, 2, 3)

Hello World

911

1 2 3

3.14 is not an integer.

Printing values on the screen

Page 52: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Basics of handling data

print(“Hello World”)

print(911)

print(3.14, “is not an integer”)

print(1, 2, 3)

Hello World

911

1 2 3

3.14 is not an integer.

Data/value types

string

integer

float

Page 53: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Basics of handling data

Variables and Assignment Statements

variable-name = value

x = 5y = xx = 0print(y)

x <-- 5y <-- “Hello World”

x = 5y = “Hello World”

print(x)

print(y)

1. Evaluate RHS.2. Assign the value to the variable.

In an assignment statement:

Page 54: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Basics of handling data

Operators

x = 3 + 5

print(“Hello” + “ World”)

print(1.5 + 1.5)

x = 2 * x + 1

x = “Hi!” * 2

Expression: - a valid combination of data and operators- evaluates to a value

Expressions are evaluated first.

x stores 8

Hello World3.0

x stores 17x stores “Hi!Hi!”

Page 55: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Basics of handling data

Data type conversion

x = 251 + “ is a prime number.”

print(x)

Error: cannot add an integer and a string

print(int(“123”) + int(“456”))

x = str(251) + “ is a prime number.”

print(x)

print(“123” + “456”)

251 is a prime number

123456

579

Page 56: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A useful Python program

Help Peter!His cookbook is in Fahrenheit, but his stove is in Celsius.

Last time:

Page 57: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Fahrenheit to Celsius Converter

The formula: C = (F - 32) * (5 / 9)

x = input(“Enter degrees in Fahrenheit: ”)x = float(x)y = (x - 32) * (5/9)print(y)

here x will be a string

Page 58: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Careful: Easy to make errors!

Try to modify the examples:

- Misspell some of the words.- Write in upper case.- Put two statements on one line.- Divide one statement over two lines.- ...

Try to run and see what kind of errors you get.

Page 59: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Types of Programming Errors (Bugs)

3 typesCompile-time errors:

Run-time errors:

Logical errors:

The compiler finds problems with syntax or other basicissues.

A problem occurs during program execution, and causes the program to terminate abnormally (crash).e.g. division by 0.

The program runs, but produces incorrect results.

celsius = (5 / 9) * fahrenheit - 32e.g. maybe in your program you used a wrong formula:

e.g. typed “Print” rather than “print”

Page 60: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A more sophisticated example

print(“1. Convert Celsius to Fahrenheit”)print(“2. Convert Fahrenheit to Celsius”)

option = int(input(“Enter 1 or 2: ”))degrees = float(input(“Enter degrees: ”)) if(option == 1): print(degrees * (9/5) + 32)else: print((degrees - 32) * (5/9))

Page 61: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

A more sophisticated example

print(“1. Convert Celsius to Fahrenheit”)print(“2. Convert Fahrenheit to Celsius”)

option = int(input(“Enter 1 or 2: ”))degrees = float(input(“Enter degrees: ”)) if(option == 1): print(degrees * (9/5) + 32)else: print((degrees - 32) * (5/9))

Evaluates to True or False

So True and False are values.

What is their type?

Boolean

Page 62: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Data Types

NoneType absence of value None

bool (boolean) Boolean values True, False

int (integer) integer values to

long large integer values all integers

float fractional values e.g. 3.14

complex complex values e.g. 1+5j

str (string) text e.g. “Hello World!”

Python name Description Values

�263 263 � 1

...

Page 63: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Basic Building BlocksStatements

Data Types

Variables

Operators

Functions

Conditional Statements

Tells the computer to do something.

Data is divided into different types.

Allows you to store data and access stored data.

Allows you to manipulate data.

Executes statements if a condition is satisfied.

BASICS OF HANDLING

DATA

Mini self-contained programs.

Page 64: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions

Page 65: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in math

A function in math:

input(s) outputf

fx

x

2f(x) = x

2

f(2) + f(5) evaluates to 29

Page 66: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in math

A function in math:

input(s) outputf

f(x, y) =x

2 + y

2

2

fx

2 + y

2

2x, y

f(2, 4) + 5 evaluates to 15

Page 67: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

A function in Python:

input(s) outputf

But now, inputs and output can be any type of data.

Page 68: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

def square(x): y = x*x return y

print(square(5))

function definition

Page 69: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

def square(x): y = x*x return y

print(square(5))

function body (must be indented)

Page 70: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

def square(x): y = x*x return y

print(square(5))

parameter

Page 71: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

def square(x): y = x*x return y

print(square(5)) function call

Page 72: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

def square(x): y = x*x return y

print(square(5)) argument

Page 73: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

def square(x): y = x*x return y

print(square(5))

def square(x): return x*x

print(square(5))

def square(x): return x**2

print(square(5))

def f(x, y): return (square(x) + square(y))/2

print(f(2, 3))

Page 74: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

def greetUser(name): print(“Hello”, name)

greetUser(“David”)

Hello DavidNone

print(greetUser(“David”))

Hello David

Does this function return anything?It actually returns None.

Page 75: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

def greetEveryone(): print(“Hello everyone!”)

greetEveryone() Hello everyone!

def isPositive(x): return (x > 0)

print(isPositive(-1)) False

greetEveryone(“David”) ERROR

Page 76: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

def celsiusToFahrenheit(degrees): return (degrees - 32) * (5 / 9)

def fahrenheitToCelsius(degrees): return degrees * (9 / 5) + 32

def isPositive(x): print(“Hello.”) return (x > 0) print(“Bye.”)

print(isPositive(-1)) Hello.False

Page 77: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

A function in Python:

input(s) outputf

In fact,Python program = a function + other “helper” functions

Never define variables outside of a function!!!

def celsiusToFahrenheit(degrees): return (degrees - 32) * (5 / 9)

def fahrenheitToCelsius(degrees): return degrees * (9 / 5) + 32

Page 78: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

Example problem:Write a function that takes 2 integers as input andreturns the maximum of the ones digit of the numbers.

def max(x, y): if(x >= y): return x else: return y

def onesDigit(x): return x%10 the remainder when x is divided by 10

def largerOnesDigit(x, y): return max(onesDigit(x), onesDigit(y))

helper functions

Page 79: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Functions in Python

A function in Python:

input(s) outputf

In fact,Python program = a function + other “helper” functions

We don’t want to worry about how the input is acquiredor how the output will be used.

We only use print( ) for debugging purposes.We almost never use input( ).

Page 80: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Built-in functionsprint(abs(-5)) print(max(2, 3)) print(min(2, 3)) print(pow(2, 3)) raise to the given power (pow(x,y) == x**y)print(round(2.354, 1)) round with the given number of digits

print(type(5)) print(type(“hello”)) print(type(True))

print(int(2.8))

import mathprint(math.factorial(10))print(math.pi)

Page 81: 15-112 Fundamentals of Programmingaada/courses/15112s16/slides/slides-w1-lec1.pdf · 15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming.

Python fun fact

x = 0.1 + 0.1 + 0.1y = 0.3print(x == y)

x actually stores 0.30000000000000004

prints False

def almostEqual(x, y):epsilon = 10**(-10)return (abs(x - y) < epsilon)