In Python, data types define the type of value a variable can hold. Python is a dynamically typed language, meaning you don’t have to declare a variable’s type explicitly—it is determined automatically based on the assigned value.

Python has the following built-in data types:

Category
Data Types
Example
Numeric Types
int, float, complex
10, 3.14, 5+2j
Sequence Types
str, list, tuple, range
"Hello", [1,2,3], (4,5,6), range(10)
Set Types
set, frozenset
{1,2,3}, frozenset({1,2})
Mapping Type
dict
{"name": "Nitin", "age": 25}
Boolean Type
bool
True, False
Binary Types
bytes, bytearray, memoryview
b"hello"

1. Integers and Floats (Numeric Types)

a) Integers (int)

  • Whole numbers, positive or negative, without decimals.
  • Example:
    x = 10  # Integer
    y = -5  # Negative Integer
    print(type(x))  # Output: <class 'int'>
    

b) Floating-Point Numbers (float)

  • Numbers with decimal points.
  • Example:
    pi = 3.14159  # Float
    weight = -45.6
    print(type(pi))  # Output: <class 'float'>
    

c) Type Conversion (intfloat)

  • Convert an integer to a float and vice versa:
    a = 10
    b = float(a)  # Converts 10 to 10.0
    c = int(3.99)  # Converts 3.99 to 3 (truncates decimal)
    

2. Strings (str)

  • Strings are sequences of characters enclosed in single ('), double ("), or triple (''') quotes.
  • Example:
    name = "Nitin Prajapati"
    greeting = 'Hello, World!'
    paragraph = """This is a 
    multi-line string"""
    print(type(name))  # Output: <class 'str'>
    

String Operations

text = "Python"

# Concatenation
print(text + " is awesome!")  # Output: Python is awesome!

# Repetition
print(text * 3)  # Output: PythonPythonPython

# Indexing
print(text[0])  # Output: P

# Slicing
print(text[1:4])  # Output: yth

# String Length
print(len(text))  # Output: 6

3. Lists (list)

  • Lists are ordered, mutable (changeable) collections of elements.
  • Can contain different types: int, float, str, etc.
  • Defined using square brackets ([]).

List Example

numbers = [10, 20, 30, 40]
mixed_list = [1, "Hello", 3.14]

# Accessing elements
print(numbers[0])  # Output: 10

# Modifying lists
numbers.append(50)  # Adds 50 to the end
numbers[1] = 25  # Modifies the second element

# List slicing
print(numbers[1:3])  # Output: [25, 30]

List Methods

numbers = [5, 3, 8, 1]

numbers.append(10)   # Add 10 to the end
numbers.insert(2, 15)  # Insert 15 at index 2
numbers.remove(3)   # Remove 3 from the list
numbers.sort()  # Sorts the list
numbers.reverse()  # Reverses the list

print(numbers)  # Output: [10, 8, 5, 1]

4. Tuples (tuple)

  • Tuples are ordered, immutable (unchangeable) collections of elements.
  • Defined using parentheses (()).

Tuple Example

coordinates = (10.5, 20.3)
colors = ("red", "green", "blue")

# Accessing elements
print(colors[1])  # Output: green

# Tuples are immutable
# colors[1] = "yellow"  # ❌ This will cause an error

Tuple Packing & Unpacking

point = (4, 5)
x, y = point  # Unpacking
print(x)  # Output: 4
print(y)  # Output: 5

5. Dictionaries (dict)

  • Dictionaries are unordered key-value pairs.
  • Defined using curly braces ({}).

Dictionary Example

person = {
    "name": "Nitin",
    "age": 25,
    "city": "Delhi"
}

# Accessing values
print(person["name"])  # Output: Nitin

# Modifying dictionary
person["age"] = 26

# Adding a new key-value pair
person["job"] = "Software Engineer"

# Deleting a key
del person["city"]

print(person)  # Output: {'name': 'Nitin', 'age': 26, 'job': 'Software Engineer'}

Dictionary Methods

print(person.keys())  # Get all keys
print(person.values())  # Get all values
print(person.items())  # Get key-value pairs

6. Sets (set)

  • Sets are unordered, mutable collections of unique elements.
  • Defined using curly braces ({}).
  • Duplicate values are automatically removed.

Set Example

fruits = {"apple", "banana", "cherry", "apple"}

print(fruits)  # Output: {'banana', 'cherry', 'apple'} (removes duplicates)

Set Operations

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

print(A | B)  # Union: {1, 2, 3, 4, 5, 6}
print(A & B)  # Intersection: {3, 4}
print(A - B)  # Difference: {1, 2}

Summary Table of Python Data Types

Data Type
Mutable?
Ordered?
Duplicates Allowed?
Example
int
❌ No
N/A
✅ Yes
10
float
❌ No
N/A
✅ Yes
3.14
str
❌ No
✅ Yes
✅ Yes
"Hello"
list
✅ Yes
✅ Yes
✅ Yes
[1, 2, 3]
tuple
❌ No
✅ Yes
✅ Yes
(1, 2, 3)
dict
✅ Yes
✅ Yes (Python 3.7+)
❌ No (Unique keys)
{"name": "Nitin"}
set
✅ Yes
❌ No
❌ No
{1, 2, 3}

Conclusion

Python provides various built-in data types to handle different kinds of data. Understanding these types is essential for writing efficient Python programs.