15-112 Fundamentals of Programming Lecture 1: Introduction + Basic - - PowerPoint PPT Presentation

15 112 fundamentals of programming
SMART_READER_LITE
LIVE PREVIEW

15-112 Fundamentals of Programming Lecture 1: Introduction + Basic - - PowerPoint PPT Presentation

15-112 Fundamentals of Programming Lecture 1: Introduction + Basic Building Blocks of Programming Anil Ada aada@cs.cmu.edu May 16, 2016 What is programming (coding) ? What is computer programming ? What is a computer? Any device that


slide-1
SLIDE 1

Anil Ada aada@cs.cmu.edu

May 16, 2016

15-112 Fundamentals of Programming

Lecture 1: Introduction + Basic Building Blocks of Programming

slide-2
SLIDE 2

What is programming (coding) ? What is computer programming ?

slide-3
SLIDE 3

What is a computer?

Any device that manipulates/processes data (information) Usually Device Input Output We call this process computation. Calculation: manipulation of numbers. (i.e., computation restricted to numbers)

slide-4
SLIDE 4

Examples

slide-5
SLIDE 5

“Computers” in early 20th century

slide-6
SLIDE 6

Examples: Nature (?)

Evolution

slide-7
SLIDE 7

The computational lens Computational biology Computational physics Computational chemistry Computational neuroscience Computational finance … Computer Science: The science that studies computation.

slide-8
SLIDE 8

A more refined definition of “computer”

  • Restricted to electronic devices
slide-9
SLIDE 9

A more refined definition of “computer”

  • Restricted to electronic devices
  • “Universal”

programmable to do any task.

slide-10
SLIDE 10

An electronic device that can be programmed to carry out a set of basic instructions in order to acquire data, process data and produce output. Computer:

slide-11
SLIDE 11

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.

slide-12
SLIDE 12

Example of a program

Joe (the robot) coin

slide-13
SLIDE 13

Example of a program

slide-14
SLIDE 14

Example of a program

Move 1 step forward

slide-15
SLIDE 15

Example of a program

Move 1 step forward Move 1 step forward

slide-16
SLIDE 16

Example of a program

Move 1 step forward Move 1 step forward Move 1 step forward

slide-17
SLIDE 17

Example of a program

Move 1 step forward Move 1 step forward Move 1 step forward Move 1 step forward

slide-18
SLIDE 18

Example of a program

Move 1 step forward Move 1 step forward Move 1 step forward Move 1 step forward Turn right

slide-19
SLIDE 19

Example of a program

Move 1 step forward Move 1 step forward Move 1 step forward Move 1 step forward Turn right Move 1 step forward

slide-20
SLIDE 20

Example of a program

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

slide-21
SLIDE 21

Example of a program

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

slide-22
SLIDE 22

Example of a program

Repeat 4 times: Move 1 step forward Turn right Repeat 2 times: Move 1 step forward Pick up coin

slide-23
SLIDE 23

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.

slide-24
SLIDE 24

In this course:

This course is about learning to write programs for: You will be their master.

slide-25
SLIDE 25

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

slide-26
SLIDE 26

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 Systems Windows MacOS Unix Web Sites Facebook Twitter Wikipedia Applications Internet Explorer iTunes Warcraft

slide-27
SLIDE 27

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
slide-28
SLIDE 28

What you will learn in this course:

  • 1. How to think like a computer scientist.

Finding an efficient (preferably most efficient) solution. Solving problems.

  • use instructions a machine can understand.
  • divide the problem into smaller manageable parts.

EXAMPLE Your Program

digital phone book

Name Phone number

input

  • utput
  • How do you solve it using instructions the computer can understand?

(Can’t just say “find phone number”)

  • How do you solve the problem efficiently?
slide-29
SLIDE 29

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
slide-30
SLIDE 30

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?

But these are not the only important things:

  • Does your program work correctly?
  • Is it efficient?

Most important properties of a program:

slide-31
SLIDE 31

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
slide-32
SLIDE 32

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.

  • Lots of similarities between different languages,

but also important differences.

  • Mix of math and English.
slide-33
SLIDE 33

Programming is awesome!

Sky is the limit. Combines technical skill and creativity. When your program does what it is supposed to do: When it doesn’t:

slide-34
SLIDE 34

The destination

Term Projects

slide-35
SLIDE 35

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.

slide-36
SLIDE 36

Keys to success in this course

Most importantly: Have fun!

slide-37
SLIDE 37

Course Webpage

http://www.cs.cmu.edu/~112/m16/

slide-38
SLIDE 38

Let’s start.

slide-39
SLIDE 39

How do you create and run Python programs?

  • 1. Install Python: www.python.org/download

version 3.5.x

  • b. Install Sublime or Komodo Edit
  • r some other program.
  • 2. To type your code and run it, you need an IDE:
  • a. Install and use IEP (now called Pyzo).
  • r
slide-40
SLIDE 40

What we know so far:

A programmable device that manipulates data/information Usually Device Input Output What is a computer? A set of instructions that tells the computer how to manipulate data/information. What is a computer program ?

slide-41
SLIDE 41

This Lecture (and next, 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?)

slide-42
SLIDE 42

Calculation as computation

We can express calculation as a math function: input(s)

  • utput

f f x x2 f(x) = x2 f(2) + f(5) evaluates to 29

slide-43
SLIDE 43

Calculation as computation

input(s)

  • utput

f f(x, y) = x2 + y2 2 f x2 + y2 2 x, y f(2, 4) + 5 evaluates to 15 We can express calculation as a math function:

slide-44
SLIDE 44

Calculation as computation

input(s)

  • utput

f We can express calculation as a math function: f f(n) n f(n) = n’th prime number Often, there will be no formula for the output.

slide-45
SLIDE 45

Calculation as computation

input(s)

  • utput

f We can express calculation as a math function: The most important part of calculation/computation is: specifying how to go from the input to the output.

  • This specification/description is called:

> algorithm, if a human can follow it; > computer program (or code), if a computer can follow it.

slide-46
SLIDE 46

Computation using Python

input(s)

  • utput

f But now, inputs and output can be any type of data. We can express computation as a Python function: Examples:

def f(x): y = x*x return y def f(x, y): z = (x**2 + y**2)/2 return z def nthPrime(n): …

more complicated.

slide-47
SLIDE 47

Basic Building Blocks

Statements Tells the computer to do something. An instruction. Data Types Data is divided into different types. Variables Allows you to store data and access stored data. Operators Allows you to manipulate data. Conditional Statements Executes statements if a condition is satisfied. Functions Programs are structured using functions. Loops Execute a block of code multiple times.

slide-48
SLIDE 48

Basic Building Blocks

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.

Statements

In Python3, this is technically a function.

slide-49
SLIDE 49

Basic Building Blocks

Assignment Statements and Variables variable-name = value

x = 3.14 y = x x = 0 print(y) x = 5 y = “Hello World” print(x) print(y)

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

In an assignment statement:

slide-50
SLIDE 50

Basic Building Blocks

Data/value types

x = 3.14 y = x x = 0 print(y) x = 5 y = “Hello World” print(x) print(y)

string integer float

slide-51
SLIDE 51

Data Types Python name Description Values

...

NoneType absence of value None int (integer) integer values −263 263 − 1 to long large integer values all integers float fractional values e.g. 3.14 str (string) text e.g. “Hello World!” bool (boolean) Boolean values True, False

slide-52
SLIDE 52

Basic Building Blocks

Operators

x = 3 + 5 print(“Hello” + “ World”) print(1.5 + 1.5) x = 2 * x + 2**3 x = “Hi!” * 2 Expression: - a valid combination of data and operators

  • evaluates to a value

Expressions are evaluated first! x stores 8 Hello World 3.0 x stores 24 x stores “Hi!Hi!” What an operator does depends on the types of data it’s acting on. print(x > 25) False print((x < 25) and (x >= 0)) True

slide-53
SLIDE 53

Basic Building Blocks

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

Functions

print(square(5))

slide-54
SLIDE 54

Basic Building Blocks

def square(x): y = x*x return y print(square(5)) function body (must be indented)

Functions

slide-55
SLIDE 55

Basic Building Blocks

def square(x): y = x*x return y print(square(5)) parameter

Functions

slide-56
SLIDE 56

Basic Building Blocks

def square(x): y = x*x return y print(square(5)) function call

Functions

slide-57
SLIDE 57

Basic Building Blocks

def square(x): y = x*x return y print(square(5)) argument

Functions

slide-58
SLIDE 58

Basic Building Blocks

def square(x): y = x*x return y def square(x): return x*x def square(x): return x**2 def f(x, y): return (square(x) + square(y))/2 print(f(2, 3))

Functions Functions can have multiple inputs

slide-59
SLIDE 59

Basic Building Blocks

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

Does this function return anything? It actually returns None. Functions Same as:

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

slide-60
SLIDE 60

Basic Building Blocks

def greetEveryone(): print(“Hello everyone!”) greetEveryone() Hello everyone! def isPositive(x): return (x > 0) print(isPositive(-1)) False greetEveryone(“David”) ERROR

Functions Functions don’t have to take any input

slide-61
SLIDE 61

Basic Building Blocks

def celsiusToFahrenheit(degrees): return degrees * (9 / 5) + 32 def fahrenheitToCelsius(degrees): return (degrees - 32) * (5 / 9) def isPositive(x): print(“Hello.”) return (x > 0) print(“Bye.”) print(isPositive(-1)) Hello. False

Functions

slide-62
SLIDE 62

Basic Building Blocks

print(abs(-5)) print(max(2, 3)) print(min(2, 3)) print(pow(2, 3)) print(round(-3.14)) print(type(5)) print(type(“hello”)) print(type(True)) print(int(2.8))

Functions There are various built-in functions:

slide-63
SLIDE 63

Basic Building Blocks

Statements Tells the computer to do something. An instruction. Data Types Data is divided into different types. Variables Allows you to store data and access stored data. Operators Allows you to manipulate data. Conditional Statements Executes statements if a condition is satisfied. Functions Programs are structured using functions. Loops Execute a block of code multiple times.

slide-64
SLIDE 64

Basic Building Blocks

def absoluteValue(n): if (n < 0): n = -n return n

Conditional Statements

print(absoluteValue(-5)) print(absoluteValue(3)) 5 3

slide-65
SLIDE 65

Basic Building Blocks

def absoluteValue(n): if (n < 0): return -n return n

Conditional Statements

print(absoluteValue(-5)) print(absoluteValue(3)) 5 3

slide-66
SLIDE 66

Basic Building Blocks

def degreeConverter(degrees, option): if (option == 1): result = degrees * (9 / 5) + 32 else: result = (degrees - 32) * (5 / 9) return result print(degreeConverter(100, 1))

Conditional Statements

slide-67
SLIDE 67

Basic Building Blocks

Loops

for i in range(5): print("Hello!") Hello! Hello! Hello! Hello! Hello!

slide-68
SLIDE 68

Basic Building Blocks

Loops

def printHello(n): for i in range(n): print("Hello!") Hello! Hello! Hello! Hello! Hello! Hello! Hello! printHello(7)

slide-69
SLIDE 69

Basic Building Blocks

Loops

def printHello(n): i = 0 while (i < n): print(“Hello!”) i = i + 1 Hello! Hello! Hello! Hello! Hello! Hello! Hello! printHello(7)

slide-70
SLIDE 70

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.

slide-71
SLIDE 71

Types of Programming Errors (Bugs)

3 types Syntax errors (compile-time errors): Run-time errors: Logical errors: The compiler finds problems with syntax 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 - 32

e.g. maybe in your program you used a wrong formula: e.g. typed “Print” rather than “print”

slide-72
SLIDE 72

One of the most important parts of programming is debugging!