Your variables need not be numeric. There are several types. The most useful are described below:
Integer: Any whole number:
>>> myinteger = 0 >>> myinteger = 15 >>> myinteger = -23 >>> myinteger = 2378
Float: A floating point number, i.e. a non-integer.
>>> myfloat = 0.1 >>> myfloat = 2.0 >>> myfloat = 3.14159256 >>> myfloat = 1.6e-19 >>> myfloat = 3e8
2.0we indicate that we want it stored as a float, with the precision that entails.3.4 The last examples use exponentials, and in maths would be written and . If the number is given in exponential form it is stored with the precision of floating point whether or not it is a whole number.
String: A string or sequence of characters that can be printed on your screen. They must be enclosed in either single quotes or double quotes--not a mixture of the two, e.g.
>>> mystring = "Here is a string" >>> mystring = 'Here is another'
Arrays and Lists: These are types which contain more than one
element, analogous to vectors and matrices in mathematics. Their
discussion is deferred until Section 3.10 ``Arrays''.
For the time being, it is sufficient to know that a list is written by
enclosing it in square brackets as follows:
mylist = [1, 2, 3,
If you are not sure what type a variable is, you can use the
type() function to inspect it:
>>> type(mystring) <type 'str'>
'str'tells you it is a string. You might also get
<type 'int'>(integer) and
<type 'float'>(float) 3.5.
Use the interactive interpreter to create integer, float and string variables. Once you've created them
Experiment with the following code snippet to prove to yourself that
Python is case-sensitive, i.e. whether a variable named
a is the same as one called
>>> a = 1.2 >>> print A
As a beginner you will avoid making mistakes if you restrict yourself to using lower-case for your Python functions and the names of your variables (but see also Section 2.4 on case-sensitivity).