Remember, Python refers to anything used in a program as an object. This is meant in the generic sense. Instead of saying 'the something', we say 'the object'.
Python is strongly object-oriented in the sense that everything is an object including numbers, strings and even functions.
We will now see how to use variables along with literal constants. Save the following example and run the program.
Henceforth, the standard procedure to save and run a Python program is as follows:
Open your favorite editor.
Enter the program code given in the example.
Save it as a file with the filename mentioned in the comment.
I follow the convention of having all Python programs saved with
the extension .py
.
Run the interpreter with the command
python program.py
or
use IDLE to run the programs. You can also use the
executable method
as explained earlier.
Example 4.1. Using Variables and Literal constants
# Filename : var.py i = 5 print i i = i + 1 print i s = '''This is a multi-line string. This is the second line.''' print s
Here's how this program works. First, we assign the literal constant value
5
to the variable i
using the
assignment operator (=
). This line is called a statement
because it states that something should be done and in this case, we connect
the variable name i
to the value 5
.
Next, we print the value of i
using the print
statement which, unsurprisingly, just prints the value of the variable to the
screen.
The we add 1
to the value stored in i
and
store it back. We then print it and expectedly, we get the value
6
.
Similarly, we assign the literal string to the variable s
and then print it.
Variables are used by just assigning them a value. No declaration or data type definition is needed/used.