Python Data Types

Python Data Types — Notes

1. Overview

  • A data type defines the kind of value a variable holds.
  • Python is dynamically typed — no need to declare the type explicitly.
  • Use type() to check a variable’s type:

    x = 10
    print(type(x))  # <class 'int'>
    

2. Basic Data Types

a) Numeric Types

  1. Integer (int)

    • Whole numbers (positive, negative, zero)
    • Example:

      a = 5
      b = -10
      
  2. Floating Point (float)

    • Numbers with decimal points
    • Example:

      pi = 3.14
      
  3. Complex Numbers (complex)

    • Numbers with a real and imaginary part
    • Example:

      c = 2 + 3j
      

b) Text Type

  • String (str)

    • Sequence of characters inside quotes
    • Example:

      name = "Python"
      

c) Boolean Type

  • Boolean (bool)

    • Represents True or False
    • Example:

      is_active = True
      

3. Collection Data Types

a) List

  • Ordered, changeable (mutable)
  • Allows duplicate values
  • Example:

    fruits = ["apple", "banana", "cherry"]
    

b) Tuple

  • Ordered, unchangeable (immutable)
  • Example:

    coordinates = (10.0, 20.0)
    

c) Set

  • Unordered, unique items
  • Example:

    unique_nums = {1, 2, 3}
    

d) Dictionary

  • Key-value pairs, unordered
  • Example:

    person = {"name": "Alice", "age": 25}
    

4. None Type

  • None represents absence of value
data = None

5. Type Conversion (Casting)

x = int("10")     # strint
y = float(5)      # intfloat
z = str(3.14)     # floatstr

6. Checking Type

print(type(5))           # <class 'int'>
print(isinstance(5, int))  # True


Previous Post Next Post