Data Types in Python language
Python handles several data types to facilitate the needs of programmers and application developers for workable data.
In Python, all data has an associated data “Type”.
Python has five standard data types:
- Numbers
- String
- List
- Tuple
- Dictionary
Python Numbers
One of the many conveniences of using Python is that you don’t really have to declare a numeric value to distinguish its type.
Number data types store numeric values. They are immutable data types, which means that changing the value of a number data type results in a newly allocated object.
Number objects are created when you assign a value to them. For example:
var1 = 10
var2 = 15
Python supports four different numerical types:
- int (signed integers)
- long (long integers [can also be represented in octal and hexadecimal])
- float (floating point real values)
- complex (complex numbers)
Number Examples:
int |
long |
float |
complex |
10 |
51924361L |
0.0 |
3.14j |
100 |
-0x19323L |
15.20 |
45.j |
-786 |
0122L |
-21.9 |
9.322e-36j |
080 |
0xDEFABCECBDAECBFBAEl |
32.3+e18 |
.876j |
-0490 |
535633629843L |
-90. |
-.6545+0J |
-0x260 |
-052318172735L |
-32.54e100 |
3e+26J |
0x69 |
-4721885298529L |
70.2-E12 |
4.53e-7j |
Python Strings
- A string is a sequence of Unicode characters that may be a combination of letters, numbers, and special symbols.
- To define a string in Python, you can enclose the string in matching single or double quotes:
>>>string1 = “I am enclosed in single quotes.”
>>>string2 = “I am enclosed in double quotes.”
Python Lists
- A list is a data type that can be used to store any type and number of variables and information.
- Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]).
- To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.
- You can define and assign items to a list with the expression:
my_list = [item_1, item_2, item_3]
Python Tuples
- A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.
- The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ), and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
Python Dictionary
- A dictionary is like a list but instead of looking up an index to access values, you’ll be using a unique key, which can be a number, string, or tuple. Dictionary values can be anything but the keys must be an immutable data type. A colon separates a key from its value and all are enclosed in curly braces.
- Python 's dictionaries are hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs.
- Keys can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.
- Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and accessed using square braces ( [] ).
d = {key_1 : a, key_2 : 2, key_3 : ab}