Values

A value can be assigned directly to a variable, using an explicit value. Such an explicit value is called a literal. A character literal, in this case the character literal b, can be assigned to a variable with the name a using the assignment

a = 'b' 

As can be seen, the assigment is done by placing the character b, enclosed in single quotes, at the right hand side of an equals sign. When a value is assigned to the variable, the variable is automatically being declared. In this case, since a character value was assigned to the variable, the variable is declared to be a variable that can store strings of characters. A variable of this type can hold values which are strings, like "Hello, world", consisting of characters such as the letters a to z, but also special signs, like semicolon (;), comma (,), and exclamation mark (!).

A variable can also be assigned a numerical value. There are two kinds of numerical values. One kind is referred to as floating point values, and the other kind is referred to as integer values. A floating point variable represents decimal numbers and an integer variable represents whole numbers. As an example, the decimal value for \(\pi\), rounded by the programmer to 7 decimals, can be assigned to a variable named pi, using the assignment

pi = 3.1415927

As an example of an assignment of an integer variable, a variable named i can be assigned the value 5, as

i = 5

Variables of integer type can hold values which are whole numbers. Positive numbers as well as negative numbers are allowed. A complete program with variables, and with literal values being assigned to these variables, is shown in Figure 3.

i = 5

a = 'b' 
pi = 3.1415927

print "i is %d and pi is %g and a is %c" % (i, pi, a)

Figure 3. A program with variables, and with assignments of values to these variables.

This the Python view - other views are C - Java

The program in Figure 3 contains the three variables described above, named i, pi, and a. The program also contains an instruction for printing the values of the variables. The printing is done by a print statement, starting with the word print and then followed by the value to be printed, as

print "i is %d and pi is %g and a is %c" % (i, pi, a)

In this statement, a string of text is combined with the variables i, pi, and a. The combination is done by having special formatting codes inside the text string. The formatting codes consist of a percent-sign followed by a letter. The letters used are d - indicating that the printed value is an integer value, g - indicating that the printed value is a floating point value, and c, indicating that the printed value is a character value.

The program in Figure 3 can be run, as described in Section How to make it run. The output of the program is then shown, as

i is 5 and pi is 3.14159 and a is b