Data Types in Python
Python is a dynamically typed language, meaning you don't need to declare the type of a variable
when assigning a value to it. The interpreter automatically binds the value to its type. Here are
the main data types in Python:
Numeric Data Types
1. Integer (int): Represents whole numbers, positive or negative, without decimals.
Example: 10, -20.
2. Float (float): Represents real numbers with a floating-point representation. Example:
1.9, 15.2.
3. Complex (complex): Represents complex numbers in the form x + yj, where x is the
real part and y is the imaginary part. Example: 2.14j, 2.0 + 2.3j.
a = 5
b = 5.0
c = 2 + 4j
print(type(a)) # <class 'int'>
print(type(b)) # <class 'float'>
print(type(c)) # <class 'complex'>
Sequence Data Types
1. String (str): A sequence of characters enclosed in single, double, or triple quotes.
Example: "hello", 'world'.
2. List (list): An ordered collection of items, which can be of different types, enclosed in
square brackets. Example: [1, 'hi', 3.5].
3. Tuple (tuple): Similar to lists but immutable, meaning their values cannot be changed
after creation. Enclosed in parentheses. Example: (1, 'hi', 3.5).
# String
s = "Hello, Python"
print(type(s)) # <class 'str'>
# List
l = [1, 'hi', 3.5]
print(type(l)) # <class 'list'>
# Tuple
t = (1, 'hi', 3.5)
print(type(t)) # <class 'tuple'>
Boolean Data Type
Boolean (bool): Represents one of two values: True or False. Used to determine the truth value
of an expression.
x = True
y = False
print(type(x)) # <class 'bool'>
print(type(y)) # <class 'bool'>
Set Data Type
Set (set): An unordered collection of unique items. Created using curly braces or the set()
function. Example: {1, 2, 3}, set([1, 2, 3]).
s = {1, 2, 3}
print(type(s)) # <class 'set'>
Dictionary Data Type
Dictionary (dict): An unordered collection of key-value pairs. Keys must be unique and
immutable. Created using curly braces with key-value pairs separated by colons. Example:
{'name': 'John', 'age': 30}.
d = {'name': 'John', 'age': 30}
print(type(d)) # <class 'dict'>