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).
- Integer (
- Boolean (
bool): Represents truth values:TrueorFalse. - 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)).
- String (
- 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}).
- Dictionary (
Mutable vs. Immutable Data Types
- Mutable Data Types: Objects whose values/content can be modified after creation.
- Examples:
list,dict,set.
- Examples:
- 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.
- Examples:
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:
- Arithmetic Expressions: Use arithmetic operators (
+,-,*,/,%,//,**).(a + b) * (c - d) - Relational (Comparison) Expressions: Use relational operators (
==,!=,>,<,>=,<=). They evaluate toTrueorFalse.age >= 18: - Logical Expressions: Use logical operators (
and,or,not) to combine relational expressions.marks > 33 and attendance > 75 - Assignment Expressions: Use the assignment operator (
=) or compound operators (+=,-=, etc.). Pythontotal = 100count += 1 - 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.
| Precedence | Operator | Description |
| Highest | () | Parentheses |
** | Exponentiation | |
+x, -x, ~x | Unary 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 in | Comparisons, identity, and membership operators | |
not | Logical NOT | |
and | Logical AND | |
| Lowest | or | Logical 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 - 2is evaluated as(10 - 5) - 2=3.
- Right-to-Left Associativity: Applied to Assignment (
=) and Exponentiation (**).a = b = 5is evaluated asb = 5, thena = b.2 ** 3 ** 2is evaluated as2 ** (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 # intb = 2.5 # floatc = a + b # c becomes 7.5 (float) - Explicit Typecasting: Manually converting data types using built-in functions such as
int(),float(), andstr().num_str = "123"num_int = int(num_str) # Converts string "123" to integer 123 number = 100text = "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 ofx.math.pow(x, y): Returns xy (xraised to powery).math.ceil(x): Returns the smallest integer >= xmath.floor(x): Returns the largest integer <= xmath.fabs(x): Returns the absolute value ofx.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 <=brandom.randrange(a, b): Returns a random integer $N$ such that a<=1 N <brandom.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:
- Syntax Error: Violations of syntax rules (e.g., missing colons, unbalanced parentheses).
if x == 5 # SyntaxError: Missing colon ':'
print("Hello World" # SyntaxError: Unclosed parenthesis - 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 & 6outputs4because it performs a Bitwise AND (comparing the numbers bit-by-bit).5 and 6outputs6because 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,
0is consideredFalse, and any non-zero number is consideredTrue. - Therefore, both
5and6are evaluated asTrue.
How Python’s and evaluates:
Python uses short-circuit evaluation for logical operators. For an and expression:
- It looks at the first value (
5). Since5isTrue, it must check the second value to see if the whole statement is true. - It moves to the second value (
6). - Python’s
andoperator actually returns the last value it evaluated to determine the truth. Because it had to check6to confirm the expression was true, it simply returns6.
Note: If you run
print(0 and 6), it would output0. Because0isFalse, Python’s short-circuit immediately returns0without even looking at the6.
Quick Summary
| Operator | Type | What it looks at | Rule / Behavior |
& | Bitwise | Binary bits (0s and 1s) | Returns 1 only if both bits are 1. |
and | Logical | Truth values (True / False) | Returns the first expression if it’s Falsy, otherwise returns the second expression. |