---
# System prepended metadata

title: 'Python: Variables'

---

# Python: Variables

([home](https://github.com/alexhkurz/introduction-to-programming/blob/master/README.md) ... [previous](https://hackmd.io/@alexhkurz/SJN2udq3I) ... [next](https://hackmd.io/@alexhkurz/H1o4Mcr6L))


In the session on [functions](https://hackmd.io/@alexhkurz/SJ1DcL43L), we introduced variables as place-holders for values to be used in a function.

These place-holder variables behave similar to the variables we know from mathematics.

But in Python (and so-called imperative programming languages more generally) we can do much more with variables: 

A variable 
- is a name of a memory location that can contain values and 
- the value can be changed by **assignment**.

(Caveat: We will need to refine this in a later session.)

For example, after

    x=1
    
the name `x` refers to a memory location that contains the value `1`. 

Different names always refer to different memory locations but two different locations can contain the same value. For example, 

    >>> x=1
    >>> y=1
    >>> x==y
    True
    
shows us that the two different locations `x` and `y` contain the same value. 

**Activity:** After

    >>> x=1
    >>> y=2
    >>> x=y
    
what are the values of `x` and `y`? 

Construct a memory model of variables-as-locations-that-have-values that explains how assignment works above. 

Run more experiments to validate (or adapt) this memory model. 

What will be the result of the following?

    >>> sum = 1
    >>> sum = sum + 2
    >>> sum = sum + 3
    >>> sum = sum + 4
    >>> sum

and

    >>> x = 1
    >>> y = 2
    >>> z = x
    >>> x = y
    >>> y = z
    
Before checking the values of `x` and `y` write down your prediction. 

Then do

    >>> x
    >>> y

<!--
**Some examples one could try:** (But much better than copying the below is to try your own.)

    >>> 2=3
    
or 

	>>> x=3
    >>> x
    
	>>> y=5
    >>> y
    
	>>> x=y
	>>> 3==y
	
	>>> y==x

	>>> x=y
	>>> y=x
	>>> 3==y
	
	>>> 5==x
-->   
	


    


## References

[Python Language Reference: Literals](https://docs.python.org/3/reference/expressions.html#literals)

---

**Session stops here ... everything below is for later ...**

---


## Local and global variables

**Exercise:** What does the following program print?

    i = 10
    def f(n):
        print(n+i)
    i = 42
    f(1)


## Weird Stuff

There is always something to discover. For example, to explain the following one needs to go into quite some detail of the memory-model of Python.

### Integers are objects

    a=256
    b=256
    a is b
    
vs

    a=257
    b=257
    a is b
    
vs 

    a=257; b=257
    a is b

### Mutable vs immutable objects

	>>> x=1
	>>> y=x
	>>> y=2
	>>> y==x
	False
    
	>>> x=[1]
	>>> y=x
	>>> y[0]=2
	>>> y==x
	True
    
## Further Reading

[Why Python is Slow: Looking Under the Hood](https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/). If you are new to programming, you have to be quite courageous to read this. But it is full of deep and important ideas about programming languages, in particular if you also read the comments. So I couldn't resist linking this.