Data Analysis Using Python
1. Python Data Structures
- Tuple - Immutable collection. Example: t = (1, 2, 3)
- List - Mutable collection. Example: l = [1, 2, 3]
- Dictionary - Key-value pairs. Example: d = {'a': 1, 'b': 2}
- Set - Unordered collection of unique items. Example: s = {1, 2, 3}
- List Comprehension - Compact list creation. Example: [x*x for x in range(5)]
- Dict and Set Comprehension - Example: {x: x*x for x in range(3)}, {x for x in range(3)}
- Nested List Comprehension - Example: [[i*j for j in range(3)] for i in range(3)]
2. Functions
- Defining Functions - Example: def func(): pass
- Arguments - Example: def add(a, b): return a + b
- Positional and Keyword Arguments - Example: func(1, b=2)
- Arbitrary Arguments (*args, **kwargs) - Example: def f(*a, **k): pass
- Namespace - Space where names are mapped to objects.
- Scope (Local, Global) - Defines where variables can be accessed.
- Returning Multiple Values - Example: return a, b
- Functions as Objects - Functions can be passed and returned.
- Anonymous (Lambda) Functions - Example: lambda x: x+1
- Lambda with Filter and Map - Example: map(lambda x: x*2, [1,2,3])
3. Exception Handling
- Try-Except Block - Handles errors. Example: try: x/0 except: print('error')
- Catching Specific Exceptions - Example: except ZeroDivisionError:
- Else and Finally Blocks - Else runs if no error; finally runs always.
4. Files and Operating System
- File Handling - Open, read, write files.
- Reading Files - Example: open('file.txt').read()
- Writing Files - Example: open('file.txt', 'w').write('data')
- Creating Files - Use write mode 'w' or 'x'
- Deleting Files - Example: os.remove('file.txt')