CS1 Lecture 3 Jan. 18, 2019

Similar documents
CS1 Lecture 3 Jan. 22, 2018

CS1 Lecture 4 Jan. 23, 2019

CS1 Lecture 4 Jan. 24, 2018

CS1 Lecture 2 Jan. 16, 2019

CS1 Lecture 5 Jan. 25, 2019

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

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

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

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

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

Getting Started Values, Expressions, and Statements CS GMU

Midterms Save the Dates!

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

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

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

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

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

Python for C programmers

Python 1: Intro! Max Dougherty Andrew Schmitt

Lecture 2: Variables & Assignments

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lecture 1. Types, Expressions, & Variables

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

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

CIS192: Python Programming

(Refer Slide Time 3:31)

Today. Homework 1 / Test 1 Making decisions in Java If statements If/Else ( Either-or ) statements Logical NOT operator

CS1 Lecture 5 Jan. 26, 2018

Basic Syntax - First Program 1

CS101: Intro to Computing Summer Week 1

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

61A Lecture 2. Wednesday, September 4, 2013

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1

CS 11 Haskell track: lecture 1

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CMSC 201 Computer Science I for Majors

Beyond Blocks: Python Session #1

Python for Non-programmers

cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Introduction to Python (All the Basic Stuff)

Boolean Algebra A B A AND B = A*B A B A OR B = A+B

These are notes for the third lecture; if statements and loops.

ENGR 101 Engineering Design Workshop

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Algorithms and Programming I. Lecture#12 Spring 2015

CIS 110: Introduction to Computer Programming

CS1110 Lab 1 (Jan 27-28, 2015)

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

CIS192 Python Programming. Robert Rand. August 27, 2015

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Unit E Step-by-Step: Programming with Python

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

61A Lecture 3. Friday, September 5

CS Introduction to Programming Fall 2016

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

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

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

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

CS 360: Programming Languages Lecture 10: Introduction to Haskell

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

Lecture 1. Course Overview Types & Expressions

Variables, Constants, and Data Types

CSE 115. Introduction to Computer Science I

Variables, expressions and statements

Text Input and Conditionals

CS112 Lecture: Primitive Types, Operators, Strings

Getting started with C++ (Part 2)

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

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

CS Summer 2013

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

CS 320: Concepts of Programming Languages

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

CSI33 Data Structures

Lecture 1. Course Overview, Python Basics

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

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Lecture #23: Conversion and Type Inference

61A Lecture 4. Monday, September 9

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Lecture 3. Input, Output and Data Types

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

SCHEME AND CALCULATOR 5b

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

QUIZ. What is wrong with this code that uses default arguments?

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

Types, Expressions, and States

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Announcements. 1. Forms to return today after class:

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Lecture Set 2: Starting Java

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Transcription:

CS1 Lecture 3 Jan. 18, 2019 Office hours for Prof. Cremer and for TAs have been posted. Locations will change check class website regularly First homework assignment will be available Monday evening, due Monday, Jan. 28, 9am Discussion sections next Tuesday and Wednesday are required - will have points available Discussion section assignments must be submitted at the end of the discussion section. You need to attend to be able to get the points for the discussion section assignment. Be on time so you have time to complete the work

Course overview Chapter 1 of text Last time Starting Python and intro to evaluating simple expressions expressions yield values every value has a type IC Arrest blotter demo program Discussion of graph problems posed at end of first lecture

Chapter 1 of text says it differently: input, output, math, conditional execution, repetition. I/O is important but, to me, not key or interesting at the beginning. math is too vague. Assignment and variables, in the programming sense, don t fit well under everyday use of word math. And while functions can be thought of as math, in programming functions (often called procedures when used slightly differently) play a very important role beyond just "being part of math." They help organize programs into understandable components, etc. (from last time) Ch 1: Programs Key components of programming, independent of particular programming language. Expressions Variables and assignment if-then-else (decision making/branching) Iteration Functions Essentially all programming languages are built around these components. Once you understand how to describe computations using them, you can program. Learn these basics well! Changing programming languages is usually just a matter of looking up details of how to say a particular standard thing in the different language.

(from last time) Ch 1: expressions, arithmetic, types You can use the Python interpreter like a calculator, typing in many different mathematical (and other) expressions and seeing immediate results print( hello ) 3 + 2 2**128 5/2+1 Expressions yield values. Every value has a type. 3 s type in int (for integer) 3.5 s type is float (for floating point number) (What about 3.0? 3.0 s type is float. It is not the exact same thing as 3 in Python but (fortunately) it is equal to 3. More on this later.) hello s type is string You can ask Python for a value s type >>> type(3.5)

Today Chapter 2 More detail on expressions Variables and assignment statements Strings and expressions Plus a very quick look at defining functions (from Ch 3)

Expressions Generally, an expression is a combination of literals (things like numbers, strings, Booleans) and operators (+, -, ) that, when evaluated by Python, yield a value mathematical expressions yield numbers, objects of type int or float) 3 * (200 / 1.5) abs(-4) + 2 logical expressions yield True or False, objects of type bool

Logical expressions Some relational/logical operators (see Ch 5): >, <, ==, and, or, not Example expressions: >>> (1 < 3) and (0 > 2) False >>> abs(-1) == 2 False >>> (5 == (2 + 3)) or True True Note: == is not a statement of equality! It s a question are the two sides equal? True or False question!

Order of operations The textbook has a section on order of operations, so that you can figure out how to calculate the value of something like: >>> 3 + 1 or 3 + 2 ** 2 + -14 / 2.0 == 0??? This has a legal value but I wouldn t be able to tell you without looking things up. I always parenthesize fully to make expression clear. E.g. ((3 * x) + (4.2 * (z ** 1.2))) - y Code should be written so that you and others can readily read and understand it!

Variables and Assignment Statements Expressions yield values and we often want to give names to those values so we can use them in further calculations. A variable is a name associated with a value. The statement >>> x = 10 >>> creates the variable x and associates it with value 10. x = 10 is a statement not an expression. It doesn t have/produce a value. BUT it does have an important effect - it associates x with the value 10 and subsequently x can be used in expressions! >>> x + 3 13

Variables and Assignment Statements In general, you write: >>> var_name = expression where var_name is a legal variable name (see book/python reference) and expression is any expression >>> zxy1213 = 14.3 + (3 * math.sin(math.pi/2)) >>> zxy1213 17.3 And since zxy1213 is a variable, thus a legal expression, we can write: >>> sillyvarname = zxy1213 1.0 >>> sillyvarname 16.3

Variables and Assignment Statements Only a single variable name, by itself, can appear on to the left of an = sign (unlike for ==, the equality question ) >>> x + 3 = 4 X (crashes, yields syntax error.) >>> x + 3 == 4 OK (will return True or False, or give error if x has not been assigned a value)

Variables and Assignment Statements x >>> x = 3 >>> y = 4 * x >>> result = x + y y 12 3 result 15

One of the most important things to know all semester To process an assignment statement x = y * 32.1 ** foo(...) math.sin(bar(...))) /... 1) Evaluate right hand side (ignore left for a moment!) yielding a value (no variable involved in result it s a number or a string or a boolean value or ) 2) Associate variable name on left hand side with resulting value

Variables and Assignment Statements >>> x = 3 >>> y = 4 * x >>> result = x + y Rule (very important to remember): 1) Evaluate right hand side (ignore left for a moment!) yielding a value (no variable involved in result) 2) Associate variable name on left hand side with resulting value x y result 12 3 15 100 >>> x = 100 >>> y? >>>result? y and result are not changed! Don t think of assignments as constraints or lasting algebraic equalities. They make (perhaps temporary) associations between names and values.

Ch2 s section on strings There s a whole chapter (8) on strings but we want to use them much sooner (for one thing, we need them to print nice output!) Use quotes (single, double, even triple!) to delimit: abc == abc But abc is not a legal string. The quotes at the ends are not part of the string. abc123 has 6 characters in it, not 8. Strings can contain quote characters E.g. abc def is a 7 character string containing a, b, c, d, e, f, and a single-quote character Strings in Python are powerful and can get complicated. Can represent full Unicode, chars from many languages,

Ch2 s section on strings Again, we ll talk more about strings later but for now you should at least know you can use + operator on them >>> name = Jim >>> sentence = Hi + name +! >>> sentence >>> Hi Jim! >>> print(sentence) Hi Jim! >>>

Very quick function (def) overview From Ch 3, more next Wed. and Fri But you need basics now since you will implement a function in Tuesday/Wednesday discussion sections A function call, f(a,b,c) is an expression with a value, just like other Python expressions. Like in math, a function receives some input arguments, computes something, and (usually) returns an answer. min(3,4) returns 3 you can use the value a function returns: >>> result = min(3,4) >>> z = result + 1 def enables you to define your own functions

Functions def functionname (list of formal parameters):.. (body of function, can be many lines). Note: indentation matters in Python. Is not optional has meaning. Python is unusual in that way indentation is not meaningful in most programming languages.

Functions def mymin (a,b): if (a < b): return a else: return b ----------------------- >>> mymin(5,7) >>> 5 (think of it like) a, b = 5, 7 if (a < b): return a else: return b 5

Functions def mymin (a,b): if (a < b): return a else: return b ----------------------- >> x, y = 12, 10 >> mymin(x,y) mymin(12,10) a, b = 12, 10 if (a < b): return a else: return b >> 10

Next time More on variables and assignment Ch 3: Functions For next time (and for disc. section), read Ch3