Programming Fundamentals Cheat Sheet
A printable reference covering variables, data types, conditionals, loops, functions, arrays, algorithms, and debugging for grades 9-12.
Programming fundamentals are the basic ideas used to write clear, correct, and reusable code. This cheat sheet helps students remember common syntax patterns, logic rules, and problem-solving steps used in beginning computer science. It focuses on Python-style pseudocode because the structure is readable and similar to many classroom languages. Students can use it while planning algorithms, tracing code, or checking programs for errors. The core concepts include variables, data types, operators, control flow, loops, functions, lists, and debugging. Programs process input, store values, make decisions with Boolean expressions, repeat steps with loops, and organize work into functions. Important formulas include index ranges, loop counts, assignment updates, and common runtime patterns. Understanding these rules makes code easier to predict, test, and improve.
Key Facts
- Assignment stores a value using variable = expression, so x = x + 1 means take the old value of x, add 1, and store the result back in x.
- Common data types include int for whole numbers, float for decimals, str for text, bool for True or False values, and list for ordered collections.
- Boolean comparisons return True or False, such as x == 5, x != 5, x < 5, x <= 5, x > 5, and x >= 5.
- Logical operators combine conditions using and, or, and not, where A and B is True only if both A and B are True.
- An if statement chooses between paths using if condition: do_this, elif other_condition: do_that, and else: default_action.
- A for loop over range(start, stop, step) visits values from start up to but not including stop, changing by step each time.
- A list of length n uses indexes 0 through n - 1, so the first item is list[0] and the last item is list[n - 1].
- A function follows the pattern def name(parameters): return value, and it should perform one clear task with predictable inputs and outputs.
Vocabulary
- Variable
- A named storage location that holds a value which can be used or changed while a program runs.
- Data Type
- A category of value, such as int, float, str, bool, or list, that determines what operations are valid.
- Conditional
- A control structure that runs different code depending on whether a Boolean condition is True or False.
- Loop
- A control structure that repeats a block of code while a condition is true or for each value in a sequence.
- Function
- A reusable block of code with a name, optional parameters, and often a return value.
- Algorithm
- A step-by-step procedure for solving a problem or completing a task.
Common Mistakes to Avoid
- Confusing = with == is wrong because = assigns a value, while == compares two values and returns True or False.
- Using the wrong list index is wrong because lists start at index 0, so a list with n items has no item at index n.
- Forgetting to update a while-loop variable is wrong because the loop condition may never become False, causing an infinite loop.
- Changing a variable inside a function and expecting the outside variable to always change is wrong because many variables are local to the function scope.
- Testing only one example is wrong because code can work for one input but fail for edge cases such as 0, negative numbers, empty lists, or maximum values.
Practice Questions
- 1 If x = 4, y = 7, and x = x + y, what are the final values of x and y?
- 2 A loop runs for i in range(2, 11, 3). List every value of i and state how many times the loop runs.
- 3 A list named scores has 8 items. What are the valid indexes, and which index gives the last item?
- 4 Explain why breaking a large program into functions can make the program easier to test, debug, and reuse.