Python Data Types

Python, known for its simplicity and readability, is a versatile programming language widely used in various fields, from web development to data science. One of the foundational aspects of Python is its data types. Understanding data types is crucial because they determine the kind of operations that can be performed on data. In this section, we will explore the different data types in Python, including their characteristics and common use cases.

1. Numeric Types

a. Integer (int)

  • Description: Represents whole numbers without a fractional component.
  • Example: a = 10, b = -5
  • Operations: Addition (+), subtraction (-), multiplication (*), division (/), modulus (%), exponentiation (**), floor division (//).

b. Float (float)

  • Description: Represents numbers with a fractional component, stored as a decimal point.
  • Example: x = 3.14, y = -0.001
  • Operations: Same as integers but with floating-point arithmetic.

c. Complex (complex)

  • Description: Represents complex numbers with a real and imaginary part.
  • Example: z = 2 + 3j (where j is the imaginary unit)
  • Operations: Addition, subtraction, multiplication, division, and conjugation.

2. Sequence Types

a. String (str)

  • Description: Represents a sequence of Unicode characters.
  • Example: s = "Hello, World!", t = 'Python'
  • Operations: Concatenation (+), repetition (*), slicing (s[1:5]), membership testing ('H' in s).

b. List (list)

  • Description: Ordered, mutable sequence of items.
  • Example: lst = [1, 2, 3, "a", "b", "c"]
  • Operations: Indexing, slicing, appending (lst.append(4)), extending (lst.extend([5, 6])), removing (lst.remove(2)).

c. Tuple (tuple)

  • Description: Ordered, immutable sequence of items.
  • Example: tpl = (1, 2, 3, "a", "b", "c")
  • Operations: Indexing, slicing, concatenation, repetition, but not modification (immutable).

3. Mapping Type

a. Dictionary (dict)

  • Description: Unordered, mutable collection of key-value pairs.
  • Example: d = {"name": "Alice", "age": 25}
  • Operations: Accessing values (d["name"]), adding (d["address"] = "123 Main St"), removing (del d["age"]), membership testing ("name" in d).

4. Set Types

a. Set (set)

  • Description: Unordered collection of unique items.
  • Example: s = {1, 2, 3, 4}
  • Operations: Adding (s.add(5)), removing (s.remove(3)), union (s.union({4, 5, 6})), intersection (s.intersection({2, 3, 4})).

b. Frozen Set (frozenset)

  • Description: Immutable version of a set.
  • Example: fs = frozenset([1, 2, 3, 4])
  • Operations: Same as set, but immutable (no add or remove operations).

5. Boolean Type

a. Boolean (bool)

  • Description: Represents truth values.
  • Example: t = True, f = False
  • Operations: Logical operations (and, or, not).

6. None Type

a. NoneType

  • Description: Represents the absence of a value or a null value.
  • Example: x = None
  • Operations: Comparison (x is None), assignment.

Additional Points on Python Data Types

  • Type Conversion: Python provides functions to convert between types, such as int(), float(), str(), list(), tuple(), set(), and dict().
  • Mutable vs. Immutable:
  • Mutable types can be changed after creation (e.g., lists, dictionaries, sets).
  • Immutable types cannot be changed after creation (e.g., strings, tuples, frozensets).


Discover more from Learn with Anu Arora

Subscribe to get the latest posts sent to your email.

Leave a comment