Udacity CS 101: Even more Variables

Now that we know what are variables and how do we assign them, its time to move on to something more advanced. Consider the statement below:

savings = 2000

savings = savings + 1000

Now if you come from a mathematical background, this probably wouldn’t make sense to you. How can savings on the left (2000) be equals to savings (2000) plus 1000, which is 3000. 2000 = 3000? That is just plain nonsense.

Well the equals sign in Python, as well as many other programming languages, isnt the same as equals. Rather it is an assignment operator. In other words when we write code such as savings = 2000 we are actually assigning the number 2000 to the variable named savings.

So python evaluates expressions in this manner, it looks at what is on the right of the assignment operator

savings = savings + 1000

As we have learned in Backus Naur form, the non-terminal terms will be replaced by terminal terms, and in this case savings is replaced by 2000, which we assigned in the prior statement:

savings = 2000 + 1000

Python evaluates the addition:

savings = 3000

And now this is simplified to an assignment expression, the value of savings is thus, 3000.

This is a very important concept that you will have to grasp and if you are still confused, feel free to ask questions in the comments section below.

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