Variables

The technical term used to define a variable is: “a placeholder for texts and numbers that is used to store a value”. If you’re like me then that made no real comprehensive sense. So in my own words, a variable is something (whether it be a letter or a word or phrase) that you can give a specific value to. This is useful because you refer back to the variable later in the code rather than type out everything you had before. For example if you wanted to type out the whole value of pi, you could assign the value of pi to the variable “pi” and if you wanted to refer back to the value of pi you could just used the variable that was defined as pi. A real example can be seen as:

[Console]

>>> x = 7

>>>print(x)

=====

[Interpreter]

7

Another example would be:

[Console]

>>> x = 7

>>> print(x + 3)

=====

[Interpreter]

10

An interesting thing about variables is that once you assign a value to a variable, it is possible to reassign another value later. In python, variables don’t have specific requirements therefore what you can do is that a variable can be assigned a string but then can be later assigned an integer without having to do any sort of conversion or other function.

[Console]

>>> x = 4

>>> print(x)

=====

[Interpreter]

4

*In the same code but later on…

[Console]

>>> x = “Hello World”

>>> print(x + “!”)

=====

[Interpreter]

Hello World!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s