PythonNumbersAnd1DArraysofNumbers

# PythonNumbersAnd1DArraysofNumbers - approximate, the...

This preview shows pages 1–15. Sign up to view the full content.

Python Numbers and 1-D Arrays of Numbers 1 © Popescu 2012

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Numbers Reading assignment: Chapter 3 from Zelle Numbers w/o fractional part: integers, or int for short Numbers w/ fractional part: floating-point numbers, or floats for short 2
Int or float Type does not have to be declared explicitly Python infers type based on presence or absence of decimal point operands Special Python function type returns the type float or int >>> type(2) <class 'int'> >>> type(2.0) <class 'float'> >>> type(1+2) <class 'int'> >>> type(1 + 2.0) <class 'float'> 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Built-in numeric operations 4
Built-in numeric operations 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
iClicker Question Explain the result in the Python shell window A. The result should be 1.15, you have discovered a Python bug B. The Python shell window image was photoshop-ed to show the incorrect result C. 3.45 cannot be represented precisely in base 2, so, since one operand is

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: approximate, the division result is approximate D. 3.45 divided by 3.0 is a little more than 1.15, theres nothing to explain E. We should have used integer division // for the correct result 6 Some math library functions 7 Some math library functions Using math library import math Prefix function name with math. 8 Explicit type conversion Force int to float or float to int 9 Rounding Allows specifying the number of decimal places 10 1-D array of numbers [element , element 1 , , elemen n-1 ] 0-based indices len function returns length of array, i.e. the number of elements (n) 11 Traversal of 1-D array, version 1 12 Using for loop Directly on elements Traversal of 1-D array, version 2 13 Using range function Traversal of 1-D array , version 2 14 Using range function and for loop on index SortMin in Python 15...
View Full Document

## This note was uploaded on 02/27/2012 for the course CS 177 taught by Professor Staff during the Spring '08 term at Purdue University-West Lafayette.

### Page1 / 15

PythonNumbersAnd1DArraysofNumbers - approximate, the...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online