Jul 11, 2024
[] or the list() function.['banana', 'cherry', 'apple'], list() -> []if item in list: ...len(list): Get number of elements.list.append(item): Add an element at the end.list.insert(index, item): Insert element at a specific position.list.pop(): Remove and return the last element.list.remove(item): Remove first occurrence of an element.list.clear(): Remove all elements.list.reverse(): Reverse the order of elements.list.sort(): Sort the list in ascending order.sorted(list): Return a new sorted list without changing the original.[0] * 5 -> [0, 0, 0, 0, 0]list1 + list2list[start:stop:step]list.copy(), list(list), slicing [:]() or the tuple() function.(item,)for item in tuple: ...if item in tuple: ...len(tuple): Get number of elements.tuple.count(item): Count occurrences of an element.tuple.index(item): Find first occurrence of an element.list(tuple), tuple(list)tuple[start:stop:step]{} or the dict() function.dict[key] = valuedel dict[key]dict.pop(key)dict.popitem(): Pops last item (Python 3.7+).if key in dict: ...for key in dict: ... or dict.keys()for value in dict.values(): ...for key, value in dict.items(): ...dict.copy() or dict(dict)dict1.update(dict2){} or the set() function.add(item), remove(item), discard(item), clear(), pop()for item in set: ...if item in set: ...set1.union(set2) or |set1.intersection(set2) or &set1.difference(set2) or -set1.symmetric_difference(set2) or ^issubset(), issuperset()set.copy() or set(set)frozenset (immutable set)'' or double "" quotes for strings, or triple quotes for multi-line strings.+ operator.for char in string: ...if char in string: ....upper(), .lower(), .startswith(), .endswith(), .find(), .count(), .replace()string.split(separator), 'separator'.join(list)'%s %d' % (str, int)str.format(): '{0} {1}'.format(str, int)f'{str} {int}'lambda keyword.lambda arguments: expressionsorted(), map(), filter(), reduce().raise keyword.assert condition, messagetry: ... except: ...try: ... except ExceptionType: ...else: ..., finally: ...Exception.import logging, logging.basicConfig(level=logging.DEBUG).conf files for configuration.exc_info=Truejson.dumps(), json.dump() for encoding.json.loads(), json.load() for decoding.default function.object_hook.random.random(), random.uniform(start, stop), random.randint(start, stop).random.shuffle(list), random.sample(list, k), random.choices(list, k), random.choice(list).@decorator above a function definition.wrapper.__call__ method.yield keyword.from threading import Threadthread.start(), thread.join()from multiprocessing import Processprocess.start(), process.join()Value, Array for shared state.*args for positional, **kwargs for keyword arguments.* and ** for unpacking iterables into function calls.* for multiplication, ** for power.*.*args and **kwargs.* and ** to unpack lists, tuples, dicts.*, merge dicts with **.*copy.copy() for shallow, copy.deepcopy() for deep copy.__copy__ and __deepcopy__ methods.with statement.with open(file), file operations, with lock, managing locks.__enter__ and __exit__ methods.contextlib.contextmanager decorator with a generator function.