Why does Python need variables?

So, I was hanging out at the OpenStudy “introduction to python” group, when I saw this question :

What are variables in python?Why are variables required?Does variables have a role to play in development of programs using python? what roles does it play?

Although this is a very basic question, for which the answer lies in pages 10 to 12 of every single programming textbook, I thought I would give it a try with three simple examples. I didn’t want this answer to get lost in the deep oceans of the internet, so here it is. I’ve only changed the formating a little bit.

Because in Python, and programming generally, we need a place to keep our results for a while until we need them. Keep in mind that whatever you compute with a program, you lose it if you don’t store it somewhere. So variables are just that. A way to store information so that you can use it later on in your program and thus they play a central role in development. Except for the case where you are doing something really simple, you will want to use variables to keep things. As you learn more about programming, you will understand this concept for yourself. Until then, here are some examples.

Example 1 : Numerical Sequence

Let’s say that we write a program to compute a sequence of numbers up to a number N. The most famous is the Fibonacci sequence for which we know that

x_{n-1}+x_{n}=x_{n+1},\ x_{0}=x_{1}=1

To do that with a program, we need to store somewhere the two last values x_{n+1},x_{n}, so that we can use them to compute the next number. There is no way that we can continue calculating if we can’t store the results somewhere.

Example 2 : Generality of input

Say we want to write a program in Python to battle against internet trolls who write everything in CAPS. So, our program should take as input a phrase in caps and turn into something in lowercase. Since we are talking about a general phrase here, we have to store it somewhere so that then we can give it to the relevant function in Python.

Example 3 : Ease of programming.

Let’s say that you have to solve the following problem in Python.

Import the math library and compute the values of sine, cosine and tan at 18.849555921538759430775860299677017305183016396250634

Now there are of course many ways of solving this problem. Here’s one :

import math

math.sin(18.849555921538759430775860299677017305183016396250634) math.cos(18.849555921538759430775860299677017305183016396250634) math.tan(18.849555921538759430775860299677017305183016396250634)

and here’s another one :

import math

x = 18.849555921538759430775860299677017305183016396250634




The second way is easier to type, easier to read and has one more important advantage. It’s general! Just change x and you have the values of functions wherever you like without having to change anything below that line. This allows us to concentrate on the problem we are trying to solve and make fewer errors while developing.

I hope those three examples illustrate, even slightly, how important variables are for programming. 🙂


One thought on “Why does Python need variables?

Comments are closed.