Python Cheatsheet

kuniga.me > Docs > Python Cheatsheet

Python Cheatsheet

Syntax for common tasks I run into often. Tested in Google Chrome 102 (circa 2022-05).

Index

  1. Data Structures
    1. Array
      1. Create
      2. Add
      3. For loop
      4. Size
    2. Set
      1. Create
      2. Add
      3. For loop
      4. Membership check
      5. Remove
      6. Get first element

Data Structures

Array

Create

Fixed size:

const arr = new Array(10);

Add

keywords: insert, append

Append to the end:

arr.push(e)

For loop

let s = 0
arr.forEach(e => s += e);

Size

keywords: length

arr.length

Set

Create

Empty:

const s = new Set();

From Array:

const s = new Set([1, 2]);

Add

keywords: insert, append

s.add(x);

For loop

for (let e of s) {
  print(e);
}

Membership check

s.has(x);

Remove

s.delete(x);

Get first element

$O(1)$:

const first = s.values().next().value;