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

Similar documents
Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

IT 374 C# and Applications/ IT695 C# Data Structures

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

Python Programming Exercises 1

Full file at

Lecture 2 Tao Wang 1

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

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

Introduction to Computer Programming for Non-Majors

Outline. Data and Operations. Data Types. Integral Types

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

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

ENGR 101 Engineering Design Workshop

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Built-in Types of Data

Topic 2: Introduction to Programming

Introduction to Computer Programming for Non-Majors

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Fundamentals: Expressions and Assignment

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

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

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Algorithms and Programming I. Lecture#12 Spring 2015

Introduction to Computer Programming for Non-Majors

Lecture 3. Input, Output and Data Types

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

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

The C++ Language. Arizona State University 1

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

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

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

Chapter 2 Working with Data Types and Operators

CITS 1401 Problem Solving & Programming

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Python Programming: An Introduction to Computer Science

Fundamentals of Programming Session 4

COMP1730/COMP6730 Programming for Scientists. Functions

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

Getting started with Java

Visual C# Instructor s Manual Table of Contents

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Full file at

COMP Primitive and Class Types. Yi Hong May 14, 2015

Language Reference Manual

Programming for Engineers Introduction to C

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

CSc Introduction to Computing

Programming in Python 3

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

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

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

Variables, expressions and statements

LECTURE 02 INTRODUCTION TO C++

Basic types and definitions. Chapter 3 of Thompson

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Computer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

Fundamental of Programming (C)

ENGR 102 Engineering Lab I - Computation

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

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

The SPL Programming Language Reference Manual

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

Comp 151. More on Arithmetic and intro to Objects

Reserved Words and Identifiers

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Lecture 2: Python Arithmetic

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Introduction to TURING

egrapher Language Reference Manual

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Welcome to Python 3. Some history

Advanced Algorithms and Computational Models (module A)

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Programming Lecture 3

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

Creating a C++ Program

CS177 Python Programming. Recitation 2 - Computing with Numbers

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Comp 151. More on Arithmetic and number types

DaMPL. Language Reference Manual. Henrique Grando

Expressions and Variables

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

GraphQuil Language Reference Manual COMS W4115

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

CHAPTER 3 BASIC INSTRUCTION OF C++

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Python Programming: An Introduction to Computer Science

CHAPTER 3: CORE PROGRAMMING ELEMENTS

CS1 Lecture 3 Jan. 22, 2018

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

Transcription:

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

Lecture outline * Data and data types. * Expressions: computing values. * Variables: remembering values.

What is data? * The number of students currently enrolled in the course. * The words typed into a web search engine. * A time series of total rainfall in Canberra for the month of June since 1971. * An elevation map of Australia. * Most (scientific) applications of computing involve summarising or deriving information from data.

Example: Data analysis * In 2016, enrolment in COMP1730/6730, at its peak, was 364 students. In 2017, the enrolment was 485 students. How big an increase, in percent, is this? * The increase is: 485-364 * as a fraction of the 2016 number: (485-364) / 364 * in percent: ((485-364) / 364) * 100

Expressions * ((485-364) / 364) * 100 is an expression; * it evaluates to 33.24175824175824; * 485, 364, 100 and 33.24175824175824 are all values (and expressions). * In interactive mode, the python interpreter will print the result of evaluating an expression: >>> ((485-364) / 364) * 100 33.24175824175824 (with one exception, which we ll see later).

python syntax (recap) * A python program is a sequence of statements: - import a module; - function definition; - function call expression. - Every function call is an expression. -...and more we ll see later. * Comment: # to end-of-line. * Whitespace: - end-of-line ends statement (except for function definition, which ends at the end of the suite); - indentation defines extent of (function) suite.

python expressions * Expressions are built up of: - constants ( literals ); - variables; - operators; and - function calls. * When an expression is executed, it evaluates to a value (a.k.a. the return value). * Expressions can act as statements (the return value is ignored), but statements cannot act as expressions.

Continuation * end-of-line marks the end of a statement. * Except that, - adding a \ at the end makes the statement continue onto the next line, e.g., (2 ** 0) + (2 ** 1) + (2 ** 2) \ + (2 ** 3) + (2 ** 4) - an expression enclosed in parentheses continues to the closing parenthesis, e.g., math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)

Values and Types

Every value has a type * Value (data) types in python: - Integers (type int) - Floating-point numbers (type float) - Strings (type str) - Truth values (type bool) -...and many more we ll see later. * Types determine what we can do with values (and sometimes what the result is).

* The type function tells us the type of a value: >>> type(2) <class int > >>> type(2 / 3) <class float > >>> type("zero") <class str > >>> type("1") <class str > >>> type(1 < 0) <class bool >

Numeric types * python has two built-in numeric types: Integers and floating-point numbers. * Integers (type int) can represent positive and negative whole numbers. - 0, 1, 2, 1, 17, 4096,... * Values of type int have no inherent size limit. >>> 2 ** (2 ** 2) 16 >>> 2 ** (2 ** (2 ** 2)) 65536 >>> 2 ** (2 ** (2 ** (2 ** 2)))...

* Floating-point numbers (type float) can represent decimal numbers. * Values of type float have limited range and limited precision. - Min/max value: ±1.79 10 308. - Smallest non-zero value: 2.22 10 308. - Smallest value > 1: 1 + 2.22 10 16. (These are typical limits; actual limits depend on the python implementation.) * Type float also has special values ± inf (infinity) and nan (not a number). * More about floating-point numbers and their limitations in a coming lecture.

* Every decimal number is a float: >>> type(1.5-0.5) <class float > >>> type(1.0) <class float > * The result of division is always a float: >>> type(4 / 2) <class float > * floats can be written (and are sometimes printed) in scientific notation : - 2.99e8 means 2.99 10 8. - 6.626e-34 means 6.626 10 34-1e308 means 1 10 308.

Strings * Strings (type str) represent text. * A string literal is enclosed in single or double quote marks: >>> "Hello world" Hello world >>> 4" long 4" long - A string can contain the other type of quotation mark, but not the one used to delimit it. * More about strings in a coming lecture.

Type conversion * Explicit conversions use the type name like a function: >>> int(2.0) >>> float(" -1.05") >>> str(0.75 * 1.75) * Conversion from str to number only works if the string contains (only) a numeric literal. * Conversion from int to float is automatic. - E.g., int times float becomes a float.

Expressions: Operators and Functions

Numeric operators in python +, -, *, / standard math operators ** power (x ** n means x n ) // floor division % remainder * Some operators can be applied also to values of other (non-numeric) types, but with a different meaning (this is called operator overloading ). * We ll see more operators later in the course.

Precedence * There is an order of precedence on operators, that determines how an expression is read: - 2 * 3-1 means (2 * 3) - 1, not 2 * (3-1). - -1 ** 5 means -(1 ** 5), not (-1) ** 5. * Operators with equal precedence associate left: - d/2*pi means (d/2)*pi, not d/(2*pi) *...except exponentiation, which associates right. * Whenever it is not obvious, use parentheses to make it clear.

Math functions * The math module provides standard math functions, such as square root, logarithm, trigonometric functions, etc. >>> import math >>> help(math) # read documentation... >>> math.sqrt(3 ** 2 + 4 ** 2) 5.0 * Almost all math functions take and return values of type float.

Comparison operators <, >, <=, >= ordering (strict and non-strict) == equality (note double = sign)!= not equal * Can compare two values of the same type (for almost any type). * Comparisons return a truth value (type bool), which is either True or False. * Caution: Conversion from any type to type bool happens automatically, but the result may not be what you expect.

Variables

Variables * A variable is a name that is associated with a value in the program. - The python interpreter stores name value associations in a namespace. (More about namespaces later in the course.) * A variable can be an expression: evaluating it returns the associated value. * A name value association is created by the first assignment to the name.

Valid names in python (reminder) * A (function or variable) name in python may contain letters, numbers and underscores ( ), but must begin with a letter or underscore. * Reserved words cannot be used as names. * Names are case sensitive: upper and lower case letters are not the same. - Length Of Rope and length of rope are different names.

Variable assignment * A variable assignment is written var name = expression - Reminder: Equality is written == (two = s). - Assignment is a statement. * When executing an assignment, the interpreter 1. evaluates the right-hand side expression; 2. associates the left-hand side name with the resulting value.

Programming problem * A block resting on an inclined surface will begin to move if the force pulling it down the slope is greater than the normal force times the static friction coefficient (µ s ). (Image from Wikipedia) Say m = 1, g = 9.81, θ = 23 and µ s = 0.62: will the block move? * Yes, if mg sin(θ) > mg cos(θ)µ s.

The print function * print prints text to the console: >>> print("the answer is:", 42) The answer is: 42 - Non-text arguments are converted to type str before printing. - print takes a variable number of arguments, and prints them all followed by a newline. * Print the result, and intermediate steps, when a program is run in script mode.