What is Data type?
- A data type shows what kind of data is being used.
- The type() function can be used to determine which type of data.
Example:
Number = 5
Here, 5 is an integer assigned to the variable, so data type of number is int class.
Types of data types:
Numeric (int, float, complex): Stores numbers.
Example Program:
# int number1 = 5 print(number1, 'is type of', type(number1)) # float number2 = 6.0 print(number2, 'is type of', type(number2)) # complex number3 = 7+8j print(number3, 'is type of', type(number3))
output:
5 is type of <class 'int'> 6.0 is type of <class 'float'> (7+8j) is type of <class 'complex'>
String (str): Stores text.
Example Program:
text = "Hello" print(text, 'is type of', type(text))
output:
Hello is type of <class 'str'>
Sequence (list, tuple, range): Stores a collection of items.
Example Program:
# List my_list = [1, 2, 3] print(my_list, 'is type of', type(my_list)) # Tuple my_tuple = (1, 2, 3) print(my_tuple, 'is type of', type(my_tuple)) # Range my_range = range(3) print(my_range, 'is type of', type(my_range))
output:
[1, 2, 3] is type of <class 'list'> (1, 2, 3) is type of <class 'tuple'> range(0, 3) is type of <class 'range'>
Mapping (dict): Stores data in key-value pairs.
Example Program:
my_dict = {'a': 1, 'b': 2} print(my_dict, 'is type of', type(my_dict))
output:
{'a': 1, 'b': 2} is type of <class 'dict'>
Boolean (bool): Stores True or False.
Example program:
my_bool = True print(my_bool, 'is type of', type(my_bool))
output:
True is type of <class 'bool'>
Set (set, frozenset): Stores unique items.
Example program:
# Set my_set = {1, 2, 3} print(my_set, 'is type of', type(my_set)) # Frozenset my_frozenset = frozenset([1, 2, 3]) print(my_frozenset, 'is type of', type(my_frozenset))
output:
{1, 2, 3} is type of <class 'set'> frozenset({1, 2, 3}) is type of <class 'frozenset'>
0 Comments