您在這裡

Variables

17 三月, 2015 - 10:17

One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value.

An assignment statement creates new variables and gives them values:

>>> message = 'And now for something completely different'>>> n = 17>>> pi = 3.1415926535897931

This example makes three assignments. The first assigns a string to a new variable named message; the second assigns the integer 17 to n; the third assigns the (approximate) value of \pi to pi.

To display the value of a variable, you can use a print statement:

>>> print n17>>> print pi3.14159265359

The type of a variable is the type of the value it refers to.

>>> type(message)<type 'str'>>>> type(n)<type 'int'>>>> type(pi)<type 'float'>