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

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

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

Introduction to Python (All the Basic Stuff)

Notes on Chapter 1 Variables and String

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

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

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

CS1 Lecture 3 Jan. 22, 2018

Fundamentals of Programming. Week 1 - Lecture 3: Loops

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.

ENGR 101 Engineering Design Workshop

CS1 Lecture 3 Jan. 18, 2019

Execution order. main()

Lecture 1 August 9, 2017

Algorithms and Programming I. Lecture#12 Spring 2015

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

CS 1110 Prelim 1 October 4th, 2012

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

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

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

CS558 Programming Languages

CS558 Programming Languages

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

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

Functions and abstraction. Ruth Anderson UW CSE 160 Winter 2017

Logical Thinking through Computer Programming

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

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

CONTROL AND ENVIRONMENTS 1

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

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

Python for Non-programmers

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

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

cs1114 REVIEW of details test closed laptop period

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

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CME 193: Introduction to Scientific Python Lecture 1: Introduction

SPRING COMP 141 MIDTERM 2 PRACTICE PROBLEMS

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

Lecture 3: Recursion; Structural Induction

Introduction to JES and Programming. Installation

Programming for Engineers in Python. Recitation 1

Introduction to Programming II W4260. Lecture 2

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

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

MEIN 50010: Python Introduction

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

CPSC 217 Midterm (Python 3 version)

Lecture 3. Input, Output and Data Types

Algorithms: Why and How. Dr. Ted Ralphs

Programming for Engineers in Python. Autumn

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

Python 1: Intro! Max Dougherty Andrew Schmitt

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

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

Control structure: Repetition - Part 1

Python for C programmers

Announcements Tues., Aug and 2. to call them ( ) ! For next time, read Learning Computing with Robots, chs.

Types, lists & functions

CIS192: Python Programming

ENGR 102 Engineering Lab I - Computation

Python Unit

Functions with Parameters and Return Values

Python Day 3 11/28/16

Genome Sciences 373: Genome Informatics. Quiz section #1 March 29, 2018

Python: common syntax

CSSE 120 Introduction to Software Development Practice for Test 1 paper-and-pencil part Page 1 of 6

Text Input and Conditionals

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

CS 1110, LAB 1: PYTHON EXPRESSIONS.

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

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1]

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

Chapter 2: Introduction to Programming

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Introduction to programming (LT2111) Lecture 1: Introduction

Question 1. CSC 120H1 F Midterm Test Solutions Fall 2017

CPSC 217-T03/T08. Functions Ruting Zhou

CS 1301 Exam 1 Spring 2015

Introduction to Python Programming

python 01 September 16, 2016

Getting Started with Python

Beyond Blocks: Python Session #1

Starting. Read: Chapter 1, Appendix B from textbook.

CMSC 201 Computer Science I for Majors

Math Day 2 Programming: How to make computers do math for you

Python Games. Session 1 By Declan Fox

Intro. Scheme Basics. scm> 5 5. scm>

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY

ASSIGNMENT 4 SOLUTIONS

MAS212 Scientific Computing and Simulation

C ONTROL AND H IGHER O RDER F UNCTIONS

CONTROL AND HIGHER ORDER FUNCTIONS 2

Testing. UW CSE 160 Winter 2016

Artificial Intelligence Lecture 1

Transcription:

SAMS Programming - Section C Lecture 1: Introduction + Basic Building Blocks of Programming Anil Ada aada@cs.cmu.edu July 3, 2017

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

What is a computer? Any device that manipulates/processes data (information) Input Device computer 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 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: a recipe 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 Learn to write programs for:

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

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. e.g. English, Spanish, French, Japanese, etc. Similarly, there are many programming languages. - Mix of math and English. - Lots of similarities between different languages, but also important differences.

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

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. Get to know your TAs. They are awesome.

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

Course Webpage http://www.cs.cmu.edu/~aada/courses/sams17/

Video http://www.youtube.com/watch?v=nkiu9yen5nc

Let s start.

How do you create and run Python programs? 1. Install Python: www.python.org/download (version 3.6.x) 2. To type your code and run it, you need an IDE: e.g. Pyzo, Sublime, IDLE

What we know so far: What is a computer? A programmable device that manipulates data/information 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 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 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 Can express calculation as a math function: input(s) f output f(n) =n thprimenumber n f f(n) Often, there is no formula for the output.

Calculation as computation Can express calculation as a math function: input(s) f output Most important part of calculation/computation: 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 Can express computation as a Python function: input(s) f output Now, inputs and output can be any type of data. Examples of defining math functions in Python: 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.

Computation using Python Your program will be a collection of 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 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( Ty ) Hello Ty Does this function return anything? It actually returns None. Same as: def greetuser(name): print( Hello, name) return None print(greetuser( Ty )) Hello Ty None

def greeteveryone(): print( Hello everyone! ) Basic Building Blocks Functions Functions don t have to take any input greeteveryone() greeteveryone( Ty ) Hello everyone! ERROR 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 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.