Chapter 2: Variables and data types

In Python, variables are used to store data. They act as placeholders that allow you to refer to values by a name. Here are the key points about Python variables and data types:

    Variable Assignment:


  • To assign a value to a variable, you use the assignment operator (=).
  • The variable name should follow the rules of Python naming conventions (e.g., no spaces, start with a letter or underscore, case-sensitive).

  • Variable Naming:

    1. Variable names should be descriptive, meaningful, and follow Python's naming conventions.
    2. They can contain letters, digits, and underscores.
    3. Variable names are case-sensitive.

  • Introduction to Data Types: Python supports several built-in data types:

    • Numeric types:

      • Integers (int): Whole numbers without a fractional part, e.g., 10, -5, 0.
      • Floating-point numbers (float): Real numbers with a decimal point, e.g., 3.14, -2.5.
    • Text type:

      • Strings (str): Ordered sequences of characters enclosed in single or double quotes, e.g., "Hello", 'Python'.
    • Boolean type:

      • Booleans (bool): Represents the truth values True or False.
    • Sequence types:

      • Lists (list): Ordered, mutable collections of items, enclosed in square brackets [], e.g., [1, 2, 3].
      • Tuples (tuple): Ordered, immutable collections of items, enclosed in parentheses (), e.g., (1, 2, 3).
    • Mapping type:

      • Dictionaries (dict): Unordered collections of key-value pairs, enclosed in curly braces {}, e.g., {"name": "John", "age": 25}.
    • Other types:

      • Sets (set): Unordered collections of unique items, enclosed in curly braces {}, e.g., {1, 2, 3}.
      • None: A special type representing the absence of a value.

      • Example:

      • # Numeric types age = 25 price = 9.99 # Text type name = "John Doe" # Boolean type is_student = True # Lists numbers = [1, 2, 3] fruits = ['apple', 'banana', 'orange'] # Tuples coordinates = (10, 20) # Dictionaries person = {'name': 'John', 'age': 30}


Comments