Data Handling in Python Class-XI

Data Handling in Python

1. Data Types in Python

Python supports various built-in data types to categorize and store different forms of data:

  • Number: Used to store numerical values.
    • Integer (int): Whole numbers without decimals (e.g., -5, 0, 42).
    • Floating Point (float): Numbers with decimal points (e.g., 3.14, -0.5).
    • Complex (complex): Numbers with real and imaginary parts (e.g., 3 + 4j).
  • Boolean (bool): Represents truth values: True or False.
  • Sequence: Ordered collection of items.
    • String (str): An immutable sequence of characters enclosed in quotes (e.g., "Hello").
    • List (list): An ordered, mutable collection of items enclosed in square brackets [] (e.g., [1, "apple", 3.5]).
    • Tuple (tuple): An ordered, immutable collection of items enclosed in parentheses () (e.g., (10, 20, 30)).
  • None (NoneType): A special data type representing the absence of a value or a null value (None).
  • Mapping: Unordered collection of key-value pairs.
    • Dictionary (dict): A mutable mapping enclosed in curly braces {} with key-value pairs (e.g., {"name": "Alice", "age": 20}).

Mutable vs. Immutable Data Types

  • Mutable Data Types: Objects whose values/content can be modified after creation.
    • Examples: list, dict, set.
  • Immutable Data Types: Objects whose values/content cannot be changed once created. Any modification creates a new object in memory.
    • Examples: int, float, complex, bool, str, tuple.

2. Expressions

An expression is a valid combination of variables, constants, operators, and function calls that the Python interpreter evaluates to produce a single value.

Example:

result = 5 + 3 * 2  # The right side, "5 + 3 * 2", is an expression.
name = "Alice"      # Even a single value is an expression.

3. Types of Expressions

Expressions are categorized based on the operators and operands involved:

  1. Arithmetic Expressions: Use arithmetic operators (+, -, *, /, %, //, **).
    (a + b) * (c - d)
  2. Relational (Comparison) Expressions: Use relational operators (==, !=, >, <, >=, <=). They evaluate to True or False.
    age >= 18:
  3. Logical Expressions: Use logical operators (and, or, not) to combine relational expressions. marks > 33 and attendance > 75
  4. Assignment Expressions: Use the assignment operator (=) or compound operators (+=, -=, etc.). Pythontotal = 100
    count += 1
  5. String Expressions: Use string operators (+ for concatenation, * for repetition).
    name = "India"
    "Hello " + name

4. Operator Precedence

Operator precedence defines the evaluation order in an expression—operators with higher precedence are evaluated first.

PrecedenceOperatorDescription
Highest()Parentheses
**Exponentiation
+x, -x, ~xUnary plus, unary minus, and bitwise NOT
*, /, //, %Multiplication, division, floor division, modulus
+, -Addition and subtraction
<<, >>Bitwise left and right shifts
&Bitwise AND
^Bitwise XOR
|Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not inComparisons, identity, and membership operators
notLogical NOT
andLogical AND
LowestorLogical OR

Rule of Thumb: When in doubt, use parentheses () to force the desired evaluation order.

Evaluation Example:

result = 5 + 3 * 2 ** 2
# Step 1: 2 ** 2 = 4    (Exponentiation first)
# Step 2: 3 * 4 = 12    (Multiplication next)
# Step 3: 5 + 12 = 17   (Addition last)
print(result)           # Output: 17

5. Operator Associativity

When multiple operators of the same precedence appear in an expression, associativity determines the direction of evaluation:

  • Left-to-Right Associativity: Applied to most operators (e.g., +, -, *, /, %, //).
    • 10 - 5 - 2 is evaluated as (10 - 5) - 2 = 3.
  • Right-to-Left Associativity: Applied to Assignment (=) and Exponentiation (**).
    • a = b = 5 is evaluated as b = 5, then a = b.
    • 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2) = 2 ** 9 = 512.

6. Typecasting

The process of converting a variable from one data type to another.

  • Implicit Typecasting: Python automatically converts a lower data type (integer) to a higher data type (float) to prevent data loss.
    a = 5 # int
    b = 2.5 # float
    c = a + b # c becomes 7.5 (float)
  • Explicit Typecasting: Manually converting data types using built-in functions such as int(), float(), and str().
    num_str = "123"
    num_int = int(num_str) # Converts string "123" to integer 123 number = 100
    text = "The number is " + str(number) # Converts integer to string for concatenation

7. Standard Library Modules

Python’s standard library is a collection of pre-written modules providing common functionality. You must import a module before using it:

import module_name
# OR
from module_name import function_name

Key Modules:

1. Math Module (import math)

  • math.sqrt(x): Returns the square root of x.
  • math.pow(x, y): Returns xy (x raised to power y).
  • math.ceil(x): Returns the smallest integer >= x
  • math.floor(x): Returns the largest integer <= x
  • math.fabs(x): Returns the absolute value of x.
  • math.pi: Constant value of π ≈ 3.14159…

2. Statistics Module (import statistics)

  • statistics.mean(data): Calculates the arithmetic mean (average).
  • statistics.median(data): Finds the middle value.
  • statistics.mode(data): Finds the most frequent value.
  • statistics.stdev(data): Calculates the standard deviation.

3. Random Module (import random)

  • random.random(): Returns a random float in the range $[0.0, 1.0)$.
  • random.randint(a, b): Returns a random integer N such that a<=1 N <=b
  • random.randrange(a, b): Returns a random integer $N$ such that a<=1 N <b
  • random.choice(sequence): Returns a random element from a non-empty sequence.
  • random.shuffle(sequence): Shuffles a sequence in place.

8. Debugging and Errors

Debugging

The process of identifying and removing errors (bugs) from a program.

  • Code Reading: Manually tracing execution line by line.
  • Print Statement Debugging: Inserting print() calls to inspect variable values.
  • Using a Debugger: Setting breakpoints and stepping through code in IDEs (like IDLE, PyCharm, or VS Code).

Types of Program Errors

A. Compile-Time Errors

Detected by the Python interpreter during parsing before the code executes:

  1. Syntax Error: Violations of syntax rules (e.g., missing colons, unbalanced parentheses).
    if x == 5 # SyntaxError: Missing colon ':'
    print("Hello World" # SyntaxError: Unclosed parenthesis
  2. Semantic Error: Code is syntactically valid but fails structural semantics
    a + = 5 # SyntaxError: Invalid syntax (space between + and =)

B. Runtime Errors (Exceptions)

Errors occurring while the program is running.

  • ZeroDivisionError: Division by zero.
  • ValueError: Invalid argument value, e.g., int("abc").
  • NameError: Referencing an undefined variable.
  • IndexError: List index out of range.
  • FileNotFoundError: Attempting to access a non-existent file.

C. Logical Errors

The program executes without crashing but yields incorrect outputs due to algorithmic or logic flaws. Python does not detect logical errors.

# Program to calculate average of two numbers
a = 10
b = 20
average = a + b / 2  # Logical Error! Should be (a + b) / 2
print(average)       # Output: 20.0 (Incorrect; should be 15.0)

9. Exception Handling

Runtime errors raise Exception Objects Unhandled exceptions crash the program. Use try...except blocks to handle exceptions gracefully:

try:
    num = int(input("Enter a number: "))
    result = 10 / num
    print(f"Result is {result}")
except ZeroDivisionError:
    print("You cannot divide by zero!")
except ValueError:
    print("That's not a valid number!")

Important notes:

What is the difference between the statements print(5 & 6) and print(5 and 6)?

This is one of the most common tripping hazards in programming! While & and and look like they do the same thing, they actually have completely different purposes and mechanics.

Here is the short answer:

  • 5 & 6 outputs 4 because it performs a Bitwise AND (comparing the numbers bit-by-bit).
  • 5 and 6 outputs 6 because it performs a Logical AND (evaluating truth values and using short-circuit logic).

1. print(5 & 6) Outputs 4

The ampersand (&) is the bitwise operator. It doesn’t look at 5 and 6 as whole numbers; it looks under the hood at their binary representation (0s and 1s) and aligns them.

It returns a 1 only if both aligned bits are 1.

Plaintext

  0 1 0 1  (This is 5 in binary)
& 0 1 1 0  (This is 6 in binary)
  -------
  0 1 0 0  (This is 4 in binary)

Because only the second column has a 1 in both numbers, the result is 0100, which translates back to the integer 4.

2. print(5 and 6) Outputs 6

The keyword and is a logical (or boolean) operator. In Python, it doesn’t look at bits at all. Instead, it checks whether the values are “truthy” or “falsy”.

  • In Python, 0 is considered False, and any non-zero number is considered True.
  • Therefore, both 5 and 6 are evaluated as True.

How Python’s and evaluates:

Python uses short-circuit evaluation for logical operators. For an and expression:

  1. It looks at the first value (5). Since 5 is True, it must check the second value to see if the whole statement is true.
  2. It moves to the second value (6).
  3. Python’s and operator actually returns the last value it evaluated to determine the truth. Because it had to check 6 to confirm the expression was true, it simply returns 6.

Note: If you run print(0 and 6), it would output 0. Because 0 is False, Python’s short-circuit immediately returns 0 without even looking at the 6.

Quick Summary

OperatorTypeWhat it looks atRule / Behavior
&BitwiseBinary bits (0s and 1s)Returns 1 only if both bits are 1.
andLogicalTruth values (True / False)Returns the first expression if it’s Falsy, otherwise returns the second expression.

Leave a Comment

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

Scroll to Top