Module 3: Basics of Programming, with supporting citations from credible programming
references.
Module 3: Basics of Programming – Detailed Notes
1. Variables
Definition:
A variable is a named storage location in memory used to hold data that can change during
program execution.
Declaration: The act of creating a variable and specifying its name (and sometimes
type).
Initialization: Assigning an initial value to a variable at the time of declaration.
Key Points:
Variables improve code readability and maintainability.
Naming rules vary by language but typically must start with a letter or underscore, and
cannot contain spaces or reserved keywords (e.g., for, if).
Scope determines where in the code a variable is accessible (local, global).
Example (Python):
age = 25 # integer variable
name = "Alice" # string variable
Sources:
Downey, A. (2015). Think Python: How to Think Like a Computer Scientist. Green Tea
Press.
Kernighan, B.W., & Ritchie, D.M. (1988). The C Programming Language. Prentice Hall.
2. Data Types
Definition:
A data type defines the kind of data a variable can store and the operations that can be
performed on it.
Common Categories:
1. Primitive Types
o Integer (int): Whole numbers (e.g., 42)
o Float (float): Decimal numbers (e.g., 3.14)
o Boolean (bool): True/False values
o Character (char): Single characters (in languages like C, Java)
2. Non-Primitive Types
o Strings: Sequence of characters (e.g., "Hello")
o Arrays/Lists: Ordered collections of elements
o Objects: Complex structures in OOP
Example (Python):
price = 19.99 # float
is_active = True # boolean
fruits = ["apple", "banana", "mango"] # list
Sources:
Zelle, J. (2017). Python Programming: An Introduction to Computer Science. Franklin,
Beedle & Associates.
3. Control Structures
3.1 Conditional Statements
Used to execute different code paths based on conditions.
Syntax (Python):
if age >= 18:
print("Adult")
elif age > 12:
print("Teenager")
else:
print("Child")
Key Points:
Conditionals use relational operators (<, >, ==, !=) and logical operators (and, or, not).
3.2 Loops
Used to repeat a block of code until a condition is met.
Types:
For Loop: Iterates over a sequence.
for i in range(5):
print(i)
While Loop: Executes while a condition remains true.
count = 0
while count < 5:
print(count)
count += 1
Sources:
Sweigart, A. (2019). Automate the Boring Stuff with Python. No Starch Press.
4. Functions
Definition:
A function is a reusable block of code designed to perform a specific task.
Advantages:
Improves modularity
Encourages code reuse
Simplifies debugging and testing
Syntax (Python):
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Parameters & Arguments:
Parameters: Variables in the function definition.
Arguments: Values passed to the function when it’s called.
Sources:
McMillan, M. (2006). Data Structures and Algorithms Using Python. Cambridge
University Press.
5. Introduction to Algorithms
Definition:
A step-by-step set of instructions to solve a problem.
Key Characteristics:
Finiteness: Must terminate after a finite number of steps.
Definiteness: Each step must be clear and unambiguous.
Input & Output: Takes input, produces output.
Effectiveness: Steps must be basic enough to be executed.
Example – Finding Maximum in a List:
numbers = [3, 8, 1, 6]
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
print(max_num)
Sources:
Cormen, T.H., Leiserson, C.E., Rivest, R.L., & Stein, C. (2009). Introduction to Algorithms.
MIT Press.
6. Problem-Solving Techniques
Common Approaches:
Top-Down Design: Breaking a problem into smaller sub-problems.
Flowcharts & Pseudocode: Visual and textual ways to outline logic.
Incremental Development: Build and test parts before integrating.
Example Pseudocode:
START
SET sum = 0
FOR each number in list
ADD number to sum
END FOR
PRINT sum
END
Sources:
Downey, A. (2015). Think Python. Green Tea Press.
7. Debugging Methods
Definition:
Debugging is the process of finding and fixing errors in code.
Common Strategies:
1. Print Debugging: Inserting print statements to trace variables.
2. Using a Debugger: Step through code line-by-line.
3. Rubber Duck Debugging: Explaining the problem to an inanimate object to clarify
thinking.
4. Binary Search Debugging: Narrow down error location by halving the search area.
Sources:
Hunt, A., & Thomas, D. (1999). The Pragmatic Programmer. Addison-Wesley.
✅ Key Takeaways:
Variables and data types are foundational building blocks.
Control structures dictate the flow of program execution.
Functions encourage modular, reusable code.
Algorithms are the blueprint for solving problems.
Debugging is as essential as writing the code itself.