Basic Concepts The Underrated Pillars of Software Development

As developers, we’re constantly chasing the next big framework, the latest language feature, or the most efficient algorithm. But sometimes, we move so fast that we overlook the bedrock upon which all complex systems are built: the basic concepts.

Understanding these fundamentals deeply isn’t just for beginners; it’s what separates a hacky coder from a true software craftsman. It’s about knowing why something works, not just how to make it work. In this post, we’ll re-examine variables, data types, control flow, and functions, reinforced with practical, runnable examples.

Let’s dive in.


1. Variables: Naming Your Data

At its core, a variable is a named storage location that holds a value. Think of it as a labeled box where you can put different things. The “label” is the variable’s name, and the “thing inside” is its value. Values can change over time, hence “variable.”

Bash Examples

In Bash, you declare and assign variables without explicit type declarations. Access their values using a dollar sign ($).

Example 1.1: Simple Variable Assignment

#!/bin/bash

# Declare and assign a string variable
MY_NAME="Alice"

# Declare and assign an integer variable
MY_AGE=30

# Print the values
echo "Hello, my name is $MY_NAME."
echo "I am $MY_AGE years old."
Hello, my name is Alice.
I am 30 years old.

Example 1.2: Reassigning and Using Variables in Operations

#!/bin/bash

# Initial assignment
COUNT=5

echo "Initial count: $COUNT"

# Reassign
COUNT=10
echo "Updated count: $COUNT"

# Use in arithmetic operation (requires `((...))` or `expr`)
SUM=$((COUNT + 5))
echo "Count + 5: $SUM"

# Note: Variable names in Bash are often capitalized by convention,
# but it's not a strict requirement. Lowercase is also common for local variables.
Initial count: 5
Updated count: 10
Count + 5: 15

Python Examples

Python variables are dynamically typed, meaning you don’t declare their type explicitly. The type is inferred based on the assigned value.

Example 1.3: Simple Variable Assignment and Types

# Assign a string
user_name = "Bob"

# Assign an integer
user_age = 25

# Assign a float
user_height = 1.75

print(f"User Name: {user_name}, Type: {type(user_name)}")
print(f"User Age: {user_age}, Type: {type(user_age)}")
print(f"User Height: {user_height}, Type: {type(user_height)}")
User Name: Bob, Type: <class 'str'>
User Age: 25, Type: <class 'int'>
User Height: 1.75, Type: <class 'float'>

Example 1.4: Variable Reassignment and Swapping

x = 10
y = 20

print(f"Before swap: x={x}, y={y}")

# Reassign x
x = 15
print(f"After reassign x: x={x}, y={y}")

# Pythonic way to swap variables
x, y = y, x
print(f"After swap: x={x}, y={y}")
Before swap: x=10, y=20
After reassign x: x=15, y=20
After swap: x=20, y=15

2. Data Types: What Kind of Data Are We Storing?

Data types classify the kind of values a variable can hold, determining what operations can be performed on them. Is it text? A whole number? A number with decimals? A true/false value? Or perhaps a collection of items?

Bash Examples

Bash primarily treats everything as a string, but it has capabilities for integer arithmetic.

Example 2.1: Bash’s String-centric Nature

#!/bin/bash

# All assignments are strings by default
MESSAGE="Hello World"
NUMBER="123"

echo "MESSAGE type: string"
echo "NUMBER type: string" # Even though it looks like a number

# Arithmetic context is needed for calculations
RESULT=$((NUMBER + 7))
echo "NUMBER + 7 (arithmetic context): $RESULT"

# Note: If you try to add a non-numeric string, it will error out in arithmetic context.
# Example: $(( "hello" + 5 )) would cause an error.
MESSAGE type: string
NUMBER type: string
NUMBER + 7 (arithmetic context): 130

Python Examples

Python has a rich set of built-in data types, and it automatically manages them.

Example 2.2: Common Python Data Types

# Integers (int)
age = 30
print(f"Age: {age}, Type: {type(age)}")

# Floating-point numbers (float)
price = 19.99
print(f"Price: {price}, Type: {type(price)}")

# Strings (str)
product_name = "Laptop Charger"
print(f"Product Name: {product_name}, Type: {type(product_name)}")

# Booleans (bool)
is_available = True
is_expired = False
print(f"Is Available: {is_available}, Type: {type(is_available)}")
print(f"Is Expired: {is_expired}, Type: {type(is_expired)}")
Age: 30, Type: <class 'int'>
Price: 19.99, Type: <class 'float'>
Product Name: Laptop Charger, Type: <class 'str'>
Is Available: True, Type: <class 'bool'>
Is Expired: False, Type: <class 'bool'>

Example 2.3: Collection Data Types

# Lists (ordered, mutable collections)
fruits = ["apple", "banana", "cherry"]
print(f"Fruits: {fruits}, Type: {type(fruits)}")
print(f"First fruit: {fruits[0]}")
fruits.append("date")
print(f"Fruits after append: {fruits}")

# Tuples (ordered, immutable collections)
coordinates = (10.0, 20.0)
print(f"Coordinates: {coordinates}, Type: {type(coordinates)}")
# coordinates.append(30.0) # This would raise an error: 'tuple' object has no attribute 'append'

# Dictionaries (unordered, mutable key-value pairs)
person = {"name": "Charlie", "age": 40, "city": "New York"}
print(f"Person: {person}, Type: {type(person)}")
print(f"Person's name: {person['name']}")
person['city'] = "London" # Update a value
print(f"Person after city update: {person}")
Fruits: ['apple', 'banana', 'cherry'], Type: <class 'list'>
First fruit: apple
Fruits after append: ['apple', 'banana', 'cherry', 'date']
Coordinates: (10.0, 20.0), Type: <class 'tuple'>
Person: {'name': 'Charlie', 'age': 40, 'city': 'New York'}, Type: <class 'dict'>
Person's name: Charlie
Person after city update: {'name': 'Charlie', 'age': 40, 'city': 'London'}

3. Control Flow: Directing the Program’s Path

Control flow statements determine the order in which individual statements or instructions are executed. The most common types are conditionals (if/else) and loops (for/while).

3.1. Conditionals: Making Decisions (if, else, elif)

Conditionals allow your program to make decisions based on whether a certain condition is true or false.

Bash Examples

Bash uses if [ condition ] or if [[ condition ]] for conditionals. Note the spaces around the brackets and conditions.

Example 3.1.1: Basic if/else

#!/bin/bash

SCORE=85

if [ $SCORE -ge 90 ]; then
  echo "Grade: A"
elif [ $SCORE -ge 80 ]; then
  echo "Grade: B"
elif [ $SCORE -ge 70 ]; then
  echo "Grade: C"
else
  echo "Grade: F"
fi

# Note: The `-ge` is "greater than or equal to". Other operators:
# -eq (equal), -ne (not equal), -gt (greater than), -lt (less than), -le (less than or equal to)
# For string comparison: = (equal), != (not equal)
Grade: B

Example 3.1.2: File Existence Check

#!/bin/bash

FILENAME="myfile.txt"

# Create a dummy file
touch "$FILENAME"

if [ -f "$FILENAME" ]; then
  echo "File '$FILENAME' exists and is a regular file."
  rm "$FILENAME" # Clean up
else
  echo "File '$FILENAME' does not exist."
fi

# Note: Common file test operators:
# -e (exists), -f (regular file), -d (directory), -r (readable), -w (writable), -x (executable)
File 'myfile.txt' exists and is a regular file.

Python Examples

Python uses if, elif (else if), and else with indentation to define blocks.

Example 3.1.3: Basic if/elif/else

temperature = 28

if temperature > 30:
    print("It's hot outside!")
elif temperature >= 20:
    print("It's warm and pleasant.")
else:
    print("It's a bit chilly.")

# Note: Python comparison operators:
# == (equal), != (not equal), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to)
# Logical operators: `and`, `or`, `not`
It's warm and pleasant.

Example 3.1.4: Combining Conditions

age = 17
has_license = True

if age >= 18 and has_license:
    print("You can legally drive.")
elif age >= 16 and has_license:
    print("You can drive with a provisional license.")
else:
    print("You are too young to drive or don't have a license.")
You can drive with a provisional license.

3.2. Loops: Repeating Actions (for, while)

Loops allow you to execute a block of code multiple times, either for a fixed number of iterations or until a certain condition is met.

Bash Examples

Bash for loops can iterate over lists of items, and while loops repeat as long as a condition is true.

Example 3.2.1: for loop (Iterating over a list of strings)

#!/bin/bash

# Iterate over a list of fruits
for fruit in apple banana cherry; do
  echo "I like $fruit."
done

echo ""

# Iterate over numbers using a sequence
for i in {1..5}; do
  echo "Number: $i"
done
I like apple.
I like banana.
I like cherry.

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Example 3.2.2: while loop

#!/bin/bash

COUNTER=1

while [ $COUNTER -le 3 ]; do
  echo "Counter: $COUNTER"
  COUNTER=$((COUNTER + 1)) # Increment counter
done

echo "Loop finished."
Counter: 1
Counter: 2
Counter: 3
Loop finished.

Python Examples

Python’s for loop is typically used for iterating over collections, and while loops are used for condition-based iteration.

Example 3.2.3: for loop (Iterating over lists and ranges)

# Iterate over a list
colors = ["red", "green", "blue"]
for color in colors:
    print(f"Current color: {color}")

print("-" * 20)

# Iterate using `range()` for a fixed number of times
for i in range(3): # This will go from 0 to 2
    print(f"Iteration {i+1}")

print("-" * 20)

# Iterate with start, stop, and step
for i in range(2, 10, 2): # Start at 2, stop before 10, step by 2
    print(f"Even number: {i}")
Current color: red
Current color: green
Current color: blue
--------------------
Iteration 1
Iteration 2
Iteration 3
--------------------
Even number: 2
Even number: 4
Even number: 6
Even number: 8

Example 3.2.4: while loop with break and continue

countdown = 5

while countdown > 0:
    if countdown == 3:
        print("Skipping 3 (continue)...")
        countdown -= 1
        continue # Skip the rest of the current iteration and go to the next
    
    print(f"T-minus {countdown}...")
    
    if countdown == 1:
        print("Liftoff! (break)")
        break # Exit the loop immediately
        
    countdown -= 1

print("While loop finished.")
T-minus 5...
T-minus 4...
Skipping 3 (continue)...
T-minus 2...
T-minus 1...
Liftoff! (break)
While loop finished.

4. Functions: Abstraction and Reusability

Functions (or methods in object-oriented contexts) are named blocks of code designed to perform a specific task. They are fundamental for:

  • Reusability: Write once, use many times.
  • Modularity: Break down complex problems into smaller, manageable pieces.
  • Readability: Give meaningful names to operations.

Bash Examples

Bash functions are defined with the function keyword or simply by naming a block of code. Parameters are accessed positionally ($1, $2, etc.).

Example 4.1: Simple Bash Function

#!/bin/bash

# Define a function
greet() {
  echo "Hello, $1!" # $1 refers to the first argument passed to the function
  echo "Nice to meet you."
}

# Call the function
greet "Dave"
greet "Eve"
Hello, Dave!
Nice to meet you.
Hello, Eve!
Nice to meet you.

Example 4.2: Function with Multiple Arguments and Return Value

#!/bin/bash

# Function to add two numbers
add_numbers() {
  local num1=$1 # `local` keyword makes variables local to the function
  local num2=$2
  local sum=$((num1 + num2))
  echo "$sum" # Functions "return" values by printing to stdout
}

# Call the function and capture its output
result=$(add_numbers 15 7)
echo "The sum is: $result"

result2=$(add_numbers 100 200)
echo "The sum of 100 and 200 is: $result2"
The sum is: 22
The sum of 100 and 200 is: 300

Python Examples

Python functions are defined using the def keyword, can accept arguments, and use return to send values back.

Example 4.3: Simple Python Function

# Define a function that takes one argument
def greet(name):
    """Prints a greeting message to the given name."""
    print(f"Hello, {name}!")
    print("Welcome to the program.")

# Call the function
greet("Frank")
greet("Grace")
Hello, Frank!
Welcome to the program.
Hello, Grace!
Welcome to the program.

Example 4.4: Function with Multiple Arguments and Return Value

# Define a function that adds two numbers and returns the sum
def calculate_sum(a, b):
    """Calculates the sum of two numbers."""
    total = a + b
    return total # Return the result

# Call the function and store the returned value
sum1 = calculate_sum(10, 5)
print(f"Sum of 10 and 5: {sum1}")

sum2 = calculate_sum(23.5, 7.2)
print(f"Sum of 23.5 and 7.2: {sum2}")

# Function with default arguments
def power(base, exp=2):
    """Calculates base raised to the power of exp. exp defaults to 2."""
    return base ** exp

print(f"3 to the power of 2 (default): {power(3)}")
print(f"2 to the power of 4: {power(2, 4)}")
Sum of 10 and 5: 15
Sum of 23.5 and 7.2: 30.7
3 to the power of 2 (default): 9
2 to the power of 4: 16

Conclusion

While these concepts might seem “basic,” their mastery is crucial for writing clean, efficient, and maintainable code. Every complex system, from operating systems to web applications, is ultimately composed of these fundamental building blocks.

By understanding how variables store data, how data types influence operations, how control flow dictates execution paths, and how functions enable modularity, you gain a deeper intuition for debugging, optimizing, and designing robust software. Don’t skip the basics; truly understanding them empowers you to build anything.

Last updated on