Variables

  • Python is 'dynamically typed', meaning that the interpreter doesn't require you to explicitly state the type of a variable when you create it, unlike C/FORTRAN.
  • Python is also 'strongly typed', meaning that the variables do have a given type internally once declared and operations trying to combine different types will give errors, just like C/FORTRAN.

Creating variables of simple types


In [1]:
a = 1 # Integer
a


Out[1]:
1

In [2]:
b = 2.0 # Float
b


Out[2]:
2.0

In [3]:
c = "A string" # String, single, double, and treble-double quotations marks are
               # all accepted.
c


Out[3]:
'A string'

In [4]:
d = 1+2j # Complex number
d


Out[4]:
(1+2j)

In [5]:
e = True # Boolean
e


Out[5]:
True
  • Python variables are all objects rather than simply values. This means that they can (and often do) have member variables or functions, called methods in Python. Test them out in IPython to see what is available.

In [6]:
a = 0.25
a.as_integer_ratio() # Calling a member function/method of object 'a'


Out[6]:
(1, 4)

Difference between Python variables and C/FORTRAN

  • In C when you declare an integer variable, a, and its value, 1, you are asking for an "integer's worth" of memory and putting an integer representation of 1 at that memory location. Using the variable a then queries the memory location to get the value stored at a.
  • In Python you are instead asking for a Python object of type int and a specific value to be created, then to be given the label a. Using the variable a then tells the interpreter to return the object that a points at.
  • The fact that Python variables act like labels or references, rather than the value they contain can lead to some false assumptions about how the assignment operator = works.

In [7]:
a = 1
b = 2
print("id(a) =", id(a))
print("id(b) =", id(b))
b = a
print()
print("id(a) =", id(a))
print("id(b) =", id(b))


id(a) = 10455040
id(b) = 10455072

id(a) = 10455040
id(b) = 10455040
  • In the code above we are asking for the memory identifier (like a pointer) of the integer a and b. After asking for b = a we can see that b actually refers to the same object in memory as a. Rather than having the value of a copied into the memory of b, as it would in C. In essence we have labelled the integer 1 with both variables a and b.
  • However all of the types we've seen so far are immutable, meaning that their values cannot be changed without changing their id() value. Because of this we can't get into trouble by forgetting this difference here. But the mutable types discussed later can get you into trouble if you don't remember what your variables are labelling.

In [8]:
a = b = c = 1  # Can assign multiple variables to the same object on one line
print(id(a))
print(id(b))
print(id(c))


10455040
10455040
10455040

Operators

  • Python supports the standard arithmetic operators and a few extra ones

In [9]:
a = 5.0
b = 2.0
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print()
print(a % b)  # Modulus, divides left by right and returns remainder
print(a ** b) # Exponent, raises left value to the power of the right
print(a // b) # Floor division, divides left by right and removes any
              # value after the decimal point


7.0
3.0
10.0
2.5

1.0
25.0
2.0
  • Strings can be joined together with the + operator

In [10]:
a = "str"
b = "ing"
print(a + b)


string
  • You can use all of the arithmetic operators with the = operator in the following way (Note that in Python 3, division of two integers gives a float).

In [11]:
a = 1
a += 1 # Equivalent to a = a + 1
print(a)
b = 5
a /= b # Equivalent to a = a / b
print(a)


2
0.4

Comparisons

  • Python supports the standard comparison operators

In [12]:
a = 1
b = 2
print(a == b)
print(a != b)
print(a < b) # Also <=
print(a > b) # Also >=


False
True
True
False
  • You can also compare the id of the Python objects rather than their value with the is and not keywords.

In [13]:
a = "abc"
b = "a"
b = b+"bc" # In notebooks/IPython we have to do this to force a and b to have
           # different ids. Not necessary in normal Python scripts.
print(a==b)
print(a is b)
b = a
print(a is b)
print(a is not b)


True
False
True
False

Casting

  • You can change the type of variables using built in functions, if the conversion makes sense.

In [14]:
# Define some numbers as strings
a = "3.0"
b = "-6.0"
print(a + b) # '+' operator simply concatenates strings
print(float(a) + float(b)) # Cast as floats before '+' evaluated


3.0-6.0
-3.0

In [15]:
c = -4.0
str(c)+" is a string" # Can cast to strings


Out[15]:
'-4.0 is a string'

In [16]:
int(float(a)) # Can chain together casting


Out[16]:
3

In [17]:
complex(1,2)


Out[17]:
(1+2j)
  • There are several other built in types and their functions in Python which we'll skip over here for brevity. Full lists can be found in the Python documentation or elsewhere on the web.
  • If you want to have more control over your float precision use libraries like Decimal.