Data Types in python
Built-in Data Types
In Python, every value has a data type. A data type tells Python what kind of data a variable contains and what operations can be performed on it.
Python has several built-in data types.
| Text Type: |
|
| Numeric Types: |
, ,
|
| Sequence Types: |
, ,
|
| Mapping Type: |
|
| Set Types: |
,
|
| Boolean Type: |
|
| Binary Types: |
, ,
|
| None Type: |
|
Why Are Data Types Important?
Data types help Python understand how to store and process information.
For example:
name = "John"
age = 25
Here:
-
"John"is text (string) -
25is a number (integer)
Python automatically determines the data type when you assign a value to a variable.
Checking the Data Type
You can use the type() function to check the data type of any variable.
name = "John"
print(type(name))
Output:
<class 'str'>
Let's look at each type one by one.
String (str)
Strings are used to store text.
Example
name = "Alice"print(type(name))
Output:
<class 'str'>
Strings can use single or double quotes.
name = 'Alice'
name = "Alice"
Both are valid.
Integer (int)
Integers are whole numbers without decimal points.
Example
age = 25
print(type(age))
Output:
<class 'int'>
Float (float)
Floats are numbers that contain decimal points.
Example
price = 19.99
print(type(price))
Output:
<class 'float'>
Complex Numbers (complex)
Complex numbers contain a real and an imaginary part.
Example
x = 3 + 4j
print(type(x))
Output:
<class 'complex'>
Complex numbers are commonly used in advanced mathematics and engineering.
Boolean (bool)
Boolean values represent either True or False.
Example
is_logged_in = True
print(type(is_logged_in))
Output:
<class 'bool'>
Booleans are often used in conditions and comparisons.
List (list)
A list is an ordered collection of items.
Lists can be modified after creation.
Example
fruits = ["Apple", "Banana", "Mango"]
print(type(fruits))
Output:
<class 'list'>
Lists can contain different data types:
data = ["John", 25, True]
Tuple (tuple)
A tuple is similar to a list, but it cannot be changed after creation.
Example
colors = ("Red", "Green", "Blue")
print(type(colors))
Output:
<class 'tuple'>
Use tuples when data should remain fixed.
Range (range)
The range() function generates a sequence of numbers.
Example
numbers = range(5)
print(type(numbers))
Output:
<class 'range'>
This creates:
0, 1, 2, 3, 4
Dictionary (dict)
A dictionary stores data in key-value pairs.
Example
student = { "name": "John", "age": 20 }
print(type(student))
Output:
<class 'dict'>
You can access values using keys:
print(student["name"])
Output:
John
Set (set)
A set is an unordered collection of unique values.
Example
numbers = {1, 2, 3, 4}
print(type(numbers))
Output:
<class 'set'>
Sets automatically remove duplicate values.
numbers = {1, 1, 2, 3}
Result:
{1, 2, 3}
Frozen Set (frozenset)
A frozenset is an immutable version of a set.
Example
numbers = frozenset([1, 2, 3])
print(type(numbers))
Output:
<class 'frozenset'>
Once created, it cannot be modified.
Bytes (bytes)
Bytes are used to store binary data.
Example
data = b"Hello"
print(type(data))
Output:
<class 'bytes'>
Bytearray (bytearray)
A bytearray is similar to bytes but can be modified.
Example
data = bytearray(5)
print(type(data))
Output:
<class 'bytearray'>
Memoryview (memoryview)
A memoryview provides access to the internal memory of an object without copying it.
Example
data = memoryview(bytes(5))
print(type(data))
Output:
<class 'memoryview'>
This type is mostly used in advanced applications.
NoneType
The value None represents the absence of a value.
Example
x = None
print(type(x))
Output:
<class 'NoneType'>
None is commonly used as a placeholder value.
Type Conversion
You can convert one data type into another using Python's built-in functions.
Convert Integer to String
age = 25 text = str(age)
Convert String to Integer
number = int("100")
Convert Integer to Float
price = float(10)
Convert Float to Integer
price = int(10.99)