Data type identifies the type of data values a variable can hold and the operations that can be performed on that data.
Number
Number data type stores numerical values only.
It is further classified into three different types:
Boolean data type (bool) is a subtype of integer . It is a unique data type, consisting of two constants, True and False . Boolean True value is non-zero, non-null and non-empty. Boolean False is the value zero.
Sequence
A Python sequence is an ordered collection of items , where each item is indexed by an integer. The three types of sequence data types available in Python are:
- Strings - String is a group of characters . These characters may be alphabets, digits or special characters including spaces . String values are enclosed either in single quotes or double quotes.
Examples:
- str1 = ”hello”
- str2 = ’123’
- Lists - List is a sequence of items separated by commas and the items are enclosed in square brackets [ ].
Examples:
- list1 = [1,25,”abc”,”Delhi”]
- list2 = [“India”,”USA”,”UK”]
- Tuples - Tuple is a sequence of items separated by commas and items are enclosed in parenthesis ( ). Once created, we cannot change the tuple.
Examples:
- tuple1 = (‘a’,1,25,”Noida”)
- tuple2 = (“Delhi”,”Mumbai”,”Chennai”,”Cochin”)
Set
Set is an unordered collection of items separated by commas and the items are enclosed in curly brackets { } . A set is similar to a list , except that it cannot have duplicate entries. Once created, elements of a set cannot be changed .
Examples:
- set1 = {1,’a’,34,”Delhi”}
- set2 = {“Delhi”,”Mumbai”,”Chennai”,”Cochin”}
None
None is a special data type with a single value. It is used to signify the absence of value in a situation. None supports no special operations , and it is neither the same as False nor 0 (zero).
Example:
- var1 = None
Mapping
Mapping is an unordered data type in Python .
There is only one standard mapping data type in Python called a dictionary.
Dictionary
- Dictionary in Python hold data items in key-value pairs.
- Items in a dictionary are enclosed in curly brackets { }.
- Dictionaries permit faster access to data.
- Every key is separated from its value using a colon (:) sign.
- The key : value pairs of a dictionary can be accessed using the key.
- The keys are usually strings and their values can be any data type.
- In order to access any value in the dictionary, we have to specify its key in square brackets [ ].
Example:
dict1={‘Fruit’:’Apple’,’Price’:120,’Weight(kg)’:1}
Mutable and Immutable Data Types
Mutable data types |
Immutable data types |
Variables whose values can be changed after they are created and assigned are called mutable. |
Variables whose values cannot be changed after they are created and assigned are called immutable. When an attempt is made to update the value of an immutable variable, the old variable is destroyed and a new variable is created by the same name in memory |
|
|