Core programming concepts
Almost every language shares a handful of building blocks: variables, data types, decisions, loops, and functions. Master these and new languages become much easier to learn.
Variables and assignment
A variable is a named storage location holding a value that can change. Assignment stores a value: in Python, score = 10 sets score to 10, and score = score + 5 then makes it 15. Note that the equals sign in programming means 'store', not mathematical equality.
Data types
Values have types that determine what operations make sense. Common types are integers (whole numbers), floating-point numbers (decimals), Booleans (True or False), strings (text), and collections such as lists or arrays. Adding two numbers gives a sum; 'adding' two strings usually joins them.
Static and dynamic typing
In statically typed languages, such as Java and C, variable types are declared or inferred and checked before the program runs, catching many mistakes early. In dynamically typed languages, such as Python and JavaScript, types are checked as the program runs, which is flexible but can let type mistakes slip through until that line executes.
Selection
Selection lets programs make decisions. An if statement runs code only when a condition is true; else provides an alternative; and else-if chains, written elif in Python, test several conditions. Conditions use comparisons, such as ==, <, and >=, combined with and, or, and not.
Iteration
Iteration repeats code. A for loop repeats a set number of times or once for each item in a collection. A while loop repeats as long as a condition stays true. A common bug is an infinite loop whose condition never becomes false; another is an off-by-one error, where a loop runs one time too many or too few.
Functions
A function is a named, reusable block of code that may take inputs, called parameters, and return an output. Functions let programmers break problems into parts, avoid repetition, and test pieces separately. Variables created inside a function usually have local scope: they exist only within that function.
Collections
Programs often handle many values. Lists or arrays store ordered items accessed by index, often starting at 0. Dictionaries, also called maps or hash tables, store key-value pairs, such as a word and its definition, for fast lookup by key. Choosing the right collection makes code simpler and faster.
Worked example
A function to find the average of a list in Python: def average(nums): total = 0; for n in nums: total = total + n; return total / len(nums). For [4, 8, 9], the loop adds 4, 8, and 9 to get 21, and 21 / 3 = 7.0. Calling it with an empty list would cause a division-by-zero runtime error, so a careful version first checks whether the list is empty.
Key takeaways
Variables store changeable values; = means assignment. Data types include integers, floats, Booleans, strings, and collections. Static typing checks types before running; dynamic typing checks during execution. Selection uses if, else, and elif with conditions. Iteration uses for and while loops; beware infinite loops and off-by-one errors. Functions package reusable code with parameters, return values, and local scope. Lists are ordered by index; dictionaries map keys to values.
Flashcard
Named storage for a value
Check your understanding
A loop meant to run 10 times runs 11 times. What is this common bug called?
