Fundamentals of Programming. Lecture 1: Introduction + Basic Building Blocks of Programming

Similar documents
SAMS Programming - Section C. Lecture 1: Introduction + Basic Building Blocks of Programming

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Introduction to Python (All the Basic Stuff)

Fundamentals of Programming. Week 1 - Lecture 3: Loops

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Getting Started with Python

ENGR 101 Engineering Design Workshop

Execution order. main()

4. Write the output that would be printed from each of the following code fragments. (8pts) x = 9 y = x / 2 print('y ==', y) +1 4.

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Text Input and Conditionals

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Algorithms and Programming I. Lecture#12 Spring 2015

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

CS1 Lecture 3 Jan. 18, 2019

Fundamentals of Programming (Python) Getting Started with Programming

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CS1 Lecture 3 Jan. 22, 2018

CS 115 Lecture 4. More Python; testing software. Neil Moore

CS558 Programming Languages

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

functions, modules, debugging

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Notes on Chapter 1 Variables and String

Python for Non-programmers

CS558 Programming Languages

CS 1110 Prelim 1 October 4th, 2012

Functions and abstraction. Ruth Anderson UW CSE 160 Winter 2017

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

Chapter 4. Computation. Bjarne Stroustrup.

cs1114 REVIEW of details test closed laptop period

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Introduction to: Computers & Programming: Review prior to 1 st Midterm

CS 31: Intro to Systems C Programming. Kevin Webb Swarthmore College September 13, 2018

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Chapter 4 Computation

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

CPSC 217 Midterm (Python 3 version)

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Introduction to Programming II W4260. Lecture 2

Algorithms in Systems Engineering ISE 172. Lecture 1. Dr. Ted Ralphs

Python Day 3 11/28/16

Testing. UW CSE 160 Winter 2016

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Types, lists & functions

Introduction to computers and Python. Matthieu Choplin

Introduction to programming (LT2111) Lecture 1: Introduction

Unit E Step-by-Step: Programming with Python

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

Control structure: Repetition - Part 1

CIS192: Python Programming

Beyond Blocks: Python Session #1

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Logical Thinking through Computer Programming

Python for Informatics

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

CONTROL AND ENVIRONMENTS 1

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Data Structure and Programming Languages

CSE / ENGR 142 Programming I

COMP 202 Java in one week

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

Data Structure and Programming Languages

How to Think Like a Computer Scientist: Learning with Python 3»

6.096 Introduction to C++

Lecture 9: July 14, How to Think About Debugging

Please write your name and username here legibly: C212/A592 6W2 Summer 2017 Early Evaluation Exam: Fundamental Programming Structures in Java

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Selection the If Statement Try Catch and Validation

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

Chapter 1: Building Blocks of Programming

The Big Python Guide

Our First Programs. Programs. Hello World 10/7/2013

Topic 2: Introduction to Programming

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

CS558 Programming Languages

SPRING COMP 141 MIDTERM 2 PRACTICE PROBLEMS

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

>>> * *(25**0.16) *10*(25**0.16)

Python Unit

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Python for C programmers

Python 1: Intro! Max Dougherty Andrew Schmitt

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

CS 1301 Exam 1 Answers Fall 2009

Transcription:

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 manipulates/processes data (information) Usually Input Device Output We call this process computation. Calculation: manipulation of numbers. (i.e., computation restricted to numbers)

Examples

Computers in early 20th century

Examples: Nature (?) Evolution

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

A more refined definition of computer - Restricted to electronic devices

A more refined definition of computer - Restricted to electronic devices - Universal programmable to do any task.

Computer: 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.

What is a computer program? A set of instructions that tells the computer how to manipulate data (information). Who is a computer programmer? The person who writes the set of instructions.

Example of a program coin Joe (the robot)

Example of a program

Example of a program Move 1 step forward

Example of a program Move 1 step forward Move 1 step forward

Example of a program Move 1 step forward Move 1 step forward Move 1 step forward

Example of a program Move 1 step forward Move 1 step forward Move 1 step forward Move 1 step forward

Example of a program Move 1 step forward Move 1 step forward Move 1 step forward Move 1 step forward Turn right

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

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

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

Example of a program Repeat 4 times: Move 1 step forward Turn right Repeat 2 times: Move 1 step forward Pick up coin

Another example: cooking Melt butter with olive oil. Add garlic. Cook until lightly browned. Stir in green beans. Season with salt and pepper. More appropriate to call this an algorithm. Cook until beans are tender. Sprinkle with parmesan cheese.

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

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

Examples of Programs Operating Systems Windows MacOS Unix Applications Internet Explorer itunes Warcraft Web Sites Facebook Twitter Wikipedia There are thousands (sometimes millions) of lines of code (instructions) that tell the computer exactly what to do and when to do it.

What you will learn in this course: We will lay the foundations of programming. 1. How to think like a computer scientist. 2. Principals of good programming. 3. Programming language: Python

What you will learn in this course: 1. How to think like a computer scientist. Solving problems. - use instructions a machine can understand. - divide the problem into smaller manageable parts. Finding an efficient (preferably most efficient) solution. EXAMPLE Name input Your Program digital phone book Phone number output - 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?

What you will learn in this course: We will lay the foundations of programming. 1. How to think like a computer scientist. 2. Principals of good programming. 3. Programming language: Python

What you will learn in this course: 2. Principals of good programming. Most important properties of a program: - Does your program work correctly? - Is it efficient? But these are not the only important things: - Is your program (code) easy to read? easy to understand? - Can it be reused easily? extended easily? - Is it easy to fix errors (bugs)? - Are there redundancies in the code?

What you will learn in this course: We will lay the foundations of programming. 1. How to think like a computer scientist. 2. Principals of good programming. 3. Programming language: Python

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. - Mix of math and English. - Lots of similarities between different languages, but also important differences.

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:

The destination Term Projects

Keys to success in this course How do you learn programming? By doing! Understand the method: learning by immersion. 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.

Keys to success in this course Most importantly: Have fun!

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

Let s start.

How do you create and run Python programs? 1. Install Python: www.python.org/download version 3.5.x 2. To type your code and run it, you need an IDE: a. Install and use IEP (now called Pyzo). or b. Install Sublime or Komodo Edit or some other program.

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

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?)

Calculation as computation We can express calculation as a math function: input(s) f output f(x) =x 2 x f x 2 f(2) + f(5) evaluates to 29

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

Calculation as computation We can express calculation as a math function: input(s) f output f(n) =n thprimenumber n f f(n) Often, there will be no formula for the output.

Calculation as computation We can express calculation as a math function: input(s) f output 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.

Computation using Python We can express computation as a Python function: input(s) f output But now, inputs and output can be any type of data. 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.

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. Functions Programs are structured using functions. Conditional Statements Executes statements if a condition is satisfied. Loops Execute a block of code multiple times.

Basic Building Blocks Statements print( Hello World ) In Python3, this is technically a function. Hello World print(911) 911 print(1, 2, 3) 1 2 3 print(3.14, is not an integer ) 3.14 is not an integer.

Basic Building Blocks Assignment Statements and Variables variable-name = value x = 5 y = Hello World print(x) print(y) x = 3.14 y = x x = 0 print(y) In an assignment statement: 1. Evaluate RHS. 2. Assign the value to the variable.

Basic Building Blocks Data/value types x = 5 y = Hello World print(x) print(y) integer string x = 3.14 y = x x = 0 print(y) float

Data Types Python name Description Values int (integer) integer values 2 63 to 2 63 1 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 NoneType absence of value None...

Basic Building Blocks Operators x = 3 + 5 print( Hello + World ) print(1.5 + 1.5) x = 2 * x + 2**3 print(x > 25) print((x < 25) and (x >= 0)) x = Hi! * 2 x stores 8 Hello World 3.0 x stores 24 False True x stores Hi!Hi! What an operator does depends on the types of data it s acting on. Expression: - a valid combination of data and operators - evaluates to a value Expressions are evaluated first!

Basic Building Blocks Functions def square(x): y = x*x return y function definition print(square(5))

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

Basic Building Blocks Functions def square(x): y = x*x return y parameter print(square(5))

Basic Building Blocks Functions def square(x): y = x*x return y print(square(5)) function call

Basic Building Blocks Functions def square(x): y = x*x return y print(square(5)) argument

def square(x): y = x*x return y Basic Building Blocks Functions def square(x): return x*x def square(x): return x**2 Functions can have multiple inputs def f(x, y): return (square(x) + square(y))/2 print(f(2, 3))

def greetuser(name): print( Hello, name) Basic Building Blocks Functions greetuser( David ) Hello David Does this function return anything? It actually returns None. Same as: def greetuser(name): print( Hello, name) return None print(greetuser( David )) Hello David None

def greeteveryone(): print( Hello everyone! ) Basic Building Blocks Functions Functions don t have to take any input greeteveryone() greeteveryone( David ) Hello everyone! ERROR def ispositive(x): return (x > 0) print(ispositive(-1)) False

Basic Building Blocks def ispositive(x): print( Hello. ) return (x > 0) print( Bye. ) print(ispositive(-1)) Functions Hello. False def celsiustofahrenheit(degrees): return degrees * (9 / 5) + 32 def fahrenheittocelsius(degrees): return (degrees - 32) * (5 / 9)

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)) Basic Building Blocks Functions There are various built-in functions:

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. Functions Programs are structured using functions. Conditional Statements Executes statements if a condition is satisfied. Loops Execute a block of code multiple times.

Basic Building Blocks Conditional Statements def absolutevalue(n): if (n < 0): n = -n return n print(absolutevalue(-5)) print(absolutevalue(3)) 5 3

Basic Building Blocks Conditional Statements def absolutevalue(n): if (n < 0): return -n return n print(absolutevalue(-5)) print(absolutevalue(3)) 5 3

Basic Building Blocks Conditional Statements def degreeconverter(degrees, option): if (option == 1): result = degrees * (9 / 5) + 32 else: result = (degrees - 32) * (5 / 9) return result print(degreeconverter(100, 1))

Basic Building Blocks Loops for i in range(5): print("hello!") Hello! Hello! Hello! Hello! Hello!

Basic Building Blocks Loops def printhello(n): for i in range(n): print("hello!") printhello(7) Hello! Hello! Hello! Hello! Hello! Hello! Hello!

Basic Building Blocks Loops def printhello(n): i = 0 while (i < n): print( Hello! ) i = i + 1 printhello(7) Hello! Hello! Hello! Hello! Hello! Hello! Hello!

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.

Types of Programming Errors (Bugs) 3 types Syntax errors (compile-time errors): The compiler finds problems with syntax e.g. typed Print rather than print Run-time errors: A problem occurs during program execution, and causes the program to terminate abnormally (crash). e.g. division by 0. Logical errors: The program runs, but produces incorrect results. e.g. maybe in your program you used a wrong formula: celsius = (5 / 9) * fahrenheit - 32

One of the most important parts of programming is debugging!