CS1 Lecture 3 Jan. 22, 2018

Similar documents
CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 4 Jan. 24, 2018

CS1 Lecture 4 Jan. 23, 2019

CS1 Lecture 2 Jan. 16, 2019

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

CS1 Lecture 5 Jan. 25, 2019

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

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

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

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

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

Getting Started Values, Expressions, and Statements CS GMU

CS1 Lecture 5 Jan. 26, 2018

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

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

Python for Non-programmers

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

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

Midterms Save the Dates!

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

Algorithms and Programming I. Lecture#12 Spring 2015

Introduction to Python (All the Basic Stuff)

Python for C programmers

Lecture 2: Variables & Assignments

Python 1: Intro! Max Dougherty Andrew Schmitt

CS101: Intro to Computing Summer Week 1

Beyond Blocks: Python Session #1

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:

CS1110 Lab 1 (Jan 27-28, 2015)

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

Variables, expressions and statements

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

61A Lecture 2. Wednesday, September 4, 2013

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

CIS192: Python Programming

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

CS 11 Haskell track: lecture 1

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

Text Input and Conditionals

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

CS1 Lecture 13 Feb. 13, 2019

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

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

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

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

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

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

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

CSCI 121: Anatomy of a Python Script

(Refer Slide Time 3:31)

CS1 Lecture 12 Feb. 11, 2019

ENGR 101 Engineering Design Workshop

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

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

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

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

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

SCHEME AND CALCULATOR 5b

Lecture 3. Input, Output and Data Types

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

CS Summer 2013

Unit E Step-by-Step: Programming with Python

CME 193: Introduction to Scientific Python Lecture 1: Introduction

CIS192 Python Programming. Robert Rand. August 27, 2015

Control Structures 1 / 17

T H E I N T E R A C T I V E S H E L L

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

Types, Expressions, and States

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

(I m not printing out these notes! Take your own.)

CMSC 201 Computer Science I for Majors

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

CSE 115. Introduction to Computer Science I

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

Variables, Constants, and Data Types

CS558 Programming Languages

CS112 Lecture: Primitive Types, Operators, Strings

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Getting started with C++ (Part 2)

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

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

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

CS 320: Concepts of Programming Languages

CSI33 Data Structures

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

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Lecture #23: Conversion and Type Inference

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

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

Python Games. Session 1 By Declan Fox

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

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

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

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

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

\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";

Lecture 5. Defining Functions

Introduction to Python

Transcription:

CS1 Lecture 3 Jan. 22, 2018 Office hours for me and for TAs have been posted, locations will change check class website regularly First homework available, due Mon., 9:00am. Discussion sections tomorrow and Wednesday. 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; in some sections you will be paired with a partner at the beginning. Make sure to complete the research-study survey that was posted on ICON

Course overview Chapter 1 of text Last time 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. But 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 Expressions Variables and assignment statements Python scripts 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.

Python scripts We ve been using Python interpreter in interactive mode. Files of Python code are called scripts. You can excute scripts directly from shell/command window or using IDLE or other IDE. Demo. When running scripts, expression (generally) don t produce output. Need print or other operations to see results.

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 Wed. and Fri 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, read Ch3