← Teacher guide Markdown for Prepare

First apps: cheat sheet

Apps with Gradio · In any playground, open Reference to copy these.

if / else

Do one thing or the other, depending on a condition.

if x > 0:
    pass
else:
    pass
Example
for x in [-90, -30, 30, 90]:
    penup()
    goto(x, 0)
    if x > 0:
        dot(30, "red")
    else:
        dot(30, "blue")
hideturtle()
What it draws
Red when x > 0, else blue

list

Keep many values in order under one name. The first item is place 0.

colours = ["red", "blue", "green"]

def (function)

Name a group of steps, then run it by calling its name.

def shape():
    pass

shape()
Example
def zigzag():
    left(45)
    forward(30)
    right(90)
    forward(30)
    left(45)

zigzag()
zigzag()
zigzag()
What it draws
Defined once, called three times

strip and lower

strip() removes spaces at the ends; lower() and upper() change the capitals. Each gives back new text.

command = command.strip().lower()

variable

Give a value a name, so you can reuse and change it.

size = 50
Example
length = 120
forward(length)
left(90)
forward(length / 2)
What it draws
length = 120, then half of it

grow value

Change a value every pass: read the old one, store a new one.

size = 5
for i in range(20):
    forward(size)
    right(90)
    size = size + 5
Example
size = 5
for i in range(30):
    forward(size)
    right(90)
    size = size + 5
What it draws
The same spiral, grown by an update

print

Show text or a value in the output box.

print("Hello")

join text

Join pieces of text into one with +. Spaces only appear where they are inside the quotes.

"Hello, " + name

int

Turn text made of digits, such as "12", into a whole number you can calculate with.

int(text)

an app with Gradio

Gradio calls your function with what was chosen, and shows what it returns.

import gradio as gr

def answer(choice):
    return "You chose " + choice

gr.Interface(fn=answer, inputs=gr.Dropdown(["pop", "rock"]), outputs="text").launch()