Datatypes in python
In Python, data types define the type of a value or a variable. Python is dynamically typed, meaning you don’t need to declare the type of a variable when you create it. The interpreter infers the type based on the value assigned.
Here are the primary data types in Python:
1. Numeric Types
int (Integer): Represents whole numbers without a decimal point.
pythonx = 10 y = -5 z = 0
float (Floating-point number): Represents real numbers or numbers with a decimal point.
pythonpi = 3.14 temperature = -2.5
complex (Complex number): Represents numbers with a real and imaginary part.
pythona = 2 + 3j # real part is 2, imaginary part is 3 b = 1 - 4j
2. Text Type
- str (String): Represents sequences of characters (text). Strings can be enclosed in single quotes (
'
) or double quotes ("
).pythongreeting = "Hello, World!" name = 'Alice'
3. Sequence Types
list: An ordered, mutable collection of elements. Lists are enclosed in square brackets (
[]
), and elements can be of any type.pythonfruits = ['apple', 'banana', 'cherry'] numbers = [1, 2, 3, 4] mixed_list = [1, "apple", 3.14]
tuple: An ordered, immutable collection of elements. Tuples are similar to lists, but once created, you cannot modify them. Tuples are enclosed in parentheses (
()
).pythoncoordinates = (10, 20) colors = ('red', 'green', 'blue')
range: A special type of sequence used for generating a sequence of numbers, commonly used in loops.
pythonnumbers = range(5) # 0, 1, 2, 3, 4
4. Mapping Type
- dict (Dictionary): An unordered collection of key-value pairs. Dictionaries are enclosed in curly braces (
{}
), with keys and values separated by a colon (:
).pythonperson = {"name": "Alice", "age": 25, "city": "New York"}
5. Set Types
set: An unordered collection of unique elements. Sets are enclosed in curly braces (
{}
) or can be created using theset()
function. They do not allow duplicates.pythonunique_numbers = {1, 2, 3, 4} letters = set("apple")
frozenset: Similar to sets, but immutable. Once created, you cannot modify the elements in a
frozenset
.pythonfrozen_set = frozenset([1, 2, 3, 4])
6. Boolean Type
- bool: Represents one of two values:
True
orFalse
. It is used for conditional statements and logical operations.pythonis_active = True is_sunny = False
7. Binary Types
bytes: Immutable sequences of bytes, often used for binary data and file handling.
pythonbyte_data = b'hello'
bytearray: A mutable version of
bytes
. It allows modification of byte sequences.pythonbyte_array = bytearray([65, 66, 67])
memoryview: A view object that allows you to access and manipulate the underlying data of byte arrays without copying the data.
pythonmem_view = memoryview(byte_array)
8. None Type
- None: A special type representing the absence of a value or a null value.python
result = None
Example Usage of Data Types:
# Integer
age = 30
# Float
height = 5.9
# String
name = "Alice"
# List
colors = ["red", "green", "blue"]
# Tuple
coordinates = (10, 20)
# Dictionary
person = {"name": "Alice", "age": 30, "city": "New York"}
# Set
fruits = {"apple", "banana", "cherry"}
# Boolean
is_sunny = True
# None
status = None
# Complex number
number = 3 + 4j
Type Conversion (Casting):
You can convert between different data types in Python using functions like int()
, float()
, str()
, etc.
# Convert float to integer
x = 3.14
y = int(x) # y = 3
# Convert string to integer
s = "100"
n = int(s) # n = 100
Type Checking:
You can check the type of a variable using the type()
function.
x = 10
print(type(x)) # Output: <class 'int'>
y = "Hello"
print(type(y)) # Output: <class 'str'>
Understanding these basic data types is essential for writing efficient and readable Python code.