MSBTE Python Programming - Comprehensive Revision Notes
1. Features and Basics of Python
Python is a high-level, interpreted, general-purpose language known for its readability and simplicity.
- Interpreted & Interactive: Code is executed line-by-line. Interactive shell available.
- Portable & Open Source: Works across platforms. Free to use and modify.
- Extensible & Embeddable: Integrate with C/C++ or other languages.
- Object-Oriented: Supports class and object-based design.
- Large Standard Library: Includes modules for OS, math, file I/O, web, etc.
2. Python Data Types
- Numeric: int, float, complex (e.g., 10, 3.14, 2+3j)
- Text: str ('Hello')
- Boolean: bool (True/False)
- List: [1, 'a', 3.5] (mutable)
- Tuple: (1, 'a', 3.5) (immutable)
- Set: {1, 2, 3} (unique items)
- Dictionary: {'name': 'Alex', 'age': 20}
3. Operators in Python
- Arithmetic: +, -, *, /, %, **, //
- Comparison: ==, !=, >, <, >=, <=
- Assignment: =, +=, -=, *=, etc.
- Logical: and, or, not
- Bitwise: &, |, ^, ~, <<, >>
- Membership: in, not in (e.g., 'a' in list)
- Identity: is, is not (memory comparison)
4. Control Flow
- if, if-else, if-elif-else for decision-making.
- Loops:
* for: Iterates over a sequence.
* while: Repeats while condition is True.
- break: exits loop prematurely.
- continue: skips current iteration.
5. Functions and Lambda
- Define using def: def greet():
- Parameters and Return: def add(a, b): return a + b
- Lambda (anonymous): add = lambda a,b: a+b
- Built-in: len(), sum(), max(), min(), abs(), round(), type(), id()
6. Lists and Tuples
- List: Mutable, ordered, supports duplicates. Methods: append(), pop(), remove(), sort(), reverse(), insert().
- Tuple: Immutable, ordered. Used for fixed data.
MSBTE Python Programming - Comprehensive Revision Notes
- Access: list[0], list[-1], list[1:4]
7. Sets and Dictionaries
- Set: Unordered, unique values. Methods: add(), remove(), union(), intersection(), difference().
- Dictionary: Key-value pairs. Methods: keys(), values(), items(), get(), update(), pop()
8. Object-Oriented Programming
- Class: Blueprint for objects.
- Object: Instance of class.
- Constructor: __init__ method for initialization.
- Self: Refers to current object.
- Concepts: Inheritance (reuse), Encapsulation (hiding data), Polymorphism (many forms), Abstraction (interface).
9. File Handling
- Modes: 'r', 'w', 'a', 'r+', 'w+', 'a+' for read, write, append.
- Functions: open(), read(), readline(), write(), close()
- Example:
f = open('file.txt', 'r')
content = f.read()
f.close()
10. Exception Handling
- Use try-except-finally to handle runtime errors.
- Example:
try:
a = 10 / 0
except ZeroDivisionError:
print('Cannot divide by zero')
finally:
print('Done')
11. Modules and Packages
- Module: Python file with functions/classes.
- Import: import math, from math import sqrt
- Package: Folder with __init__.py containing modules.
- Example: from pkg import mod1
mod1.func()
12. File & Directory Methods
- Directories: Use os.mkdir(), os.rmdir(), os.getcwd()
- File: Use os.remove(), os.rename()
MSBTE Python Programming - Comprehensive Revision Notes
13. Miscellaneous Topics
- Data conversion: int(), float(), str(), list(), tuple(), dict(), set()
- Namespace: Isolated scope containing bindings of names to objects.
- Comments: # for single line, ''' ''' or """ """ for multiline.
- Indentation: Mandatory for blocks (typically 4 spaces).