Skip to content

Instantly share code, notes, and snippets.

@raj1rana
Created January 18, 2025 16:20
Show Gist options
  • Save raj1rana/23d73a125ec06c53b7476cfc8899de4d to your computer and use it in GitHub Desktop.
Save raj1rana/23d73a125ec06c53b7476cfc8899de4d to your computer and use it in GitHub Desktop.

Here is a comprehensive list of Python data types along with examples for each:

1. Numeric Types

  • int (Integer): Whole numbers
    • Example: x = 10
  • float (Floating Point): Numbers with decimal points
    • Example: y = 3.14
  • complex (Complex Numbers): Numbers with real and imaginary parts
    • Example: z = 2 + 3j

2. Sequence Types

  • list: Ordered, mutable collection
    • Example: fruits = ["apple", "banana", "cherry"]
  • tuple: Ordered, immutable collection
    • Example: coordinates = (4, 5, 6)
  • range: Sequence of numbers
    • Example: numbers = range(5) (produces 0, 1, 2, 3, 4)

3. Text Type

  • str (String): Sequence of characters
    • Example: greeting = "Hello, World!"

4. Set Types

  • set: Unordered, mutable collection of unique items
    • Example: unique_numbers = {1, 2, 3, 3} (results in {1, 2, 3})
  • frozenset: Unordered, immutable collection of unique items
    • Example: frozen_set = frozenset([1, 2, 3, 3]) (results in frozenset({1, 2, 3}))

5. Mapping Type

  • dict (Dictionary): Collection of key-value pairs
    • Example: person = {"name": "Alice", "age": 25}

6. Boolean Type

  • bool: Represents True or False
    • Example: is_active = True

7. Binary Types

  • bytes: Immutable sequence of bytes
    • Example: b = b"hello"
  • bytearray: Mutable sequence of bytes
    • Example: ba = bytearray(b"hello")
  • memoryview: Memory view object
    • Example: mv = memoryview(b"hello")

8. None Type

  • NoneType: Represents a null value
    • Example: x = None

Additional Examples

  • Type Conversion: Python provides functions like int(), float(), str(), etc., to convert between types.
    • Example: x = int("10") converts the string "10" to an integer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment