CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

Similar documents
CS 102 Lab 3 Fall 2012

Operators. Lecture 3 COP 3014 Spring January 16, 2018

1/11/2010 Topic 2: Introduction to Programming 1 1

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

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

LECTURE 3 C++ Basics Part 2

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

ENGR 102 Engineering Lab I - Computation

Topic 2: Introduction to Programming

cs1114 REVIEW of details test closed laptop period

Lecture 3. Functions & Modules

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

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

ECS Baruch Lab 5 Spring 2019 Name NetID (login, like , not your SUID)

Lecture 1. Types, Expressions, & Variables

Downloaded from Chapter 2. Functions

Lecture 2: Python Arithmetic

Module 1: Types and Expressions

Data Handing in Python

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

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

ENGR 101 Engineering Design Workshop

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Expressions and Variables

ENGR (Socolofsky) Week 02 Python scripts

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Ex: If you use a program to record sales, you will want to remember data:

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

Lecture 3. Functions & Modules

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

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

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Announcements. Lab 1 this week! Homework posted Wednesday (late)

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

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

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

Programming. C++ Basics

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Lecture 3. Input, Output and Data Types

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

REVIEW. The C++ Programming Language. CS 151 Review #2

CEN 414 Java Programming

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

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

Lecture 3: Functions & Modules

Algorithms and Programming I. Lecture#12 Spring 2015

printf( Please enter another number: ); scanf( %d, &num2);

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 2: Variables & Assignments

VARIABLES & ASSIGNMENTS

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

Lecture Numbers. Richard E Sarkis CSC 161: The Art of 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

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

C++ Programming: From Problem Analysis to Program Design, Third Edition

Announcements. Lab 1 this week! Homework posted Thursday -Due next Thursday at 11:55pm

Visual C# Instructor s Manual Table of Contents

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

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

These are reserved words of the C language. For example int, float, if, else, for, while etc.

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Basics of Java Programming

Learning the Language - V

Lecture 3: Functions & Modules (Sections ) CS 1110 Introduction to Computing Using Python

Programming Training. Main Points: - Python Statements - Problems with selections.

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

Reserved Words and Identifiers

Ch.2: Loops and lists

12. Numbers. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Announcements. Homework 0 out! Coming soon: 1. remote access 2. programming instructions 3. lab 1 (graded!) this week

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Introduction to Computer Use II

PYTHON. Values and Variables

CS112 Lecture: Primitive Types, Operators, Strings

CS 112: Intro to Comp Prog

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

Full file at

DATA TYPES AND EXPRESSIONS

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

Expressions, Statements, Variables, Assignments, Types

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Not-So-Mini-Lecture 6. Modules & Scripts

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Programming Projects: 2.1, 2.3, 2.4, 2.7, 2.8, 2.13

CMPT 125: Lecture 3 Data and Expressions

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Arithmetic and IO. 25 August 2017

Python Input, output and variables

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

CS1 Lecture 3 Jan. 18, 2019

Chapter 2: Overview of C++

Chapter 2 Basic Elements of C++

CSEN 102 Introduction to Computer Science

Chapter 3 : Computer Science. Class XI ( As per CBSE Board) Data Handling. Visit : python.mykvs.in for regular updates

Python Day 3 11/28/16

Transcription:

CSC 120 Computer Science for the Sciences Week 1 Lecture 2 UofT St. George January 11, 2016

Introduction to Python & Foundations of computer Programming Variables, DataTypes, Arithmetic Expressions Functions Control flow File I/O Modules Packages: Numpy. CSC120 Winter 2016 2

Arithmetic Expressions Q: In which order does Python evaluate arithmetic (and other) operators? CSC120 Winter 2016 3

Operator Precedence in Python CSC120 Winter 2016 4

Arithmetic Operators -1 With 8 % 3 we get the remainder from long division How do we get the quotient? CSC120 Winter 2016 5

Arithmetic Operators -2 What happens if we try to evaluate expressions that would not work (i.e., are undefined)? CSC120 Winter 2016 6

Division by 0 CSC120 Winter 2016 7

Mathematical Functions & The Math module Not all mathematical functions (e.g., sin and cos) are built-in. to use them, we need to import them from the math module using import math CSC120 Winter 2016 8

Try it at home help(math) help(math.sin) math.sin(90) math.pi math.sin(math.pi / 2) math.degrees(math.pi) math.radians(90) math.cos( math.radians(180) ) math.fabs(-2) math.log(10) math.e math.log(math.e) math.log(10, 10) CSC120 Winter 2016 9

Side Note: floating point error Consider the expression: math.sin(math.pi) What would we expect this to evaluate to? What does it actually evaluate to? Rounding error: e.g., solving a question with a different number of decimal points than a friend, and getting a slightly different result. Cause: numbers are stored in the memory of computers in base 2 (binary), and sometimes the conversion into base 10 (decimal) adds a numerical error to the number. This gets exacerbated when the number is used in calculations further on. CSC120 Winter 2016 10

Python Types Every value has a type in Python type(4.4) <class 'float'> type(4) <class 'int'> type(4.0) <class 'float'> type("4") <class 'str'> type(true) <class 'bool'> type(4 > 6)<class 'bool'> CSC120 Winter 2016 11

Python Casting There are also functions that take a value of one type and convert it to another. This is called casting: int(4.3) int(4.4) float(4) int("8") str(8.5) int(true) bool(1) bool(3) CSC120 Winter 2016 12

Variables A variable is a name that refers to a value. Variable assignment: creates new variables and also gives it a value to refer to. Form of an assignment statement variable = expression How it is executed: 1. Evaluate the expression on the right-hand side. 2. Associate the result with the variable on the left-hand side. CSC120 Winter 2016 13

Differences between Python variables and math variables Python variables look like math variables. This could be Python or math: p = 5 q = p * 7 There are important differences! In mathematics, equations are descriptions that are simultaneously true. In Python, assignment statements look like equations but specify a sequence of steps CSC120 Winter 2016 14

Changeability In math, the following are inconsistent: p = 5 q = 7 p = q + 10 p cannot be both 5 and 17! In Python, and other programming languages, it makes perfect sense. P starts out referring to 5, then changes to refer to 17 You can change a variable's value as many times as you want, and that may change its type too. CSC120 Winter 2016 15

Equality vs Assignment In math, p = q + 10 states a fact about the value of p and that of q + 10 that they are equal In Python, p = q+10 means something completely different!!! This is reasonable, and common, in programming x = x + 1 (makes no sense in math!) We say that x is assigned x + 1 or x gets the value of x + 1 Programming languages usually have different symbols for assignment and equality. in Python, the symbol for CSC120 equality Winter 2016is == 16

Cannot tie two variables Q: What does this do? x = 37 y = x + 2 # y is now 39 x = 20 # Is y now 22? One cannot use assignment to tie the values of two variables together permanently!!! CSC120 Winter 2016 17

Assignment is not symmetric Math Python sum = a + b Legal Legal a + b = sum Legal Illegal CSC120 Winter 2016 18

Naming variables Rules about variable names: - must start with a letter (or underscore): x,my_average - can include letters, digits, and underscores, but nothing else - case matters: age = 11 age # Error! This is not defined. Conventions about choosing names: - choose meaningful names, e.g., if you are adding something up, Sum is better than X. - for names that include multiple words, use underscore: e.g., average_grade CSC120 Winter 2016 19

Expressions vs. Statements Expressions refer/evaluate to a value. Statements are a command to do something. Example Python expressions: (x+3)%4, not True Example Python statements: print(x), x = 3 In Python, you normally write statements that either produce output or change the value of at least one variable. In the shell, you can type \statements" that are really just expressions; the shell prints the values of these expressions CSC120 Winter 2016 20

Problems -1 How can I make a variable named temp that has the value 22.3? How can I then cast temp into an integer? How can I make a variable named Mins_in_day that has the value of how many minutes there are in a day? CSC120 Winter 2016 21

Problems -2 What will x be, if I enter this code? x= float(math.log(2,2)) + int(math.fabs(-6.7))*(8%5) What are the values of p and x after this code? x = 3 p = 2 x = p ** 3 p = x // 5 CSC120 Winter 2016 22