A Byte of Python

Variables

Using just literal constants can soon become boring since your program will give the same results everytime. We need some way of storing information and manipulating them so that we can do interesting things. We can achieve this using variables. Variables are exactly what they mean, their value can vary - you can store any value using a variable. Variables are just parts of your computer's memory which you are using to store some information. Unlike literal constants, we need some way to use these variables and hence we give names for variables.

Identifier Naming

Variables are examples of identifiers. Identifiers are names given to identify something. We can choose any name for identifiers as long as they satisfy these simple rules:

  • The name can consist of alphabets (both lower and uppercase), underscore ('_') and digits (0-9).

  • Only the first character of the name cannot be a digit.

  • Names are case-sensitive. For example, myname and myName are not the same names, they are two different variables. Note the lowercase n in the former and the uppercase N in the latter.

  • Examples of valid identifier names are i, __my_name, name_23 and a1b2_c3.

  • Examples of invalid identifier names are 2things, this is spaced out, and my-name.

Data Types

Variables can hold values of different types, which are called as data types.. The basic types are numbers and strings which we have already discussed.

In later chapters, we will learn how to create our own types using classes.

Objects

Python refers to anything used in a program as an object. What it really means is that Python is strongly object-oriented in the sense that everything in Python is an object including numbers, strings and even functions.

We have not yet explored what an object really means, we will explore it in later chapters. However, it is very important to keep the idea of an object in mind. You can picture an object like a bag where you can put in stuff or take out stuff.

Using Variables

Example 4.1. Using Variables and Literal Constants

#!/usr/bin/env python 
# Filename: var.py 
 
i = 5 
print i 
i = i + 1 # add number 1 to value of i and then store again in i 
print i 
 
s = '''This is a multi-line string. 
This is the second line.''' 
print s

Output

$ python var.py
5 
6 
This is a multi-line string. 
This is the second line.

How It Works

First of all, notice that we have a set of steps in the program and each step is run one after the other in the order that we have listed the steps. Also, notice that each step is on its own line. We can consider each step to be a statement since we are basically stating to Python about our intentions in this program.

Let us now consider the statement i= 5. Remember, that we had previously learnt that we should consider objects as bags. In this case, consider our program as a bag. We are putting the name i into this bag. More appropriately, we are putting the name i into the space of our program, this is why our program is referred to as having a namespace. This is one of the most important concepts that you will learn about Python - when you create a new name, you are entering it into the namespace. That name can hold a value of any type. In this case, it happens to be the name i which is holding the value 5.

We use the print statement to display the value of i. In the statement i = i + 1, the portion to the right side of the equal sign is first calculated. Here, we just add the value of the literal constant 1 to the value of the variable i (which happens to be 5). The sum is 6. Now, let us observe what is to the left side of the equal sign - we have i again - so we give the value 6 to i, this is called assigning the value.

Similarly, we assign a triple-quoted string to the name s and then display its value using the print statement.

Also notice the use of comments to explain parts of the program.

Note for C/C++ Programmers

Variable names are defined when first used. No declaration or data type definition is required.