See All Titles |
![]() ![]() Introduction to NumbersNumbers provide literal or scalar storage and direct access. Numbers are also an immutable type, meaning that changing or updating its value results in a newly allocated object. This activity is, of course, transparent to both the programmer and the user, so it should not change the way the application is developed. Python has four types of numbers: "plain" integers, long integers, floating point real numbers, and complex numbers. How to Create and Assign Numbers (Number Objects)Creating numbers is as simple as assigning a value to a variable: anInt = 1 1aLong = -9999999999999999L aFloat = 3.1415926535897932384626433832795 aComplex = 1.23 + 4.56J How to Update NumbersYou can "update" an existing number by (re)assigning a variable to another number. The new value can be related to its previous value or to a completely different number altogether. anInt = anInt + 1 aFloat = 2.718281828 How to Remove NumbersUnder normal circumstances, you do not really "remove" a number; you just stop using it! If you really want to delete a reference to a number object, just use the del statement (introduced in Section 3.5.6). You can no longer use the variable name, once removed, unless you assign it to a new object; otherwise, you will cause a NameError exception to occur. del anInt del aLong, aFloat, aComplex Okay, now that you have a good idea of how to create and update numbers, let us take a look at Python's four numeric types.
|
© 2002, O'Reilly & Associates, Inc. |