Department of Mathematics

Math 300: Mathematical Computing

Python Data Types

One of the things that can make Python a little tricky is the different interpretations it can put on a value. Python uses a few simple data types, and any time a value is entered, python must decide how that value is to be interpreted. Python can support very sophisticated user-constructed data types, but we will only concern ourselves here with the basic predefined ways that input can be interpreted.

Floating Point Numbers

The most important data type for mathematicians is the floating point number. By default, python interprets any number that includes a decimal point as a double precision floating point number. We will not discuss the true binary representation of these numbers. In order to make good use of python, we need only to observe that floating point representatations are effectively writing numbers in scientific notation: a×10b. The number a is called the mantissa of the number, while b is the exponent. In a double precision representation, the number a may be thought of as a sixteen-digit number between -1 and 1. This is not exactly right, but it is a decent rule of thumb. The number b is an integer between -1022 and 1023, inclusive.

>>> a = 3 >>> a/4 0 >>> float(a)/4 0.75

Integers

When you type a=3 at the prompt, python interprets the number 3 as an integer. This is probably what you want, but it might lead to arithmetic results that surprise you. Consider the following python commands.

>>> a = 3 >>> a/4 0 >>> float(a)/4 0.75

When python does arithmetic with integers, it always truncates the result to an integer. Thus in python, 3/4=0. If we want to get decimal answers, at least one number in the computation must be in a floating point representation.

We can convert integers to floating point numbers in python using the float and double functions. These are almost the same - two names are provided because in some programming languages there are differences between float and double types. There are no such differences in python, but note that float is built in to python, while double comes from numpy, and hence is slightly different. Interestingly, it turns out that the double function is somewhat slower than float.

>>> from numpy import * >>> a=3 >>> 1/a 0 >>> 1/float(a) 0.3333333333333333 >>> 1/double(a) 0.33333333333333331

By the same token, we can truncate floating point numbers to integers in python using the int command.

>>> pi 3.141592653589793 >>> int(pi) 3

Strings

Strings (in any programming language) are just plain ol' text. Computers like to deal in numbers, so when we want to type plain ol' text, we must enclose it in quotation marks. We can use any ol' kind of quotation marks we want, whether single or double, just so long as we match them properly.

>>> hi="Hello, world!" >>> bye='Goodbye, cruel world!' >>> wut="What is this 'world' you speak of?" >>> hi 'Hello, world!' >>> bye 'Goodbye, cruel world!' >>> wut "What is this 'world' you speak of?"

Evidently we can enclose one quote inside another, so long as we manage the types of quotation marks used. There is one other type of quotation mark, created by typing three single-quotes together. This lets us type multiple line strings. The \n you see is a representation for a newline character - the invisible character a computer uses to move to the next line.

>>> longstring='''This is a preposterously long string, ... which we use to illustrate that we can be quite verbose ... in python if we want to.''' >>> longstring 'This is a preposterously long string,\nwhich we use to illustrate that we can be quite verbose\nin python if we want to.'


The "final exam" for this course will take place at 8:00 AM on Tuesday, 12 December. This will be an ordinary 50 minute test. It will be comprehensive, but weighted toward the latter half of the semester. As always, paper notes will be permitted, but no electronic devices will be allowed.




A Solution example is available for the quiz.




Assignment A is posted.

Department of Mathematics, PO Box 643113, Neill Hall 103, Washington State University, Pullman WA 99164-3113, 509-335-3926, Contact Us
Copyright © 1996-2015 Kevin Cooper