Variables

Odin May | Google Colab Notebook

This is a short description for the post which introduces it



Variables are a crucial thing to be comfortable with in Programming. They work the same way in programming as they do in mathematics. We can use a variable to represent our data. This is something you will use all the time while coding. To declare a variable we write the name and use the assignment operator ‘=’ then we enter what the variable will represent. Below we set the variable X to the word ‘Apple’.

 

X = ‘Apple’

print(X)

 

    Our variable X can now be used later in our program and it will be interpreted as ‘Apple’. If we want to print out the word apple we can just print X instead and Python will evaluate it to ‘Apple’. In Python we can freely reassign variables to a new value. If we later decide that we want X to be equal to the word ‘Orange’, or the number 256, we can do that by just declaring it again.

 

X = ‘Orange’

X = 256

 

    Variables are very simple but very powerful. One extremely useful thing to keep in mind is that you can set a variable equal to the result of an expression. So we can say that X is equal to 10 + 10.

 

X = 10 + 10

print(x)

  

    Variables can actually be set using the variable itself. Our variable X was most recently set to 20 which means that will be it’s current value. If we want to set it to the value of itself plus 1. We can do that as well, this will often come in handy when updating your variables based on certain conditions.

 

X = X + 1

print(x)

 

    Variables allow you to make your code more readable and they are a fundamental component to programming. Making sure you understand variables is key to learning programming, but don’t worry they will come very natural to you. Take some time to assign some variables and try printing them to the screen and reassigning them just to get familiar. Remember to keep confident and keep coding.

Sidebar content