Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

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. 1 If x = 4, y = 7, and x = x + y, what are the final values of x and y?
  2. 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. 3 A list named scores has 8 items. What are the valid indexes, and which index gives the last item?
  4. 4 Explain why breaking a large program into functions can make the program easier to test, debug, and reuse.

Understanding Programming Fundamentals

A program has a changing state. State means the values currently stored in memory while the program runs. Reading code is easier when students pause after each line and write down the current values.

This process is called tracing. It reveals that the computer does not understand the goal of the program. It follows each instruction in order.

A small mistake in an update can change every later result. This matters in real programs such as score trackers, shopping carts, game counters, and temperature monitors. Each one depends on values being updated at the right moment.

Conditions and loops need careful attention because they control which instructions run. A condition should describe a fact that can be checked at that moment. Students often make errors by testing the wrong value, placing a condition in the wrong order, or forgetting an edge case.

Edge cases are unusual but valid inputs, such as an empty list, a zero value, or a value exactly at a limit. Loops need a clear stopping rule. Before writing a loop, decide what changes on every repetition and what proves the loop will end.

This prevents accidental endless loops. It also helps to state a loop invariant. A loop invariant is something that remains true after every repetition, such as a running total matching the values processed so far.

Functions help control complexity by giving a task one home in the program. Good function names describe an action or result. A function that calculates a final grade should not quietly print messages, change unrelated data, or depend on hidden values.

Keeping inputs and outputs clear makes testing much easier. Students should learn the difference between a local variable and a value shared outside the function. A local variable exists only while that function runs.

Lists need extra care because a function can change the original list if it receives that list directly. This can be useful, but it can cause confusing bugs when the change was not intended.

Algorithms are step by step methods for solving problems. Two algorithms can give the same answer while using very different amounts of time. For a short class exercise, this difference may not matter.

For a school database with thousands of records or a video app handling millions of users, it matters a great deal. Searching each item one at a time takes longer as the list grows. Repeating a full search inside another full search grows even faster.

Debugging should begin with a specific observation. Use a small test case, predict the expected result, run the code, then compare the actual result. Add temporary output to inspect important values.

Test normal cases, boundary cases, and invalid input. Fix one cause at a time, then run the tests again. This method builds reliable habits instead of relying on guesses.