Data Types in Python
What are the Data Types?
Data types in Python are used to specify the type of a variable or the type of the value that a variable holds.
Data types play an important role in Python because they determine how a value can be used or manipulated.
For example you can perform mathematical operations such as addition and subtraction on integers, but not on strings.
Additionally, different data types have different methods and properties that can be used on them.
It's worth nothing that Python is a dynamically-typed language, which means that the data type of a variable can change at runtime.
Data types in Python are used to define the type of data that can be stored in a variable and specify how the data can be used or manipulated.
Understanding data types ensures that data is collected in the preferred format and the value of each property is as expected.
There are several bult-in data types in Python, including:
- integers (int)
- floating-point numbers (float)
- strings (str)
- booleans (bool)
- lists (list)
- tuples (tuple)
- sets (set)
- dictionaries (dict)
- and others such as complex numbers (complex), bytes (bytes), bytearrays (bytearray), etc.
Python Data Types are distributed as follows:
- Numeric data types: int, float, complex.
- Sequence types: string, list, tuple.
- Boolean type: bool
- Mapping data type: dict.
- Binary types: bytes, bytearray.
- Set data types: set, frozenset.
How to identify Data Types?
You can identify the data type of any object by using type() function.
Here we can see some examples of type() function.
x = "Hello World"
print(type(x))
//Output
str
x = 5
print(type(x))
//Output
int
x = 2.3
print(type(x))
//Output
float
x = [3, 5, 7]
print(type(x))
//Output
list
x = {"name : "viraj",
"age" : 19
}
print(type(x))
//Output
dict
No comments:
Post a Comment