In programming, a data type is like a tag or label attached to a piece of information. It helps the computer to understand how to handle that specific type of data.
Python has the following built-in data types by default:
- Numeric Types:
- Integer (int): Whole numbers, positive or negative, without any decimal point.
- Float (float): Real numbers, including those with a decimal point.
- Complex (complex): Numbers with a real and imaginary part.
- Sequence Types:
- List: Ordered collections of items, enclosed within square brackets, and mutable.
- Tuple: Ordered collections of items, enclosed within parentheses, and immutable.
- Range: Represents a sequence of numbers, used in looping.
- Mapping Type:
- Dictionary (dict): Unordered collections of items, consisting of key-value pairs enclosed within curly braces.
- Set Types:
- Set: Unordered collections of unique items, enclosed within curly braces.
- Frozenset: Immutable version of sets.
- Boolean Type:
- Boolean (bool): Represents one of two values,
True
orFalse
.
- Boolean (bool): Represents one of two values,
- Binary Types:
- Bytes: Represents sequences of bytes.
- Bytearray: Mutable version of bytes.
- Memoryview: Provides a view on memory as bytes.
- None Type:
- NoneType (None): Represents the absence of a value or a null value.
Check The type of a variable:
In Python, we can use the type()
function to check the type of a variable. Here’s how we can use it:
Check type of integer variable
# Define a variable my_variable = 247 # Check its type variable_type = type(my_variable) # Print the type print(variable_type) #output: <class 'int'>
Check type of string variable
# Define a string variable my_string = "School 24/7" # Check its type string_type = type(my_string) # Print the type print(string_type) #Output: <class 'str'>
Examples:
Integers:
# Integer my_int = 247 print(my_int, type(my_int)) # Output: 247 <class 'int'>
Float
# Float my_float = 3.14 print(my_float, type(my_float)) # Output: 3.14 <class 'float'>
String
# String my_string = "Hello, world!" print(my_string, type(my_string)) # Output: Hello, world! <class 'str'>
List
# List my_list = [11, 22, 33, 44] print(my_list, type(my_list)) # Output: [11, 22, 33, 44] <class 'list'>
Tuple
# Tuple my_tuple = (44, 55, 66, 77) print(my_tuple, type(my_tuple)) # Output: (44, 55, 66, 77) <class 'tuple'>
Dictionary
# Dictionary my_dict = {'name': 'School247', 'location': India} print(my_dict, type(my_dict)) # Output: {'name': 'School247', 'location': India} <class 'dict'>
Set
# Set my_set = {77, 88, 99} print(my_set, type(my_set)) # Output: {88, 99, 77} <class 'set'>
Boolean
# Boolean my_bool = True print(my_bool, type(my_bool)) # Output: True <class 'bool'>
None
# None my_none = None print(my_none, type(my_none)) # Output: None <class 'NoneType'>
Bytes
# Bytearray my_bytearray = bytearray(b'world') print(my_bytearray, type(my_bytearray)) # Output: bytearray(b'world') <class 'bytearray'>
Frozenset
# Frozenset my_frozenset = frozenset({10, 11, 12}) print(my_frozenset, type(my_frozenset)) # Output: frozenset({10, 11, 12}) <class 'frozenset'>
Memoryview
# Memoryview my_memoryview = memoryview(b'example') print(my_memoryview, type(my_memoryview)) # Output: <memory at 0x7f72e328f780> <class 'memoryview'>