Complete Notes with Examples
Dictionaries in Python
A dictionary stores key–value pairs.
Example:
student = {'name': 'Amit', 'age': 20, 'marks': 85}
Built-in Functions for Dictionaries
Functions like len(), type(), sorted().
Example:
d = {'a':1,'b':2}; print(len(d))
Built-in Methods for Dictionaries
Methods like get(), keys(), items().
Example:
d = {'x':10}; print([Link]('x'))
Dictionary Keys
Keys must be immutable.
Example:
d = {(1,2): 'pair'}
Sorting and Looping
You can sort keys and loop.
Example:
d = {'b':2,'a':1}; print(sorted(d))
Nested Dictionaries
Dictionary inside another.
Example:
students = {'s1': {'name':'Amit'}}
File Objects
open() returns file object.
Example:
f = open('[Link]','r')
File Built-in Functions
open(), print() to file.
Example:
f = open('[Link]','w'); print('Hello', file=f)
File Built-in Methods
read(), write(), close().
Example:
f = open('[Link]','r'); data = [Link]()
File Attributes
name, mode, closed.
Example:
f = open('[Link]','r'); print([Link])
Standard Files
stdin, stdout, stderr.
Example:
import sys; [Link]('Hi')
Command Line Arguments
[Link] list.
Example:
import sys; print([Link])
File System
os module operations.
Example:
import os; print([Link]())
File Execution
Running .py file.
Example:
# python [Link]
Persistent Storage Modules
pickle, shelve.
Example:
import pickle; [Link]([1,2], open('[Link]','wb'))
Regular Expressions
Pattern matching using re.
Example:
import re; print([Link](r'cat','my cat'))
Special Symbols in RE
.*, ^, $, [], etc.
Example:
import re; [Link](r'\d+','a1b22c3')
RE in Python
search(), match(), sub().
Example:
[Link](r'\s+',' ','a b c')