Class 11 Python: Data Types, Mutable and Immutable Data Types, Statements, and Expression Evaluation

Class 11 Python: Data Types, Mutable and Immutable Data Types, Statements, and Expression Evaluation


Understanding data types, mutability, statements, and how expressions are evaluated is fundamental in Python programming. Here's an overview of these concepts:

1. Data Types

Data types define the kind of value that can be stored and manipulated within a program. Python has several built-in data types, which can be broadly categorized into:

a. Numeric Types

  • Integer (int): Represents whole numbers, positive or negative, without decimals.
    • Example: 10, -5
  • Float (float): Represents real numbers with a fractional part (decimals).
    • Example: 3.14, -2.718
  • Complex (complex): Represents complex numbers with a real and an imaginary part.
    • Example: 3 + 5j, -1j

b. Sequence Types

  • String (str): Represents a sequence of characters enclosed in single, double, or triple quotes.
    • Example: "Hello, World!", 'Python'
  • List (list): Represents an ordered collection of items, which can be of different data types.
    • Example: [1, 2, 3, 'apple']
  • Tuple (tuple): Similar to a list, but immutable (cannot be changed after creation).
    • Example: (1, 2, 3, 'apple')

c. Boolean Type

  • Boolean (bool): Represents two values: True or False.
    • Example: True, False


2. Mutable and Immutable Data Types

In Python, data types are categorized based on whether their value can be modified after they are created.

a. Mutable Data Types

Mutable data types allow modifications to their content without changing their identity (i.e., they can be updated in place).

  • Examples of Mutable Data Types:
    • List: You can change, add, or remove elements.
      • Example: my_list = [1, 2, 3], my_list[0] = 10 changes the list to [10, 2, 3].
    • Dictionary: You can add, modify, or delete key-value pairs.
      • Example: my_dict = {'a': 1}, my_dict['b'] = 2 adds a new pair.

b. Immutable Data Types

Immutable data types do not allow modifications to their content. Any attempt to change the value will result in a new object being created.

  • Examples of Immutable Data Types:
    • String: Cannot change individual characters.
      • Example: my_str = "Hello", my_str[0] = "h" will cause an error.
    • Tuple: Cannot change, add, or remove elements.
      • Example: my_tuple = (1, 2, 3), my_tuple[0] = 10 will cause an error.
    • Numbers (int, float, complex): Any operation on numbers creates a new object.
      • Example: a = 5, a = a + 1 results in a pointing to a new object with value 6.

Yes, in Python, the number data type is indeed immutable. This means that once a number is created, its value cannot be changed. If you perform an operation on a number that results in a new value, a new object is created, and the original object remains unchanged.

Example:

a = 5 print("Original value of a:", a) # Output: 5 # Trying to change the value of 'a' a = a + 1 print("New value of a:", a) # Output: 6

Explanation:

  • Initially, a is assigned the value 5.
  • When you perform the operation a = a + 1, a new integer object with the value 6 is created.
  • a now points to this new object, but the original object 5 remains unchanged.

In Python, integers (int), floating-point numbers (float), and complex numbers (complex) are all immutable data types. Every time you perform an operation on these types, a new object is created if the result is different.


3. Statements

A statement is an instruction that Python interpreter can execute.

Types of Statements:

  • Expression Statement: Evaluates an expression and returns a value.
    • Example: print(5 + 3)
  • Assignment Statement: Assigns a value to a variable.
    • Example: x = 10
  • Control Flow Statement: Directs the order of execution of statements in a program (e.g., if, for, while).
    • Example: if x > 0: print("Positive")
  • Function Call Statement: Executes a function.
    • Example: greet("Alice")

4. Expression Evaluation

An expression is a combination of values, variables, operators, and function calls that Python interprets and evaluates to produce another value.

Examples of Expressions:

  • Arithmetic Expression: Combines numbers and arithmetic operators.
    • Example: 2 + 3 * 4 evaluates to 14.
  • Comparison Expression: Compares two values and returns a Boolean value.
    • Example: 10 > 5 evaluates to True.
  • Logical Expression: Combines Boolean values and logical operators.
    • Example: True and False evaluates to False.

Evaluation of Expressions:

  • Python follows the order of operations (PEMDAS: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction) when evaluating expressions.
    • Example: 3 + 2 * 2 evaluates to 7 (2 * 2 = 4, then 3 + 4 = 7).
  • Expressions can also involve function calls, which are evaluated before applying other operators.
    • Example: len("hello") + 3 evaluates to 8.

Summary

  • Data Types define the type of data that can be stored and manipulated.
  • Mutable vs Immutable: Mutable data types can be changed after creation, while immutable data types cannot.
  • Statements are instructions executed by the Python interpreter.
  • Expressions combine variables, values, and operators, and are evaluated to produce a result.

Post a Comment

0 Comments