📄 Python Reference Sheet
Data types + assignment
True / False | boolean values |
10, 10.01, "123abc" | integer, float, string |
x = 5 | simple assignment |
x += 1 x -= 1 x *= 2 x /= 2 | change and store back |
int(x) float(x) str(x) bool(x) | convert between types |
# comment \n \t | comment, new line, tab |
Maths + comparisons + logic
+ - * / ** % // | add, subtract, multiply, divide, power, remainder, floor divide |
== != > < >= <= | equal, not equal, higher, lower, higher/lower or equal |
and or not | both true, either true, flips True/False |
pow(2, 3) | 2 to the power of 3 |
Strings
s[i] s[-1] s[i:j] | position i, last item, slice from i to j |
s.upper() s.lower() | BIG LETTERS, small letters |
s.count(x) s.find(x) s.index(x) | how many, first position, position (shouts if missing) |
s.strip() | trims spaces off both ends |
s.isdigit() s.isalpha() | all digits? all letters? |
chr(65) ord("A") | code to letter, letter to code |
Lists
L[i] L[-1] L[i:j] | position i, last item, slice (positions start at 0) |
len(L) del L[i] | how many items, remove position i |
L.append(x) L.extend(M) L.insert(i, x) | add to end, add a whole list, insert at i |
L.remove(x) L.pop(i) L.clear() | remove value, remove position (gives it back), empty all |
L.index(x) L.count(x) | first position of x, how many x |
L.sort() sorted(L) L.reverse() L.copy() | sort in place, sorted copy, flip order, safe copy |
Decisions + loops + functions
if x in L: | True when x is in the list |
while cond: | repeat until the condition is False |
for v in L: | one turn per item |
for i in range(start, stop, step): | counts start, start+step, … stopping before stop |
break continue pass | leave the loop, skip to next turn, do nothing |
def f(p): … return x | define once, call often; return sends an answer back |
import m m.f() from m import f | use a module, call its function, import one name |
map(f, L) | runs f on every item, gives the answers back |
Files
f = open("a.csv") | open for reading |
f.read() f.readline() | whole file, one line |
for line in f: | one turn per line |
f = open("a.txt", "w") f.write(s) | open for writing, save text |
f.close() | always close when finished |
The teacher's runs appear here.
🧑💻 My editor
Console
Press ▶ Run. When the program calls input(), a box appears here: type and press Enter.
📄 Data files
Files stay in this browser only · max 1 MB each · save from code (e.g. df.to_csv) then ⬇ to download.
📦 Libraries
Common:
Needs internet · installed for this session only · the standard library always works without this.