[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
BeginnersGuideToPython_Types
This tells the compiler to set aside enough memory to hold an integer and lets the compiler know how to handle operations on that variable. When a language requires the type of a variable to be specified when the variable is defined, it is said to be "Statically Typed". In languages such as C or C++, the variable will then remain that type for as long as it is in scope. Python, in contrast, is "Dynamically" typed. Specifically, variables are created without specifying a type and the same variable can change type through the course of execution.
For example:
As you can see here, the variable a was assigned the value 1. When you assign a value to a variable, Python creates an entry in your namespace for that variable. You never have to declare it beforehand and you never specify a type. Using the built-in function, type(), we can see that a is an integer. a can then be used anywhere an integer can be used as in simple addition. Halfway through our session, however, we assign a string to the variable a. Python changes our variable in mid-execution to an entirely different type! Now integer math doesn't work because you can't add an integer to a string! You can, however, add (concatenate) two strings together as shown by the statement a + '1'. Hopefully this demonstrates the dynamic nature of Python type handling.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
BeginnersGuideToPython_Types
Beginner's Guide To Python, Data Types
Type Basics
Every object in Python has a type. There are differences, though, in how Python handles types compared to other more common languages. In C or C++, a programmer declares a variable before using it and specifies the type of data that variable will hold. For example:int my_integer;
This tells the compiler to set aside enough memory to hold an integer and lets the compiler know how to handle operations on that variable. When a language requires the type of a variable to be specified when the variable is defined, it is said to be "Statically Typed". In languages such as C or C++, the variable will then remain that type for as long as it is in scope. Python, in contrast, is "Dynamically" typed. Specifically, variables are created without specifying a type and the same variable can change type through the course of execution.
For example:
Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> a = 1
>>> a
1
>>> type(a)
<type 'int'>
>>> a + 1
2
>>> a = 'foo'
>>> a
'foo'
>>> type(a)
<type 'str'>
>>> a + 1
Traceback (most recent call last):
File "<pyshell#7>", line 1, in ?
a + 1
TypeError: cannot concatenate 'str' and 'int' objects
>>> a + '1'
'foo1'
>>>
As you can see here, the variable a was assigned the value 1. When you assign a value to a variable, Python creates an entry in your namespace for that variable. You never have to declare it beforehand and you never specify a type. Using the built-in function, type(), we can see that a is an integer. a can then be used anywhere an integer can be used as in simple addition. Halfway through our session, however, we assign a string to the variable a. Python changes our variable in mid-execution to an entirely different type! Now integer math doesn't work because you can't add an integer to a string! You can, however, add (concatenate) two strings together as shown by the statement a + '1'. Hopefully this demonstrates the dynamic nature of Python type handling.
Numbers
Strings
Lists
Tuples
Dictionaries
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
