Python Data Types

Python supports several built-in data types that are fundamental to programming and data manipulation. Here are the main data types in Python:

  1. Numeric Types:
  • int: Integer values, e.g., 10, -3, 1000.
  • float: Floating-point values, e.g., 3.14, -0.001, 2.0.
  1. Boolean Type:
  • bool: Represents truth values, True or False.
  1. Sequence Types:
  • str: String type, used for text data enclosed in quotes, e.g., 'Hello', "Python".
  • list: Ordered collection of items, mutable (modifiable), e.g., [1, 2, 3], ['a', 'b', 'c'].
  • tuple: Ordered collection of items, immutable (cannot be modified once created), e.g., (1, 2, 3), ('x', 'y', 'z').
  1. Set Types:
  • set: Unordered collection of unique items, mutable, e.g., {1, 2, 3}, {'a', 'b', 'c'}.
  • frozenset: Immutable set, e.g., frozenset({1, 2, 3}).
  1. Mapping Type:
  • dict: Collection of key-value pairs, mutable, e.g., {'name': 'John', 'age': 30}, {1: 'one', 2: 'two'}.
  1. None Type:
  • NoneType: Represents the absence of a value, denoted by None.

Examples and Usage:

  • Numeric Types:
  num1 = 10       # int
  num2 = 3.14     # float
  • Boolean Type:
  is_valid = True
  is_active = False
  • Sequence Types:
  name = 'Alice'  # str
  numbers = [1, 2, 3]  # list
  coordinates = (4, 5)  # tuple
  • Set Types:
  fruits = {'apple', 'banana', 'orange'}  # set
  frozen_numbers = frozenset({1, 2, 3})   # frozenset
  • Mapping Type:
  person = {'name': 'Bob', 'age': 25}  # dict
  • None Type:
  result = None

Key Points:

  • Python is dynamically typed, meaning you don’t need to explicitly declare variables’ types.
  • Variables in Python can change type over their lifetime.
  • Understanding and correctly using these data types is crucial for effective programming and data manipulation in Python.

These data types form the core foundation of Python’s versatility and are used extensively in various applications, from basic scripting to complex data analysis and web development.

Leave a Reply

Your email address will not be published. Required fields are marked *