Programming with variables, conditionals, and functions
Aligned to CSTA-AP — CSTA K-12 Computer Science Standards.
What this lesson teaches
Variables store information your program can use and change, like a score or a name. Conditionals (if/else) let a program make decisions, doing one thing when something is true and another when it is false. Loops repeat work, and functions bundle steps into a reusable command. These same structures appear in every programming language, including Python.
Worked example
In a guessing game, a variable holds the secret number. A conditional checks: if the guess equals the number, print You win, else print Try again. A loop lets the player keep guessing until they get it.
Practice questions
- What does a variable do in a program?
- Write an if/else rule for a program that says Pass if a score is 60 or higher, else Retry.
- Why is it useful to put repeated steps into a function?
Watch the lesson
Every lesson comes with a video taught in English and Spanish — the same video the QR code in the printed workbook opens.
▶ Watch this lessonEn español
Programación con variables, condicionales y funciones
Las variables guardan información que tu programa puede usar y modificar, como un puntaje o un nombre. Los condicionales (if/else) permiten que un programa tome decisiones, haciendo una cosa cuando algo es verdadero y otra cuando es falso. Los ciclos repiten trabajo, y las funciones agrupan pasos en un comando reutilizable. Estas mismas estructuras aparecen en todos los lenguajes de programación, incluyendo Python.
Ejemplo: En un juego de adivinanzas, una variable guarda el número secreto. Un condicional verifica: if el número adivinado es igual al número secreto, imprime Ganaste, else imprime Inténtalo de nuevo. Un ciclo deja que el jugador siga adivinando hasta que lo logre.
More Algorithms & Programming lessons
Teach this lesson today
An AI tutor that guides with questions instead of just giving answers — all K–12 core subjects, every child in the family.
Start free — no card required