Interactive Computing

Size: px
Start display at page:

Download "Interactive Computing"

Transcription

1 Interactive Computing 1 Input/Output and Complex Arithmetic interactive Python scripts complex arithmetic 2 Python Coding Style and pylint coding style static code checking with pylint 3 Programming with Sound sampling sine functions numpy arrays in Python PyAudio: PortAudio v19 Python Bindings MCS 507 Lecture 3 Mathematical, Statistical and Scientific Software Jan Verschelde, 30 August 2013 Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

2 Interactive Computing 1 Input/Output and Complex Arithmetic interactive Python scripts complex arithmetic 2 Python Coding Style and pylint coding style static code checking with pylint 3 Programming with Sound sampling sine functions numpy arrays in Python PyAudio: PortAudio v19 Python Bindings Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

3 running a script We run the script hellothere.py at the command line prompt $: $ python hellothere.py Welcome to our interactive Python script! who is there? me How are you, me? type some number : 3.4 -> your number 3.4 : <type float > $ Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

4 input statements Two ways to enter data: 1 raw_input() returns a string, example: NAME = raw_input( who is there? ) After displaying who is there?, the characters typed in by the user are returned in a string are assigned to the variable NAME. 2 input() interprets the input of the user, e.g.: NUMB = input( type some number : ) After displaying type a number : and taking the user input, this input is interpreted and, if successful, is then assigned to the variable NUMB. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

5 parsing input strings The statement x = input( type some number : ) is equivalent to s = raw_input( type some number : ) x = eval(s) Test in an interactive Python session: >>> s = 1.2 >>> x = eval(s) >>> type(x) <type float > >>> x 1.2 Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

6 output statements The basic output is print some string. Some examples: print How are you, + NAME +? The + signs are for string concatenation. First the strings How are you,, the string in NAME, and? are added to one string before printing. print -> your number, NUMB, :, NUMBTYPE What is printed is separated by commas. The type of NUMB is not restricted to string. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

7 formatted output Often we want to format the output of floats: limit the number of decimal places >>> import math >>> print %.5f % math.pi display in scientific format >>> print %.4e % (100*math.pi) e+02 Observe that % is used twice differently: The % inside the string defines the format. The % after the format string is an operator. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

8 hellothere.py # L-3 MCS 507 Fri 30 Aug 2013 : hellothere.py """ Execute this script by typing at the command prompt $: python hellothere.py """ print Welcome to our interactive Python script! NAME = raw_input( who is there? ) print How are you, + NAME +? NUMB = input( type some number : ) NUMBTYPE = type(numb) print -> your number, NUMB, :, NUMBTYPE To explicitly check on the type, use isinstance. For example, in an interactive Python session: >>> x = 4.3 >>> isinstance(x,float) True Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

9 an alternative way to run a script #!/Library/Frameworks/Python.framework/Versions/2.7/bin/python # L-3 MCS 507 Fri 30 Aug 2013 : hellothere2.py """ Execute this script by typing at the command prompt $: hellothere2.py """ print Welcome to our interactive Python script! NAME = raw_input( who is there? ) print How are you, + NAME +? NUMB = input( type some number : ) NUMBTYPE = type(numb) print -> your number, NUMB, :, NUMBTYPE At the command prompt $, just type $./hellothere2.py Note that one must have permissions to execute the file, if necessary do chmod +x hellothere2.py (on Unix-like systems). Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

10 Interactive Computing 1 Input/Output and Complex Arithmetic interactive Python scripts complex arithmetic 2 Python Coding Style and pylint coding style static code checking with pylint 3 Programming with Sound sampling sine functions numpy arrays in Python PyAudio: PortAudio v19 Python Bindings Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

11 math and cmath The operations in math are restricted to floats: >>> import math >>> math.sqrt(-1) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: math domain error The c in cmath stands for complex: >>> import cmath >>> cmath.sqrt(-1) 1j The imaginary unit 1 is displayed as 1j. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

12 defining complex numbers The type complex is a builtin Python type: >>> z = complex(2,3) >>> type(z) <type complex > >>> z.real 2.0 >>> z.imag 3.0 >>> type(z.real) <type float > >>> z.conjugate() (2-3j) Python is object oriented: z.conjugate() is the application of the method conjugate to the object z. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

13 polar representation Session continued, with z = 2 + 3j: >>> import cmath >>> cmath.polar(z) ( , ) On returns is a tuple with absolute value and angle: >>> abs(z) >>> cmath.phase(z) From polar to rectangular representation: >>> (r,t) = cmath.polar(z) >>> r*cmath.exp(t*complex(0,1)) ( j) Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

14 unified treatment with numpy >>> import cmath >>> cmath.sqrt(2) ( j) We want a floating-point approximation for 2. >>> import numpy.lib >>> numpy.lib.scimath.sqrt(2) >>> type(_) <type numpy.float64 > >>> numpy.lib.scimath.sqrt(-2) j >>> type(_) <type numpy.complex128 > Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

15 Interactive Computing 1 Input/Output and Complex Arithmetic interactive Python scripts complex arithmetic 2 Python Coding Style and pylint coding style static code checking with pylint 3 Programming with Sound sampling sine functions numpy arrays in Python PyAudio: PortAudio v19 Python Bindings Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

16 Python coding style PEP 8 Style Guide for Python PEP = Python Enhancement Proposal available at This document gives coding conventions for Python code. The guidelines are intended to improve readability of the code. For example in Names to Avoid: Never use the characters l (lowercase letter el), O (uppercase letter oh), or I (uppercase letter eye) as single character variable names. In some fonts, these characters are indistinguishable from the numerals one and zero. When tempted to use l, use L instead. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

17 Interactive Computing 1 Input/Output and Complex Arithmetic interactive Python scripts complex arithmetic 2 Python Coding Style and pylint coding style static code checking with pylint 3 Programming with Sound sampling sine functions numpy arrays in Python PyAudio: PortAudio v19 Python Bindings Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

18 a Python code static checker pylint is a static checker of Python code. Copied from Pylint is a Python source code analyzer which looks for programming errors, helps enforcing a coding standard and sniffs for some code smells (as defined in Martin Fowler s Refactoring book). Pylint has many rules enabled by default, way too much to silence them all on a minimally sized program. It s highly configurable and handle pragmas to control it from within your code. Additionally, it is possible to write plugins to add your own checks. It s a free software distributed under the GNU Public Licence. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

19 running pylint $ pylint hellothere.py No config file found, using default configuration ************* Module hellothere W: 11, 7: Used builtin function input (bad-builtin) Report ====== 7 statements analysed.... output omitted... Global evaluation Your code has been rated at 8.57/10 (previous run: 8.57/10, Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

20 Interactive Computing 1 Input/Output and Complex Arithmetic interactive Python scripts complex arithmetic 2 Python Coding Style and pylint coding style static code checking with pylint 3 Programming with Sound sampling sine functions numpy arrays in Python PyAudio: PortAudio v19 Python Bindings Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

21 modeling sound A sound is a wave describe by sine function s(t) = A sin(2πf t), t is time in seconds, where the amplitude A is the strength of the sound and f is the frequency expressed in Hertz. An f Hz tone lasting for m seconds with sample rate r is defined by the sequence ( s n = A sin 2πf n ), n = 0, 1,..., m r. r For CD quality, the rate r is samples per second. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

22 playing sounds Sounds on a computer are stored in a WAV file. The package scitools available from the web site of the text book has a module sound.py. The script playsounds.py contains f = raw_input( give file name : ) print playing, f,... import scitools.sound scitools.sound.play(f) We run at the command prompt $ as $ python playsounds.py On MacOS X, scitools.sound.play(f) is equivalent to typing open f at the command line prompt. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

23 playing a tone We create a sound at 440 Hz for 6 seconds: import numpy, scitools.sound ATONE = scitools.sound.note(440, 6) calling note() of the module sound in the package scitools; note() returns a numpy array of floats and we must convert this array to two bytes integers: AMPLITUDE = 2**15-1 ATONE = AMPLITUDE*ATONE ATONE = ATONE.astype(numpy.int16) scitools.sound.write(atone, atone.wav ) scitools.sound.play( atone.wav ) Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

24 Interactive Computing 1 Input/Output and Complex Arithmetic interactive Python scripts complex arithmetic 2 Python Coding Style and pylint coding style static code checking with pylint 3 Programming with Sound sampling sine functions numpy arrays in Python PyAudio: PortAudio v19 Python Bindings Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

25 arrays in Python Python has no array type, but there is numpy, an abbreviation for the package Numerical Python. >>> import numpy as np >>> t = np.linspace(0,2*np.pi,5) >>> t array([ 0., , , , ]) linspace(a,b,n) creates an array of n equidistant points in the interval [a,b]. We sample at those points: >>> s = np.sin(2*np.pi*t) The array s contains the values of sin(2πt) at the points in the array of 5 equidistant points in t. No loop is needed! Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

26 vectorization Difference between math.sin() and numpy.sin()? numpy.sin() applies to numbers, arrays + is efficient >>> import numpy >>> x = numpy.linspace(0,1,100) >>> y = numpy.sin(x) the y = numpy.sin(x) is equivalent to >>> y = numpy.zeros(len(x)) >>> for i in range(0,len(x)):... y[i] = math.sin(x[i])... but the loop is not efficient. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

27 computing a sound Computing r = samples of a f = 440 Hz sound lasting for m = 6 seconds of amplitude A = 1 via ( s n = A sin 2πf n ), n = 0, 1,..., m r. r A couple of lines at the Python prompt: >>> import numpy as np >>> r = 44100; f = 440; m = 6; A = 1 >>> t = np.linspace(0,m,m*r) >>> s = A*np.sin(2*np.pi*f*t) Values for r, f, m, and A are the parameters for the note() in the sound module of the package scitools. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

28 creating an echo With a sound defined in the array s, we compute an echo: with a delay of one second, play a sound of half the original strength, for half of the original length. Using r as the sampling rate: delay = np.zeros(r) echo = 0.5*s[0:len(s)/2] sound = np.concatenate((s,delay,echo)) Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

29 the script compute_sound.py # L-3 MCS 507 Fri 30 Aug 2013 : compute_sound.py """ This script creates a.wav file with a sound at a prescribed frequency, using numpy and scitools. """ import numpy as np RATE = FREQ = 440 RANGE = 6 TIME = np.linspace(0, RANGE, RANGE*RATE) SINE = np.sin(2*np.pi*freq*time) DELAY = np.zeros(rate) ECHO = 0.5*SINE[0:len(SINE)/2] SOUND = np.concatenate((sine, DELAY, ECHO)) AMPLITUDE = 2**15-1 SOUND = AMPLITUDE*SOUND SOUND = SOUND.astype(np.int16) import scitools.sound scitools.sound.write(sound, atone.wav ) scitools.sound.play( atone.wav ) Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

30 Interactive Computing 1 Input/Output and Complex Arithmetic interactive Python scripts complex arithmetic 2 Python Coding Style and pylint coding style static code checking with pylint 3 Programming with Sound sampling sine functions numpy arrays in Python PyAudio: PortAudio v19 Python Bindings Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

31 PyAudio With PyAudio, you can use Python to play and record audio on a variety of platforms. Written by Hubert Phan, available at Download the scripts record.py and play.py: python record.py records a few sounds and saves to output.wav. python play.py output.wav plays the recorded sounds in output.wav. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

32 Summary + Exercises We covered chapter 1 of the text book. Exercises: (1 is exercise 1.13 how to cook the perfect egg) 1 The time t in seconds it takes for the center of the yolk to reach the temperature T y in Celsius is given by the formula t = M2/3 cρ 1/3 [ Kπ 2 ln 0.76 T ] o T w (4π/3) 2/3 T y T w where M is the mass, ρ the density, c is the specific heat capacity, and K is the thermal conductivity of the egg. Typical values for a large egg are M = 67 g, ρ = g cm 3, c = 3.7 J g 1 K 1, and K = W cm 1 K 1. The original temperature in Celsius is T o and T w = 100 C is the temperature of the boiling water. Write a script egg.py using the formula to compute the time t it takes for T y = 70 C. Prompt the user for the value of T o, for example: T o = 4 C (fridge), T o = 20 C (room temperature). Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

33 more exercises b 2 Consider the quadratic formula x = b± 2 4ac 2a for the roots of ax 2 + bx + c. Write an interactive Python script that prompts the user for a, b, c and prints the roots. Use numpy.lib.scimath to compute the roots. 3 Make a sound composed of two frequencies, e.g.: one at 440 Hz and the other at 300 Hz, where the first has twice the amplitude of the other, lasting for ten seconds. The first homework collection is on Monday 9 September, at 9AM. Bring to class your answers to exercise 3 of Lecture 1; exercises 1, 2 of Lecture 2; and exercises 1, 3 of Lecture 3. Along with a paper version, also me your scripts. Scientific Software (MCS 507 L-3) Interactive Computing 30 August / 33

List Comprehensions and Simulations

List Comprehensions and Simulations List Comprehensions and Simulations 1 List Comprehensions examples in the Python shell zipping, filtering, and reducing 2 Monte Carlo Simulations testing the normal distribution the Mean Time Between Failures

More information

Lists and Loops. defining lists lists as queues and stacks inserting and removing membership and ordering lists

Lists and Loops. defining lists lists as queues and stacks inserting and removing membership and ordering lists Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule MCS

More information

Python Programming Exercises 1

Python Programming Exercises 1 Python Programming Exercises 1 Notes: throughout these exercises >>> preceeds code that should be typed directly into the Python interpreter. To get the most out of these exercises, don t just follow them

More information

Physics 514 Basic Python Intro

Physics 514 Basic Python Intro Physics 514 Basic Python Intro Emanuel Gull September 8, 2014 1 Python Introduction Download and install python. On Linux this will be done with apt-get, evince, portage, yast, or any other package manager.

More information

turning expressions into functions symbolic substitution, series, and lambdify

turning expressions into functions symbolic substitution, series, and lambdify Defining Functions 1 Lambda Functions turning expressions into functions symbolic substitution, series, and lambdify 2 Functions and Modules writing a function definition defining and using modules where

More information

Lists and Loops. browse Python docs and interactive help

Lists and Loops. browse Python docs and interactive help Lists and Loops 1 Help in Python browse Python docs and interactive help 2 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 3 Loops in Python

More information

Defining Functions. turning expressions into functions. writing a function definition defining and using modules

Defining Functions. turning expressions into functions. writing a function definition defining and using modules Defining Functions 1 Lambda Functions turning expressions into functions 2 Functions and Modules writing a function definition defining and using modules 3 Computing Series Developments exploring an example

More information

User Interfaces. getting arguments of the command line a command line interface to store points fitting points with polyfit of numpy

User Interfaces. getting arguments of the command line a command line interface to store points fitting points with polyfit of numpy User Interfaces 1 Command Line Interfaces getting arguments of the command line a command line interface to store points fitting points with polyfit of numpy 2 Encapsulation by Object Oriented Programming

More information

User Interfaces. MCS 507 Lecture 11 Mathematical, Statistical and Scientific Software Jan Verschelde, 16 September Command Line Interfaces

User Interfaces. MCS 507 Lecture 11 Mathematical, Statistical and Scientific Software Jan Verschelde, 16 September Command Line Interfaces User 1 2 MCS 507 Lecture 11 Mathematical, Statistical and Scientific Software Jan Verschelde, 16 September 2011 User 1 2 command line interfaces Many programs run without dialogue with user, as $ executable

More information

The current topic: Python. Announcements. Python. Python

The current topic: Python. Announcements. Python. Python The current topic: Python Announcements! Introduction! reasons for studying languages! language classifications! simple syntax specification Object-oriented programming: Python Types and values Syntax

More information

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Python Lists: Example 1: >>> items=[apple, orange,100,25.5] >>> items[0] 'apple' >>> 3*items[:2] Python Lists: Lists are Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). All the items belonging to a list can be of different data type.

More information

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python ENGG1811 UNSW, CRICOS Provider No: 00098G W4 Computers have changed engineering http://www.noendexport.com/en/contents/48/410.html

More information

Built-in Types of Data

Built-in Types of Data Built-in Types of Data Types A data type is set of values and a set of operations defined on those values Python supports several built-in data types: int (for integers), float (for floating-point numbers),

More information

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial CSI31 Lecture 5 Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial 1 3.1 Numberic Data Types When computers were first developed, they were seen primarily as

More information

Root Finding Methods. sympy and Sage. MCS 507 Lecture 13 Mathematical, Statistical and Scientific Software Jan Verschelde, 21 September 2011

Root Finding Methods. sympy and Sage. MCS 507 Lecture 13 Mathematical, Statistical and Scientific Software Jan Verschelde, 21 September 2011 wrap Root Finding Methods 1 2 wrap MCS 507 Lecture 13 Mathematical, Statistical and Scientific Software Jan Verschelde, 21 September 2011 Root Finding Methods 1 wrap 2 wrap wrap octave-3.4.0:1> p = [1,0,2,-1]

More information

Lecture 1: Hello, MATLAB!

Lecture 1: Hello, MATLAB! Lecture 1: Hello, MATLAB! Math 98, Spring 2018 Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 1 / 21 Syllabus Instructor: Eric Hallman Class Website: https://math.berkeley.edu/~ehallman/98-fa18/ Login:!cmfmath98

More information

ENGR (Socolofsky) Week 02 Python scripts

ENGR (Socolofsky) Week 02 Python scripts ENGR 102-213 (Socolofsky) Week 02 Python scripts Listing for script.py 1 # data_types.py 2 # 3 # Lecture examples of using various Python data types and string formatting 4 # 5 # ENGR 102-213 6 # Scott

More information

Floating-Point Arithmetic

Floating-Point Arithmetic Floating-Point Arithmetic 1 Numerical Analysis a definition sources of error 2 Floating-Point Numbers floating-point representation of a real number machine precision 3 Floating-Point Arithmetic adding

More information

CHAPTER 3: CORE PROGRAMMING ELEMENTS

CHAPTER 3: CORE PROGRAMMING ELEMENTS Variables CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby A variable is a single datum or an accumulation of data attached to a name The datum is (or data are) stored in

More information

Functions with Parameters and Return Values

Functions with Parameters and Return Values CS101, Spring 2015 Functions with Parameters and Return Values Lecture #4 Last week we covered Objects and Types Variables Methods Tuples Roadmap Last week we covered Objects and Types Variables Methods

More information

Variable and Data Type I

Variable and Data Type I Islamic University Of Gaza Faculty of Engineering Computer Engineering Department Lab 2 Variable and Data Type I Eng. Ibraheem Lubbad September 24, 2016 Variable is reserved a location in memory to store

More information

Numerical Integration

Numerical Integration Numerical Integration 1 Functions using Functions functions as arguments of other functions the one-line if-else statement functions returning multiple values 2 Constructing Integration Rules with sympy

More information

Algorithms and Programming I. Lecture#12 Spring 2015

Algorithms and Programming I. Lecture#12 Spring 2015 Algorithms and Programming I Lecture#12 Spring 2015 Think Python How to Think Like a Computer Scientist By :Allen Downey Installing Python Follow the instructions on installing Python and IDLE on your

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Dr Richard Greenaway

Dr Richard Greenaway SCHOOL OF PHYSICS, ASTRONOMY & MATHEMATICS 4PAM1008 MATLAB 2 Basic MATLAB Operation Dr Richard Greenaway 2 Basic MATLAB Operation 2.1 Overview 2.1.1 The Command Line In this Workshop you will learn how

More information

Variable and Data Type 2

Variable and Data Type 2 The Islamic University of Gaza Faculty of Engineering Dept. of Computer Engineering Intro. To Computers (LNGG 1003) Lab 3 Variable and Data Type 2 Eng. Ibraheem Lubbad March 2, 2017 Python Lists: Lists

More information

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

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions Grace Murray Hopper Expressions Eric Roberts CSCI 121 January 30, 2018 Grace Hopper was one of the pioneers of modern computing, working with

More information

Computational Physics

Computational Physics Computational Physics Intro to Python Prof. Paul Eugenio Department of Physics Florida State University Jan 16, 2018 http://comphy.fsu.edu/~eugenio/comphy/ Announcements Read Chapter 2 Python programming

More information

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

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018 Python Input, output and variables Lecture 23 COMPSCI111/111G SS 2018 1 Today s lecture What is Python? Displaying text on screen using print() Variables Numbers and basic arithmetic Getting input from

More information

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming) Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming) Learning any imperative programming language involves mastering a number of common concepts: Variables: declaration/definition

More information

Section Graphs of the Sine and Cosine Functions

Section Graphs of the Sine and Cosine Functions Section 5. - Graphs of the Sine and Cosine Functions In this section, we will graph the basic sine function and the basic cosine function and then graph other sine and cosine functions using transformations.

More information

Problem Set 6 Audio Programming Out of 40 points. All parts due at 8pm on Thursday, November 29, 2012

Problem Set 6 Audio Programming Out of 40 points. All parts due at 8pm on Thursday, November 29, 2012 Problem Set 6 Audio Programming Out of 40 points All parts due at 8pm on Thursday, November 29, 2012 Goals Learn the basics of audio processing in C Learn how to use audio APIs Grading Metrics The code

More information

2 Computation with Floating-Point Numbers

2 Computation with Floating-Point Numbers 2 Computation with Floating-Point Numbers 2.1 Floating-Point Representation The notion of real numbers in mathematics is convenient for hand computations and formula manipulations. However, real numbers

More information

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala Introduction to Python: Data types HORT 59000 Lecture 8 Instructor: Kranthi Varala Why Python? Readability and ease-of-maintenance Python focuses on well-structured easy to read code Easier to understand

More information

The float type and more on variables FEB 6 TH 2012

The float type and more on variables FEB 6 TH 2012 The float type and more on variables FEB 6 TH 2012 The float type Numbers with decimal points are easily represented in binary: 0.56 (in decimal) = 5/10 + 6/100 0.1011 (in binary) = ½+0/4 + 1/8 +1/16 The

More information

callback, iterators, and generators

callback, iterators, and generators callback, iterators, and generators 1 Adding a Callback Function a function for Newton s method a function of the user to process results 2 A Newton Iterator defining a counter class refactoring the Newton

More information

Tuples and Nested Lists

Tuples and Nested Lists stored in hash 1 2 stored in hash 3 and 4 MCS 507 Lecture 6 Mathematical, Statistical and Scientific Software Jan Verschelde, 2 September 2011 and stored in hash 1 2 stored in hash 3 4 stored in hash tuples

More information

Introduction to Python

Introduction to Python Introduction to Python CB2-101 Introduction to Scientific Computing November 11 th, 2014 Emidio Capriotti http://biofold.org/emidio Division of Informatics Department of Pathology Python Python high-level

More information

Downloaded from Chapter 2. Functions

Downloaded from   Chapter 2. Functions Chapter 2 Functions After studying this lesson, students will be able to: Understand and apply the concept of module programming Write functions Identify and invoke appropriate predefined functions Create

More information

EE3TP4: Signals and Systems Lab 1: Introduction to Matlab Tim Davidson Ext Objective. Report. Introduction to Matlab

EE3TP4: Signals and Systems Lab 1: Introduction to Matlab Tim Davidson Ext Objective. Report. Introduction to Matlab EE3TP4: Signals and Systems Lab 1: Introduction to Matlab Tim Davidson Ext. 27352 davidson@mcmaster.ca Objective To help you familiarize yourselves with Matlab as a computation and visualization tool in

More information

Matlab Introduction. Scalar Variables and Arithmetic Operators

Matlab Introduction. Scalar Variables and Arithmetic Operators Matlab Introduction Matlab is both a powerful computational environment and a programming language that easily handles matrix and complex arithmetic. It is a large software package that has many advanced

More information

Computational Physics

Computational Physics Computational Physics Objects : Lists & Arrays Prof. Paul Eugenio Department of Physics Florida State University Jan 24, 2019 http://hadron.physics.fsu.edu/~eugenio/comphy/ Announcements Read chapter 3

More information

Lab 1 Intro to MATLAB and FreeMat

Lab 1 Intro to MATLAB and FreeMat Lab 1 Intro to MATLAB and FreeMat Objectives concepts 1. Variables, vectors, and arrays 2. Plotting data 3. Script files skills 1. Use MATLAB to solve homework problems 2. Plot lab data and mathematical

More information

A Brief Introduction to Python

A Brief Introduction to Python A Brief Introduction to Python Python is an interpreted language, meaning that a program, i.e., the interpreter, is used to execute your commands one after the other. The commands can be entered interactively

More information

GCSE 4353/02 MATHEMATICS (UNITISED SCHEME) UNIT 3: Calculator-Allowed Mathematics HIGHER TIER

GCSE 4353/02 MATHEMATICS (UNITISED SCHEME) UNIT 3: Calculator-Allowed Mathematics HIGHER TIER Surname Centre Number Candidate Number Other Names GCSE 4353/2 MATHEMATICS (UNITISED SCHEME) UNIT 3: Calculator-Allowed Mathematics HIGHER TIER A.M. MONDAY, 17 June 213 1 3 hours 4 ADDITIONAL MATERIALS

More information

Math 98 - Introduction to MATLAB Programming. Fall Lecture 1

Math 98 - Introduction to MATLAB Programming. Fall Lecture 1 Syllabus Instructor: Chris Policastro Class Website: https://math.berkeley.edu/~cpoli/math98/fall2016.html See website for 1 Class Number 2 Oce hours 3 Textbooks 4 Lecture schedule slides programs Syllabus

More information

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

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016 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

More information

CIS 110: Introduction to Computer Programming

CIS 110: Introduction to Computer Programming CIS 110: Introduction to Computer Programming Lecture 3 Express Yourself ( 2.1) 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline 1. Data representation and types 2. Expressions 9/16/2011

More information

HW3: CS 110X C Domain Information. Final Version: 1/29/2014

HW3: CS 110X C Domain Information. Final Version: 1/29/2014 HW3: CS 110X C 2014 Note: This homework (and all remaining homework assignments) is a partner homework and must be completed by each partner pair. When you complete this assignment, you must not share

More information

Ch.2: Loops and lists

Ch.2: Loops and lists Ch.2: Loops and lists Joakim Sundnes 1,2 Hans Petter Langtangen 1,2 Simula Research Laboratory 1 University of Oslo, Dept. of Informatics 2 Aug 29, 2018 Plan for 28 August Short quiz on topics from last

More information

HW DUE Floating point

HW DUE Floating point Numerical and Scientific Computing with Applications David F. Gleich CS 314, Purdue In this class: Understand the need for floating point arithmetic and some alternatives. Understand how the computer represents

More information

Computational Physics

Computational Physics Computational Physics Python Programming Basics Prof. Paul Eugenio Department of Physics Florida State University Jan 17, 2019 http://hadron.physics.fsu.edu/~eugenio/comphy/ Announcements Exercise 0 due

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 Chapter 3 The Department of Computer Science Review of Chapter 2 Stages of creating a program: Analyze the problem Determine specifications

More information

Computational Modelling 102 (Scientific Programming) Tutorials

Computational Modelling 102 (Scientific Programming) Tutorials COMO 102 : Scientific Programming, Tutorials 2003 1 Computational Modelling 102 (Scientific Programming) Tutorials Dr J. D. Enlow Last modified August 18, 2003. Contents Tutorial 1 : Introduction 3 Tutorial

More information

Lecture 1 August 9, 2017

Lecture 1 August 9, 2017 Programming in Haskell S P Suresh http://www.cmi.ac.in/~spsuresh Lecture 1 August 9, 2017 Administrative Mondays and Wednesdays at 9.10 am at Lecture Hall 6 Evaluation: Quizzes, 4 5 programming assignments,

More information

Branching and Enumeration

Branching and Enumeration Branching and Enumeration 1 Booleans and Branching computing logical expressions computing truth tables with Sage if, else, and elif 2 Timing Python Code try-except costs more than if-else 3 Recursive

More information

+ bx + c = 0, you can solve for x by using The Quadratic Formula. x

+ bx + c = 0, you can solve for x by using The Quadratic Formula. x Math 33B Intermediate Algebra Fall 01 Name Study Guide for Exam 4 The exam will be on Friday, November 9 th. You are allowed to use one 3" by 5" index card on the exam as well as a scientific calculator.

More information

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements Welcome to MCS 360 1 About the Course content expectations 2 our first C++ program using g++ input and output streams the namespace std 3 Greatest Common Divisor Euclid s algorithm the while and do-while

More information

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

CPS122 Lecture: From Python to Java last revised January 4, Objectives: Objectives: CPS122 Lecture: From Python to Java last revised January 4, 2017 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

More information

Variable and Data Type I

Variable and Data Type I The Islamic University of Gaza Faculty of Engineering Dept. of Computer Engineering Intro. To Computers (LNGG 1003) Lab 2 Variable and Data Type I Eng. Ibraheem Lubbad February 18, 2017 Variable is reserved

More information

ENCM 339 Fall 2017 Lecture Section 01 Lab 9 for the Week of November 20

ENCM 339 Fall 2017 Lecture Section 01 Lab 9 for the Week of November 20 page 1 of 9 ENCM 339 Fall 2017 Lecture Section 01 Lab 9 for the Week of November 20 Steve Norman Department of Electrical & Computer Engineering University of Calgary November 2017 Lab instructions and

More information

CSC 110 Final Exam. ID checked

CSC 110 Final Exam. ID checked ID checked CSC 110 Final Exam Name: Date: 1. Write a Python program that asks the user for a positive integer n and prints out n evenly spaced values between 0 and 10. The values should be printed with

More information

Running Cython. overview hello world with Cython. experimental setup adding type declarations cdef functions & calling external functions

Running Cython. overview hello world with Cython. experimental setup adding type declarations cdef functions & calling external functions Running Cython 1 Getting Started with Cython overview hello world with Cython 2 Numerical Integration experimental setup adding type declarations cdef functions & calling external functions 3 Using Cython

More information

PHCpack, phcpy, and Sphinx

PHCpack, phcpy, and Sphinx PHCpack, phcpy, and Sphinx 1 the software PHCpack a package for Polynomial Homotopy Continuation polyhedral homotopies the Python interface phcpy 2 Documenting Software with Sphinx Sphinx generates documentation

More information

Matlab as a calculator

Matlab as a calculator Why Matlab? Matlab is an interactive, high-level, user-friendly programming and visualization environment. It allows much faster programs development in comparison with the traditional low-level compiled

More information

Computer Lab 1: Introduction to Python

Computer Lab 1: Introduction to Python Computer Lab 1: Introduction to Python 1 I. Introduction Python is a programming language that is fairly easy to use. We will use Python for a few computer labs, beginning with this 9irst introduction.

More information

Math 2250 MATLAB TUTORIAL Fall 2005

Math 2250 MATLAB TUTORIAL Fall 2005 Math 2250 MATLAB TUTORIAL Fall 2005 Math Computer Lab The Mathematics Computer Lab is located in the T. Benny Rushing Mathematics Center (located underneath the plaza connecting JWB and LCB) room 155C.

More information

Guide to Planning Functions and Applications, Grade 11, University/College Preparation (MCF3M)

Guide to Planning Functions and Applications, Grade 11, University/College Preparation (MCF3M) Guide to Planning Functions and Applications, Grade 11, University/College Preparation (MCF3M) 006 007 Targeted Implementation and Planning Supports for Revised Mathematics This is intended to provide

More information

MATLAB/Octave Tutorial

MATLAB/Octave Tutorial University of Illinois at Urbana-Champaign Department of Electrical and Computer Engineering ECE 298JA Fall 2017 MATLAB/Octave Tutorial 1 Overview The goal of this tutorial is to help you get familiar

More information

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

>>> * *(25**0.16) *10*(25**0.16) #An Interactive Session in the Python Shell. #When you type a statement in the Python Shell, #the statement is executed immediately. If the #the statement is an expression, its value is #displayed. #Lines

More information

Scientific Computing: Lecture 1

Scientific Computing: Lecture 1 Scientific Computing: Lecture 1 Introduction to course, syllabus, software Getting started Enthought Canopy, TextWrangler editor, python environment, ipython, unix shell Data structures in Python Integers,

More information

LING 408/508: Computational Techniques for Linguists. Lecture 5

LING 408/508: Computational Techniques for Linguists. Lecture 5 LING 408/508: Computational Techniques for Linguists Lecture 5 Last Time Installing Ubuntu 18.04 LTS on top of VirtualBox Your Homework 2: did everyone succeed? Ubuntu VirtualBox Host OS: MacOS or Windows

More information

VBScript: Math Functions

VBScript: Math Functions C h a p t e r 3 VBScript: Math Functions In this chapter, you will learn how to use the following VBScript functions to World Class standards: 1. Writing Math Equations in VBScripts 2. Beginning a New

More information

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

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming Lecture Numbers Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Agenda To understand the concept of data types To be familiar with the basic numeric data types in Python To be able

More information

PART 1 PROGRAMMING WITH MATHLAB

PART 1 PROGRAMMING WITH MATHLAB PART 1 PROGRAMMING WITH MATHLAB Presenter: Dr. Zalilah Sharer 2018 School of Chemical and Energy Engineering Universiti Teknologi Malaysia 23 September 2018 Programming with MATHLAB MATLAB Environment

More information

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB.

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB. Introduction to MATLAB 1 General Information Once you initiate the MATLAB software, you will see the MATLAB logo appear and then the MATLAB prompt >>. The prompt >> indicates that MATLAB is awaiting a

More information

Expressions in JavaScript. Jerry Cain CS 106AJ October 2, 2017

Expressions in JavaScript. Jerry Cain CS 106AJ October 2, 2017 Expressions in JavaScript Jerry Cain CS 106AJ October 2, 2017 What is JavaScript? JavaScript was developed at the Netscape Communications Corporation in 1995, reportedly by a single programmer in just

More information

UNIT 1: PROGRAMMING ENVIRONMENT

UNIT 1: PROGRAMMING ENVIRONMENT UNIT 1: PROGRAMMING ENVIRONMENT 1.1 Introduction This unit introduces the programming environment for the Basic Digital Signal Processing course. It gives a brief description of the Visual Basic classes

More information

Outline. general information policies for the final exam

Outline. general information policies for the final exam Outline 1 final exam on Tuesday 5 May 2015, at 8AM, in BSB 337 general information policies for the final exam 2 some example questions strings, lists, dictionaries scope of variables in functions working

More information

CS112 Lecture: Primitive Types, Operators, Strings

CS112 Lecture: Primitive Types, Operators, Strings CS112 Lecture: Primitive Types, Operators, Strings Last revised 1/24/06 Objectives: 1. To explain the fundamental distinction between primitive types and reference types, and to introduce the Java primitive

More information

Floating-point representation

Floating-point representation Lecture 3-4: Floating-point representation and arithmetic Floating-point representation The notion of real numbers in mathematics is convenient for hand computations and formula manipulations. However,

More information

You can take the arccos of both sides to get θ by itself.

You can take the arccos of both sides to get θ by itself. .7 SOLVING TRIG EQUATIONS Example on p. 8 How do you solve cos ½ for? You can tae the arccos of both sides to get by itself. cos - (cos ) cos - ( ½) / However, arccos only gives us an answer between 0

More information

CSCE 120: Learning To Code

CSCE 120: Learning To Code CSCE 120: Learning To Code Manipulating Data I Introduction This module is designed to get you started working with data by understanding and using variables and data types in JavaScript. It will also

More information

Scripting Languages. Python basics

Scripting Languages. Python basics Scripting Languages Python basics Interpreter Session: python Direct conversation with python (>>>) Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright",

More information

Review Questions 26 CHAPTER 1. SCIENTIFIC COMPUTING

Review Questions 26 CHAPTER 1. SCIENTIFIC COMPUTING 26 CHAPTER 1. SCIENTIFIC COMPUTING amples. The IEEE floating-point standard can be found in [131]. A useful tutorial on floating-point arithmetic and the IEEE standard is [97]. Although it is no substitute

More information

2 Computation with Floating-Point Numbers

2 Computation with Floating-Point Numbers 2 Computation with Floating-Point Numbers 2.1 Floating-Point Representation The notion of real numbers in mathematics is convenient for hand computations and formula manipulations. However, real numbers

More information

Coding Styles for Python

Coding Styles for Python Wintersemester 2007/2008 1 The Zen of Python 2 Style Guide for Python Code 3 Whitespace in Expressions and Statements 4 Naming Conventions 5 References The Zen of Python Python 2.4.2 (#2, Sep 30 2005,

More information

Python for Informatics

Python for Informatics Python for Informatics Exploring Information Version 0.0.6 Charles Severance Chapter 3 Conditional execution 3.1 Boolean expressions A boolean expression is an expression that is either true or false.

More information

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas If you have your own PC, download and install a syntax-highlighting text editor and Python

More information

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

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15 1 LECTURE 1: INTRO Introduction to Scientific Python, CME 193 Jan. 9, 2014 web.stanford.edu/~ermartin/teaching/cme193-winter15 Eileen Martin Some slides are from Sven Schmit s Fall 14 slides 2 Course Details

More information

Getting Started with UNIX

Getting Started with UNIX Getting Started with UNIX What is UNIX? Boston University Information Services & Technology Course Number: 4000 Course Instructor: Kenny Burns Operating System Interface between a user and the computer

More information

S206E Lecture 19, 5/24/2016, Python an overview

S206E Lecture 19, 5/24/2016, Python an overview S206E057 Spring 2016 Copyright 2016, Chiu-Shui Chan. All Rights Reserved. Global and local variables: differences between the two Global variable is usually declared at the start of the program, their

More information

Python Scripting for Computational Science

Python Scripting for Computational Science Hans Petter Langtangen Python Scripting for Computational Science Third Edition With 62 Figures 43 Springer Table of Contents 1 Introduction... 1 1.1 Scripting versus Traditional Programming... 1 1.1.1

More information

AMS 27L LAB #1 Winter 2009

AMS 27L LAB #1 Winter 2009 AMS 27L LAB #1 Winter 2009 Introduction to MATLAB Objectives: 1. To introduce the use of the MATLAB software package 2. To learn elementary mathematics in MATLAB Getting Started: Log onto your machine

More information

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

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts COMP519 Web Programming Lecture 17: Python (Part 1) Handouts Ullrich Hustadt Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science University of Liverpool Contents

More information

Python Input, output and variables

Python Input, output and variables Today s lecture Python Input, output and variables Lecture 22 COMPSCI111/111G SS 2016! What is Python?! Displaying text on screen using print()! Variables! Numbers and basic arithmetic! Getting input from

More information

Introduction to Engineering gii

Introduction to Engineering gii 25.108 Introduction to Engineering gii Dr. Jay Weitzen Lecture Notes I: Introduction to Matlab from Gilat Book MATLAB - Lecture # 1 Starting with MATLAB / Chapter 1 Topics Covered: 1. Introduction. 2.

More information

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out COMP1917: Computing 1 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. COMP1917 15s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write down a proposed solution Break

More information

MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic

MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic September 28, 2018 Lecture 1 September 28, 2018 1 / 25 Floating point arithmetic Computers use finite strings of binary digits to represent

More information

processing data with a database

processing data with a database processing data with a database 1 MySQL and MySQLdb MySQL: an open source database running MySQL for database creation MySQLdb: an interface to MySQL for Python 2 CTA Tables in MySQL files in GTFS feed

More information