Learning Outcomes: Upon successful completion of this lesson, you will be able to: • Explain the use of variables in a program • Explain the use of expressions and statements in a program
What are Variables?
Variables are a memory locations used to store data. The data can be retrieved using variable names during program execution.
This short video gives a detailed explanation on Variables and the Rules for Naming them (about 13 minutes)
>>> x = 1
>>> print(x)1
>>> x = 99
>>> print(x)
99
x is a variable and it is being assigned a value of 1. The print statement takes the value in x and displays it on the screen.
The next statement assigns the value of 99 to x. Again, the print statement takes the value in x and displays it on the screen.
Study the program given below:
x = 1
y = 2
x = x + 3
y = x
Rules for Variable Name
Variable name must start with a letter or underscore _. The subsequent characters can be a combination of letters, numbers or underscore _. Variable names are case sensitive. That is, nric and Nric are not the same.