Basics
Table of Contents
- Python Standard Libraries
- Popular Third-Party Libraries
- Commonly used buildin keywords
- Commonly Used Built-in Functions
Python Standard Libraries
| Category | Libraries | Description |
|---|---|---|
| Data Processing | csv, json, xml |
File format parsing and handling |
| Files & OS | os, pathlib, shutil, glob |
File operations, paths, system interaction |
| Date & Time | datetime, time, calendar |
Date, time manipulation and formatting |
| Mathematics | math, random, statistics |
Mathematical functions, random number generation |
| Networking | socket, urllib, http |
Low-level networking, HTTP requests |
| Concurrency | threading, multiprocessing, asyncio |
Multi-threading, parallelism, async operations |
| Data Structures | collections, array, heapq |
Specialized container datatypes |
| Testing | unittest, doctest |
Unit testing frameworks |
Popular Third-Party Libraries
| Category | Libraries | Description |
|---|---|---|
| Data Science | numpy, pandas, scipy |
Numerical computing, data analysis |
| Machine Learning | scikit-learn, tensorflow, pytorch |
ML algorithms, deep learning |
| Visualization | matplotlib, seaborn, plotly |
Charts, graphs, interactive plots |
| Web Development | django, flask, fastapi |
Web frameworks and APIs |
| HTTP | requests, httpx |
HTTP clients |
| CLI Tools | click, typer, argparse |
Command-line interfaces |
| DevOps | docker, kubernetes, ansible |
Infrastructure automation |
| Testing | pytest, selenium |
Testing frameworks |
| Utilities | pillow, beautifulsoup4, sqlalchemy |
Image processing, web scraping, ORM |
Commonly used buildin keywords
| Keyword | Description | Keyword | Description |
|---|---|---|---|
and, or, not |
Logical operators for boolean operations | if, elif, else |
Conditional statement keywords |
for, while |
Loop creation keywords | break, continue |
Loop control keywords |
def, return |
Function definition and return value | lambda |
Anonymous function creation |
class |
Class definition keyword | import, from, as |
Module import keywords |
try, except, finally |
Exception handling keywords | raise |
Raises an exception explicitly |
with |
Context manager for resource management | in |
Membership testing operator |
is |
Identity operator (object comparison) | pass |
Null operation placeholder |
global, nonlocal |
Variable scope declaration | True, False, None |
Built-in constants |
yield |
Generator function keyword | assert |
Debugging check that raises exceptions |
del |
Removes object references or items | async, await |
Asynchronous programming keywords |
match, case |
Pattern matching (Python 3.10+) | type |
Type annotations context (Python 3.10+) |
yield
Core Idea: Creating Iterators the Easy Way
At its heart, a function that contains the yield keyword is called a generator function. When you call a generator function, it doesn't execute the function body immediately. Instead, it returns a special object called a generator object (or simply generator).
This generator object is an iterator. Iterators are objects that you can loop over (like lists or tuples), but
they produce their items one at a time and only when requested.
-
How
yieldWorks vs.return-
return: When a regular function hits a return statement, it sends a value back to the caller and exits completely. All its local variables are destroyed, and its execution state is lost. If you call the function again, it starts from the beginning. -
yield: When a generator function hits a yield statement: It pauses its execution right at that point. It sends the value specified after yield back to the caller (in C, it voluntarily yields thread to CPU and allow others to run). Crucially, it remembers its state (local variables, instruction pointer, etc.). The next time the caller asks for a value from the generator (usually via a for loop or the next() function), the generator function resumes execution immediately after the yield statement where it paused.
-
assert
Assert is a debugging aid that tests a condition. used for internal sanity checks in your code.
Throw an
AssertionErrorif the condition is false. It is not meant to be used for data validation or user input validation.DO NOT USE
assertfor data validation or user input validation. It is not a substitute for proper error handling.
assertcan be disabled globally with the-O(optimize) flag when running Python scripts. This means that any assertions in your code will be ignored
assert <condition>assert <condition>, <optional_error_message>
try:
calculate_discount(100, 1.1) # Discount > 1 (110%)
except AssertionError as e:
print(f"Assertion Error: {e}")
try:
calculate_discount(-50, 0.2) # Negative price
except AssertionError as e:
print(f"Assertion Error: {e}")
try:
calculate_discount(100, -0.1) # Negative discount
except AssertionError as e:
print(f"Assertion Error: {e}")
Commonly Used Built-in Functions
buildin functions
| Function | Description | Example |
|---|---|---|
abs(x) |
Return absolute value of a number | abs(-5) → 5 |
all(iterable) |
Return True if all elements are true | all([True, 1, "text"]) → True |
any(iterable) |
Return True if any element is true | any([False, 0, ""]) → False |
bool(x) |
Convert value to Boolean | bool(0) → False |
dict() |
Create a dictionary | dict(a=1, b=2) → {'a': 1, 'b': 2} |
dir([object]) |
List names in scope or object attributes | dir(str) → list of string methods |
enumerate(iterable) |
Return index-value pairs | list(enumerate(['a', 'b'])) → [(0, 'a'), (1, 'b')] |
filter(function, iterable) |
Filter elements by function | list(filter(lambda x: x>0, [-1,0,1])) → [1] |
float(x) |
Convert to float | float('3.14') → 3.14 |
int(x[, base]) |
Convert to integer | int('100', 2) → 4 |
len(s) |
Return length of object | len([1, 2, 3]) → 3 |
list([iterable]) |
Create a list | list('abc') → ['a', 'b', 'c'] |
map(function, iterable) |
Apply function to all items | list(map(lambda x: x*2, [1,2,3])) → [2,4,6] |
max(iterable) |
Return largest item | max([1, 5, 3]) → 5 |
min(iterable) |
Return smallest item | min([1, 5, 3]) → 1 |
open(file, mode='r') |
Open file and return file object | with open('file.txt', 'r') as f: |
print(*objects) |
Print objects to stdout | print('Hello', 'World') → Hello World |
range(stop) |
Create sequence of numbers | list(range(3)) → [0, 1, 2] |
sorted(iterable) |
Return sorted list | sorted([3, 1, 2]) → [1, 2, 3] |
str(object) |
Convert to string | str(123) → '123' |
sum(iterable) |
Sum all items | sum([1, 2, 3]) → 6 |
type(object) |
Return type of object | type(5) → <class 'int'> |
zip(*iterables) |
Combine iterables into tuples | list(zip([1,2], ['a','b'])) → [(1,'a'), (2,'b')] |
String Methods
| Method | Description | Method | Description |
|---|---|---|---|
str.capitalize() |
Return string with first character capitalized | str.casefold() |
Return string as casefolded for caseless matching |
str.center(width) |
Return centered string padded with spaces | str.count(sub) |
Count occurrences of substring |
str.encode() |
Return encoded string as bytes | str.endswith(suffix) |
Check if string ends with suffix |
str.expandtabs() |
Replace tabs with spaces | str.find(sub) |
Find substring (return index or -1) |
str.format() |
Format string with {} placeholders | str.index(sub) |
Find substring (raise ValueError if not found) |
str.isalnum() |
Check if all characters are alphanumeric | str.isalpha() |
Check if all characters are alphabetic |
str.isdigit() |
Check if all characters are digits | str.islower() |
Check if all characters are lowercase |
str.isupper() |
Check if all characters are uppercase | str.join(iterable) |
Join strings with separator |
str.lower() |
Convert string to lowercase | str.lstrip() |
Remove leading whitespace |
str.replace(old, new) |
Replace substring | str.split(sep) |
Split string at separator |
str.strip() |
Remove leading and trailing whitespace | str.title() |
Return titlecased version of string |
str.upper() |
Convert string to uppercase | str.zfill(width) |
Pad string with zeros on the left |
str.partition(sep) |
Partition string at first separator occurrence | str.rpartition(sep) |
Partition string at last separator occurrence |
str.splitlines() |
Split string at line breaks | str.swapcase() |
Swap case of all characters |
str.istitle() |
Check if string is titlecased | str.isspace() |
Check if string contains only whitespace |
str.isnumeric() |
Check if all characters are numeric | str.isdecimal() |
Check if all characters are decimal |
str.rstrip() |
Remove trailing whitespace | str.rsplit(sep) |
Split from right at separator |
str.ljust(width) |
Left-justify string in field of width | str.rjust(width) |
Right-justify string in field of width |
str.startswith(prefix) |
Check if string starts with prefix | str.translate(table) |
Translate string using character mapping table |
Related Properties in Other Languages:
| Python | C# | Java | JavaScript |
|---|---|---|---|
str.lower() |
string.ToLower() |
str.toLowerCase() |
str.toLowerCase() |
str.upper() |
string.ToUpper() |
str.toUpperCase() |
str.toUpperCase() |
str.strip() |
string.Trim() |
str.trim() |
str.trim() |
str.startswith() |
string.StartsWith() |
str.startsWith() |
str.startsWith() |
str.endswith() |
string.EndsWith() |
str.endsWith() |
str.endsWith() |
len(str) |
string.Length |
str.length() |
str.length |
str.isdigit() |
char.IsDigit() |
Character.isDigit() |
str.match(/^\d+$/) |
str.isalpha() |
char.IsLetter() |
Character.isLetter() |
str.match(/^[A-Za-z]+$/) |
String Indexing and Slicing
| Operation | Description | Example | Result |
|---|---|---|---|
str[i] |
Access character at index i | "Python"[0] |
'P' |
str[i:j] |
Slice from index i to j (excluding j) | "Python"[0:2] |
'Py' |
str[i:j:k] |
Slice with step k | "Python"[0:6:2] |
'Pto' |
str[:] |
Get a copy of the whole string | "Python"[:] |
'Python' |
str[-i] |
Access character at index i from the end | "Python"[-1] |
'n' |
str[-i:-j] |
Slice from i to j positions from the end | "Python"[-3:-1] |
'ho' |
str[:j] |
Slice from start to index j | "Python"[:3] |
'Pyt' |
str[i:] |
Slice from index i to the end | "Python"[3:] |
'hon' |
str[::-1] |
Reverse the string | "Python"[::-1] |
'nohtyP' |