Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A def circumference(diameter):

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1.
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
Program Design. Objectives Students should understand the basic steps in the programming process. Students should understand the need for good design.
Numbers. Number data types store numeric values They are immutable data types, which means that changing the value of a number data type results in a.
Physics Rules for using Significant Figures. Rules for Averaging Trials Determine the average of the trials using a calculator Determine the uncertainty.
BIL101, Introduction to Computers and Information Systems Chapter 12 A Portable Scientific Visualization Program: GnuPlot Prepared by Metin Demiralp Istanbul.
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
IWBAT compare and order positive and negative numbers.
Microsoft® Small Basic The Math Object Estimated time to complete this lesson: 1 hour.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
1 Chapter 1 Preliminaries Functions and Their Graphs.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
Computer Science 111 Fundamentals of Programming I Number Systems.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
IMS 3253: Math 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Five Fundamental Math Operations Precedence of Math.
Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.
CSC1015F – Chapter 3, Computing with Numbers Michelle Kuttel
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
STANDARD FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
How to start Visual Studio 2008 or 2010 (command-line program)
1 Math Expressions and Operators. 2 Some C++ Operators Precedence OperatorDescription Higher ( )Function call +Positive - Negative *Multiplication / Division.
Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything.
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of an assignment is an expression. An expression is anything.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Homework Assignment #4 J. H. Wang Dec. 3, 2007.
Significant Figures Part 2 Problem Solving Applications.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Significant Figure Rules RulesExamples The following are always significant Non zero digits Zeros between non zero digits Zero to the right of a non zero.
Circumference Review. Review What is the relationship between a radius and a diameter? What does a circumference measure? What formulas do we use to calculate.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Ch. 10 Numerical Calculations From Valvano’s text Introduction to Embedded Systems.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Week 7 : String and photo processing. Today’s Tasks  Practice list and string  Convert Decimal to any radix base number  Between Binary and Hexadecimal.
ICS 3U Math Class. ActionScript 3 – Math Class Rounding ceil – closest integer >= number (rounds up) floor – closest integer
Review # 2 Lessons Questions Here goes… Questions 1 –5 Are from Lesson # 31 Complementary and Supplementary Angles.
Chapter 14 LISP – Practical 3 Instructor: Haris Shahzad Artificial Intelligence CS-402.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
CSE 110: Programming Language I Afroza Sultana UB 1230.
Simple C Programs.
Introduction to Programming
Some Useful MATLAB Functions
Mathematical Functions
Introduction to Programming
BIL 104E Introduction to Scientific and Engineering Computing
Introduction to Programming
Fundamentals of Programming I Number Systems
Introduction to Programming
Introduction to Programming
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Introduction to Matlab
Elementary Programming (C++)
class 5 retrieving a previous statement in Thonny or IDLE // %
Introduction to Programming
Terminal-Based Programs
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Pretest Lessons # Questions.
Python Creating a calculator.
Presentation transcript:

Useful Python CMSC 120: Visualizing Information

Scope def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A def circumference(diameter): return PI * diameter def main(): PI = 3.14 d = input('Enter a diameter:') print area(d), circumference(d) main()

Global Variables PI = 3.14 def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A def circumference(diameter): return PI * diameter def main(): d = input('Enter a diameter:') print area(d), circumference(d) main()

Parameters def area(diameter, P): radius = diameter / 2.0 A = P * radius ** 2 return A def circumference(diameter, P): return P * diameter def main(): d = input('Enter a diameter:') print area(d, P), circumference(d, P) main()

Our Histogram Problem Histogram (0,0) (offset, offset) Which parts of the program need to access: the window? the offset? Which parts of the program need to access: the window? the offset?

Our Histogram Problem Histogram (0,0) Ticks Labels Title Ticks Labels Title

Doing Math Pylab math library an application programming interface or API set of useful functions >>> from math import * >>> pi functions/constants defined in an API turn purple

math API pow(x, y) returns x**y. sqrt(x) returns the square root of x

math API ceil(x) returns the ceiling of x as a float, the smallest integer value greater than or equal to x. floor(x) returns the floor of x as a float, the largest integer value less than or equal to x.

math API min(x) returns minimum value in list x max(x) returns maximum value in list x sum(x) returns sum of values in list x

math API Trigonometric functions sin cos asin acos tan atan degrees Radians Logarithm functions Two Constants: pi, e

Using the math API def calcBinSize(data, numBins): data.sort() mn = data[0] mx = data[len(data) - 1] return (mx - mn) / float(numBins) >>> d = range(20, 40, 2) >>> calcBinSize(d, 4) 4.5

Using the math API from math import * def calcBinSize(data, numBins): data.sort() mn = data[0] mx = data[len(data) - 1] return ceil((mx - mn) / float(numBins)) >>> d = range(20, 40, 2) >>> calcBinSize(d, 4) 5.0

Using the math API from math import * def calcBinSize(data, numBins): rng = max(data) – min(data) return ceil(rng / float(numBins))

Significant Digits Digits in a number that are meaningful Depend on number of significant digits in data Significant Digits in Calculations The number of significant digits in an answer should equal the least number of significant digits being multiplied or divided The number of decimal places in the answer should be the same as the least number of decimal places in any of the numbers being added or subtracted.

Why Care? >>> 22/ Computers have ability to calculate to a high degree of precision But...they are producing more information than input All those extra digits take up space

Outputting Numbers >>> print 'The value of pi is ', pi Value of pi, 3 places after the decimal point: >>> print 'The value of pi is %5.3f' % pi 3.142

Formatting 'The value of pi is %5.3f' % pi % : contains a format specification for the value to be inserted at that point Begins with % - sign Ends with f: floating point value Number between % and f specifies the format

Formatting 'The value of pi is %5.3f' % (pi) % : contains the value to be formatted

Numerical Specifications f: float d: integer s: string >>> print 'My choice was %10s' % 'Rock' My choice was Rock Justification: -, + >>> print 'My choice was %-10s' % 'Rock' My choice was Rock