Jun 30, 2024
my_list = ["banana", "cherry", "apple"]
list()
function.item = my_list[index]
(0-based, supports negative indices).for item in my_list: print(item)
.if "banana" in my_list:
.append()
, insert()
, pop()
, remove()
, clear()
, reverse()
, sort()
, sorted()
.sub_list = my_list[start:stop]
(stop is exclusive)..copy()
, list()
, slicing [:]
.[expression for item in iterable]
.my_tuple = ("Max", 28, "Boston")
.item = my_tuple[index]
(supports negative indices).count()
, index()
.list(my_tuple)
, tuple(my_list)
.my_dict = {"name": "Max", "age": 28}
.dict()
function without quotes for keys.value = my_dict[key]
.pop()
, popitem()
, del my_dict[key]
, clear()
, keys()
, values()
, items()
.if key in my_dict:
..copy()
, dict()
.update()
method merges dictionaries.{1, 2, 3}
, set()
function.set()
only.add()
, remove()
, discard()
, clear()
, pop()
, union()
, intersection()
, difference()
, symmetric_difference()
..copy()
, set()
.frozenset
immutable version.char = my_string[index]
(supports negative indices).strip()
, upper()
, lower()
, find()
, count()
, replace()
.split()
, join()
with lists.%
, str.format()
, f-strings for Python 3.6+.Counter
: Counts elements frequency.namedtuple
: Lightweight object-like tuples.OrderedDict
: Remembers entry order.defaultdict
: Default factory for new keys.deque
: Double-ended queue.product()
, permutations()
, combinations()
, accumulate()
, groupby()
.count()
, cycle()
, repeat()
.lambda args: expression
.sorted()
, map()
, filter()
, reduce()
.ImportError
, NameError
, FileNotFoundError
, ValueError
, IndexError
, KeyError
.raise Exception("message")
.try...except
, else
, finally
blocks.Exception
.import logging
, logging.basicConfig()
.debug
, info
, warning
, error
, critical
.logging.conf
, dictConfig
.StreamHandler
, FileHandler
, RotatingFileHandler
, SMTPHandler
.json.dump()
, json.dumps()
, json.load()
, json.loads()
.default()
, object_hook
with JSONEncoder
.random()
, uniform()
, randint()
, choice()
, shuffle()
, seed()
.secrets
module.@decorator_name
.*args
, **kwargs
.__call__
.yield
keyword.next()
, for
loops, sum()
, sorted()
on generator.Thread
, Process
, Pool
, Queue
from threading
and multiprocessing
modules.Lock
, Semaphore
, Event
.*args, **kwargs
.copy.copy()
, copy.deepcopy()
.__copy__
, __deepcopy__
methods.with
statements for resource management (files, locks).__enter__
, __exit__
or generators with contextmanager
decorator.