ID Academia · Python

Python commands — cheat sheet

Every command from the 03.11–13.11 lessons: what it does and a short example. Then common mistakes and a quiz.

Input, output, typesLesson 03.11.2026

CommandWhat it doesExample
print(a, b)Output; puts a space between argumentsprint(5, "+", 3) # 5 + 3
"\n"Line break inside textprint("A\nB")
input("text")User input — ALWAYS a stringn = input("Number: ") # "5"
int(x) / float(x) / str(x)Type conversionint("7") → 7, float("2.5") → 2.5, str(5) → "5"
type(x)Shows the typetype((1, 2)) # <class 'tuple'>
.isdigit()True if the string has only digits"12".isdigit() → True, "-5".isdigit() → False
# / """ """Comment / docstring describing a file or function# Check input

OperatorsLesson 03.11.2026

CommandWhat it doesExample
+ - * /Arithmetic; / always returns float7 / 2 → 3.5
== != > < >= <=Comparison, result True/Falsenum1 > num2
and / or / notLogical AND / OR / NOTa.isdigit() and b.isdigit()
=Assignment (not ==)result = 5

Conditions and exitLesson 03.11.2026

CommandWhat it doesExample
if / elif / elsePick a branch; stops at the first matchif x > 0: ... elif x == 0: ... else: ...
match / caseCompare one variable to options (Python 3.10+)case "+":
case _:Everything else (like else)case _: print("Wrong")
exit(1) / exit()Exit the program; 0 = success, 1 = errorProcess finished with exit code 1
import sys / sys.argv[1]Command-line arguments (strings)python main.py 7 3
Python
match operand:
    case "+":
        result = number1 + number2
    case "-":
        result = number1 - number2
    case _:
        print("Wrong operand")

LoopsLesson 03.11.2026

CommandWhat it doesExample
for x in list:Go through items one by onefor item in mylist: print(item)
while condition:Repeat while the condition is Truewhile len(mylist) > 0:
while True: / breakEndless loop, leave with breakif answer == "q": break
len(x) / .pop()Length / remove and return the last itemlen(["a", "b"]) → 2
Python
listlen = len(mylist)
while listlen > 0:
    listlen = listlen - 1  # decrease first, otherwise IndexError
    print(mylist[listlen])

String search, nested loopLesson 10.11.2026

CommandWhat it doesExample
.lower() / .upper()Lower / upper case (new string)"Mark".lower() → "mark"
.find("m")Index of first match, or -1"alma".find("m") → 2
.index("m")Index; if missing — ValueError"mark".index("m") → 0
"m" in sIs the character/item there"m" in "alma" → True
break (nested)Leaves only the inner loop
enumerate(x)Index + value togetherfor i, c in enumerate(s):
Python
for name in myList:
    nameLower = name.lower()
    if nameLower.find("m") >= 0:  # >= 0, not > 0: "Mark" has index 0
        print("Found 'm' in " + name)

Flag loop, continueLesson 10.11.2026

CommandWhat it doesExample
calc_on = True / while calc_on:A flag controls the loopcalc_on = False # exit
continueSkip to the next iterationafter a wrong input

Lists (list)Lesson 10.11.2026

CommandWhat it doesExample
[a, b, c] / list(x)Create a list (types can be mixed)["Ori", 4, 5.6, True]
x[1] / x[-1]Item by index (from 0) / laststudents[1]
x[:2] / x[1:3]Slice, end is excluded['Ori', 'Ilvia']
x[1][2]Double index: item → character"Ilvia"[2] → "v"
.append(x) / .insert(i, x)Add to end / before position istudents.insert(1, "Mark")
.pop() / .pop(0)Remove last / firstqueue: pop(0)
.remove(x) / del x[i]Delete by value / by index

Tuple, Set, DictionaryLesson 10.11.2026

CommandWhat it doesExample
(a, b, c) / tuple(x)Tuple — immutabletup = ("Yossi", "Yaniv", "Tomer")
.index(x) / .count(x)The only tuple methodstup.index("Yaniv") → 1
{a, b}Set — no duplicates, no order{"Yossi", "Tomer", "Tomer"} → 2 items
.add(x) / .discard(x)Add / remove in a set
{"key": value}Dictionary — key:value pairs{"Yisrael": 24, "Mark": 36}
d["key"] / d.get("key")By key (missing — KeyError) / safe (missing — None)ages["Yisrael"] → 24
d.items()Pairs for a loopfor k, v in d.items():
Python
tup1 = list(tup)          # tuple → list
tup1.insert(2, "Tomer")   # change the list
tup = tuple(tup1)         # list → tuple (not tuple(tup)!)

Mutable / ImmutableLesson 10.11.2026

CommandWhat it doesExample
int, float, bool, str, tupleImmutable — a change creates a new objects[0] = "P" → TypeError
list, dict, setMutable — the object itself changesmylist.append(3)
id(x)Object address in memory
b = a / a.copy()NOT a copy, a second name / a real copy

OOP — overviewLesson 10.11.2026

CommandWhat it doesExample
class Human:Class — a template: properties + methods
artem = Human("Artem", 25)Object — an instance of a class
class Doctor(Human):Inheritance: the child class gets everything from the parent

FunctionsLesson 13.11.2026

CommandWhat it doesExample
def name(params):Define a functiondef calculate(num1, operand, num2):
return xReturn a value and leave the functionreturn result
return a, b, cReturn several values (tuple)return num1, operand, num2
x, y, z = f()Unpack the returned tuplenum1, operand, num2 = get_input()
local / globalA variable inside a function exists only there
def f(x=0):Default parameter
Python
def factorial(n):
    result = 1
    for i in range(2, n + 1):
        result = result * i
    return result

print(factorial(4))  # 24

Mistakes found in lessons

CommandWhat happensFix
int(input()) # user types abcValueError, the program crashes.isdigit() / try-except
if ... if ... # no elseresult not created → NameError or an old valueelif + else
5 / 0ZeroDivisionErrorif num2 == 0:
mylist[len(mylist)]IndexError — the last index is len-1mylist[len(mylist) - 1]
exit(1) # inside the calculator loopOne mistake closes the whole programcontinue
tup = tuple(tup)The tuple did not changetup = tuple(tup1)
number2 = int(num2)num2 stays a string: 12 * "45" repeats textnum2 = int(num2)
continue # inside a functionSyntaxError — no loop aroundreturn
return str(a, b, c, d, e)TypeError: str() takes at most 3 argumentsreturn f"{a} {b} {c}"
def f(num1): print(number1)Variable not found — different namesone name everywhere
.find("m") > 0Misses a match at index 0.find("m") >= 0

Check yourself

Correct: 0 / 12
  1. 1. What does input() return?

  2. 2. Why is elif better than separate ifs?

  3. 3. What does continue do?

  4. 4. "alma".find("x") = ?

  5. 5. Which collections are immutable?

  6. 6. How do you remove duplicates from a list?

  7. 7. students[1:3] — does it include index 3?

  8. 8. How do you return 3 values from a function?

  9. 9. Why does calc_on = False inside a function not stop the loop?

  10. 10. 4! = ?

  11. 11. Queue: which method takes the first customer?

  12. 12. What does exit code 0 mean?