Algorithm
An exact step-by-step set of instructions that solves a problem – for example searching or sorting.
numbers = [5, 2, 8]
print(sorted(numbers))Look it up
Every term in one sentence – with a small example you can run and change right away.
An exact step-by-step set of instructions that solves a problem – for example searching or sorting.
numbers = [5, 2, 8]
print(sorted(numbers))A bug is a mistake in a program; debugging means finding and fixing it – e.g. with “Steps” in the editor.
total = 0
for i in range(1, 4):
total += i
print("So far:", total)A blueprint for your own objects with data and abilities.
class Dog:
def bark(self):
print("Woof!")
Dog().bark()Compares two values and gives True or False: == != < > <= >=
print(3 == 3, 3 != 4, 2 >= 5)Runs code only when something is true.
points = 7
if points > 5:
print("good")The kind of a value: whole number (int), decimal (float), text (str), truth value (bool) …
print(type(3), type(3.5), type("Hi"), type(True))A dictionary of key–value pairs in curly braces.
ages = {"Ana": 12, "Ben": 13}
print(ages["Ben"])When Python can't do something, it stops with an error message – for example NameError or ZeroDivisionError.
try:
print(10 / 0)
except ZeroDivisionError:
print("You can't divide by zero!")A string where you insert values with {}.
name = "Mia"
print(f"Hello {name}!")With open() you read and write files – with closes them again automatically.
with open("test.txt", "w") as f:
f.write("Hello")
print(open("test.txt").read())Your own named command, built with def, that you can use again and again.
def greet(name):
print("Hello", name)
greet("Tom")The spaces at the start of a line show what belongs to an if, loop or function (usually 4).
for i in range(2):
print("inside")
print("outside")The position in a list or string – counting starts at 0, -1 is the last element.
word = "Python"
print(word[0], word[-1])Asks for something and returns the input as text.
# name = input("What is your name? ")
print("input() waits for you to type something")The program that checks Python code and runs it step by step – at Codelix it runs in your browser.
import sys
print(sys.version.split()[0])A text format for data that programs use to store and exchange lists and dictionaries.
import json
print(json.dumps({"level": 3, "active": True}))Many values in order, in square brackets. The first one has index 0.
animals = ["dog", "cat"]
animals.append("mouse")
print(animals[0], len(animals))Building a list in one line.
print([n * n for n in range(5)])Repeats code – for a fixed number of times, while as long as something holds.
for i in range(3):
print("Round", i)A ready-made toolbox you bring in with import.
import random
print(random.randint(1, 6))A thing built from a blueprint (class).
class Ball:
def __init__(self, colour):
self.colour = colour
b = Ball("red")
print(b.colour)A value you pass into a function.
def double(number):
return number * 2
print(double(21))Creates number sequences for loops. range(3) gives 0, 1, 2.
print(list(range(1, 6)))A function calls itself with a smaller problem – until it reaches a base case.
def countdown(n):
if n == 0:
return
print(n)
countdown(n - 1)
countdown(3)Gives a result back from a function.
def summe(a, b):
return a + b
print(summe(2, 3))A collection where every value appears only once.
print(set([1, 2, 2, 3, 3]))Text in quotes.
greeting = "Hello"
print(greeting + "!")Like a list, but unchangeable and with round brackets – good for pairs of values.
punkt = (3, 7)
x, y = punkt
print(x + y)A name for a value – like a labelled box.
age = 12
print(age)