Python Beginner to Expert/Native Types
Python variables and built-in types
What is a variable?
A variable is symbolic name for an area in memory that has a certain format. The format of that area is the area's type. Closely related to the concept of format and type is usage. It is called a variable because the value assigned to it can be changed. Due to the flexible nature of variable binding in Python, a variable can be rebound to any type at any point in a progam.
Python's 3.x's rules for variable names are typical for a modern programming language:
Identifiers (also referred to as names) are described by the following lexical definitions. The syntax of identifiers in Python is based on the Unicode standard annex UAX-31, with elaboration and changes as defined below; see also PEP 3131 for further details. Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase letters A through Z, the underscore _ and, except for the first character, the digits 0 through 9. Python 3.0 introduces additional characters from outside the ASCII range (see PEP 3131). For these characters, the classification uses the version of the Unicode Character Database as included in the unicodedata module. Identifiers are unlimited in length. Case is significant.
In other words, variable names can be of any length and can use any upper case or lowercase letters, the underscore, and the digits 0-9. However, variables names cannot begin with a digit. Variable names are case sensitive.
One reason special characters such are not permitted is that they are already reserved for use as operators.
Examples of valid variable names:
Examples of invalid variable names
- 1var - wrong, begins with digit
- my#Variable - wrong, variable names can only include a-z, A-Z, _, and 0-9. Other special characters are not permitted.
Case sensitivity example
- myVariable, myvariable, and Myvariable represent three separate variable names.
Types in Python
What is a type?
As we have said, Python supports two main numeric types: integer and real. To us, the human being, the difference between the two types is that the integer type represent whole or round numbers while the real type also includes fractional or rational numbers. We don't recognize a big distinction between how we represent on paper but we know that operations performed on fractional numbers often require additional steps.
A computer makes a different kind of distinction between integers and real numbers and this is part due to the internal representation of the number and the methods a digital computer must use to perform operations on the numbers.
Python and other computer languages often perform arithmetic on different types of numbers in combination. For example, consider adding an integer and a real number, as in 10 + 1.1. In general a computer language will convert the less precise type to the more precise type to perform the operation and return the result as the more precise type.
How does python recognize a numeric type? It does so by storing the type as well as the value and keeping an index to it that includes a count of the number of active references to it. The variable name is used to point to the variable and in fact the variable exists completely independent of the variable name.
Python has only a few built-in types.
The Boolean type: You can assign the value of True or False to a variable. If you wish to verify that Boolean is its own type, enter the following code at the Python command line:
>>> type(True) <type 'bool'> >>> type(False) <type 'bool'>
Note: You can use the type() function on any value or variable or identifier and Python will echo or return the type.
Numbers: Python has numeric commonly used built-in numeric types: real and integer. Python also supports complex numbers.
Complex numbers are always represented as two floating point numbers, the real and imaginary part.
6+1j represents 6 + square root of -1
In the case of complex number, the j used in the imaginary part is not case sensitive. 6+1j is the same as 6+1J.
Examples of integers: -100, 200, 10000, 123456789 Integers can be of arbitrary length. There is no limit to the length of integers in Python.
Examples of real numbers: -100., 200.2, 10000.3, 1234.56789 There are limits to the size of the real numbers. Use the ?? function to determine the limits for your system.
Examples of complex numbers: 1j implies 0+1j and represents the square root of -1 2j implies 0+2j and represents the square root of -2
Characters and Strings: Python does not have a special character type for single characters. The Python primitive type used for character data is the string class <class 'str'>.
Python's string values are immutable. When a string is created in memory and assigned to a variable name, the string cannot be altered. However, the variable may be assigned a new or different string value.
Strings are considered sequence types. Python has six built-in sequence types: strings, byte sequences (bytes objects), byte arrays (bytearray objects), lists, tuples, and range objects. The other sequence types will be discussed in other parts of this tutorial.
1. Open the command line interpreter or the IDLE command shell. Try the following at the prompt (press newline after each line). Observe the results. Make a mental or other note on any unexpected results. Consider how you might research the unexpected results.
1 type(1) type(-1) type(-55555555555555555555555555555) 1.1 type(1.1) 1e5 type(1e5) 1j type(1j) 1J type(1J) 0+1j 1+1j 1+1j*2+1j (1+1j)*(2+1j) type(1j) type(1+1j) 10.1*1+1j type(10.0*1+1j) dog 'dog' type(dog) type('dog') false False 'False' "False" type(False) type(True)