Python:Basic data types

From AIMSWiki

Table of contents


Variables

Variables can have names containing any of the following characters: a-z, A-Z, 0-9 and _ (underscore). A variable name may not start with a digit (0-9).

Variables are references to objects. This takes a bit of time to understand but a couple of pictures and examples should make it clear. Firsly, note that

  1. a variable name does not contain a value, it simply points to one
  2. two or more variables can point to the same value



>>> a = 3
>>> b = a

The two variables now point to the same value.

  Image:Python_variables_1.png

>>> b = 5

Since the value of b has been changed, it now points somewhere else.

  Image:Python_variables_2.png

>>> a = [1, 2, 3, 4]
>>> b = a

Again, the two variables point to the same value (a list in this case).

  Image:Python_variables_3.png

>>> a[1] = 5

This changes a value inside the list. Note that, since b and a point to the same list, b's value has also been changed.

>>> print b
[1, 5, 3, 4]
  Image:Python_variables_4.png

Numbers

There are 4 different types of numbers in Python.

  • integer
  • long integer
  • floating point (real number)
  • complex number

The integer and long integer types can be used interchangebly. Long integers can be much larger than integers. Integers are in the range [-2^{31},\  2^{31}-1] and any integer number outside that range is automatically converted to a long integer. The only advantage to using integers rather than long integers, is that mathematical operations with integers. Long integers can be of arbitrary length (until your computer runs out of memory).

Floating point numbers are used to represent real values. These are not perfectly accurate--there are round-off errors as you can see from the following.

>>> 1.0 / 3.0
0.33333333333333331
>>> 1.0 / 4.0
0.25
>>> 1.0 / 5.0
0.20000000000000001

Complex numbers are specified using a j or J to represent \sqrt{-1}.

>>> (1 + 2j) * (3 - 4j)
(11+2j)

Strings

A string is simply a value consisting of text. Strings are placed between quotes--either single quotes ('this is a string') or double quotes ("this is a string"). There is no difference between using single and double quotes. Here are some examples of strings.

"Hello World!"
"This is a string with some 'single quotes' inside."
'This is a string with some "double quotes" inside.'

Lists

A list is a very powerful Python data type that can contain any number of values of any type. The following are examples of homogeneous lists (i.e. lists with the same types of values).

[1, 2, 3, 4]                       # A list of integers
[0.1, 10.2, 1.0, 3.14159265]       # A list of flowing point numbers
['abcde', 'Hello', 'World', '!']   # A list of strings

The following are examples of mixed lists.

[1, 'a', 2, 'b']
['There is a list inside this list', [1, 2, 3]]

Note that

  • Lists are specified using square brackets ([]) and the values inside lists are separated by commas.
  • A list can contain any type of value--even other lists. You can have lists inside lists inside lists up to as many levels as you like.

Indexing lists

The following examples are based on the list

>>> x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

Lists are indexed from 0 and you can find the value at an index as follows.

>>> x[0]     # The first element of x                             (index 0)
3
>>> x[3]     # The fourth element of x                            (index 3)
1
>>> x[-1]    # The last element of x
5
>>> x[-2]    # The second to last element of x
3
>>> x[:3]    # The first 3 elements of x                          (indices 0, 1, 2)
[3, 1, 4]
>>> x[4:]    # The elements of x from index 4 to the end          (indices 4, 5, 6, ...)
[5, 9, 2, 6, 5, 3, 5]
>>> x[2:6]   # The elements of x from index 2 to before index 6   (indices 2, 3, 4, 5)
[4, 1, 5, 9]

This shows how you can select ranges from a list. Note that if a final index is specified (e.g. [2:6]), the indexing always stops before the last value.

You can also skip values while indexing ranges. For example, to find every second value in a range, use the following.

>>> x[0:8:2] # indices 0, 2, 4, 6
[3, 4, 5, 2]

The 2 at the end defines the step size in the index range.

If you have a list inside a list

>>> x = [[1, 'a'], [2, 'b'], [3, 'c']]

the indexing is done with multiple indices:

>>> x[1]
[2, 'b']
>>> x[1][0]
2

Dictionaries

Coming soon...

Tuples

Coming soon...

Files

Coming soon...


Return to the Python index