DSA - Programming Fundamentals with Examples

Basic Programming Language:

Knowledge of a programming language such as Python, Java, C++, or JavaScript:

  • Importance:
    • In order to understand data structures and algorithms, you must be comfortable with a programming language. Language choice depends on personal preference, project requirements, or industry standards.
  • Key Concepts:
    • Syntax: Understanding the language's syntax rules.
    • Variables: Declaring, assigning values to, and using variables.
    • Control Flow: Employing conditional statements (if, else) and loops (for, while).
    • Functions: Define functions for code modularization and reusability.
    • Data Types: Understanding different data types such as integers, floats, strings, and their usage.
  • Example (Python):
  • # Python example 
    def add_two_nums(x, y):
      return (x + y)
    print(add_two_nums(5, 6))
  • Recommendation:
    • Choose a language based on your goals, community support, and the type of project you are interested in. Python is often recommended for beginners due to its readability and versatility.

Variables and Data Types:


Variables are named storage locations in a program that hold data values. During program execution, they can be used to reference and manipulate data.

  • Purpose: To store and manage information, allowing dynamic processing in a program.
  • Example (in Python):
  • age = 20 # 'age' is a variable storing an integer value
    name = "Myrto Keola" # 'name' is a variable storing a string value
    price = 10.34 # 'price' is a variable storing a floating-point value

Data Types:

  • Data types define the nature of data that a variable can hold. They specify the range of values and the operations on the data.
  • Common Data Types:
    • Integer (int): Whole numbers without decimal points (e.g., 7, -2, 0).
    • Floating-Point (float): Numbers with decimal points (e.g., 3.14, -0.5, 6.0).
    • String (str): Sequence of characters (e.g., "Hello, World!", '1234').
    • Boolean (bool): Represents true or false values.
      • Example:
      • name = "Darius Troilus" # String data type
        age = 22 # Integer data type
        height = 1.70 # Floating-point data type
        is_adult = False # Boolean data type


  • Variables: Provide named references to values, enabling manipulation and storage of dynamic data.
  • Data Types: Define the characteristics and operations associated with different types of data, ensuring proper handling and interpretation by the program.

Note: All the above code is written in Python language.

Control Flow:

Control flow refers to the order in which statements are executed in a program. It involves making decisions with conditional statements and repeating tasks with loops. A program's control flow ensures that instructions are executed in the correct order.

Key components:

  • Conditional Statements:
    • If Statement:
      • Executes a block of code if a specified condition is true.
      • Example (in Python):
      • age = 20
        if age >= 18:
        print("You are an adult.")
    • Else Statement:
      • Provides an alternative block of code to execute if the condition in the if statement is false.
      • Example (in Python):
      • age = 14
        if age >= 18:
        print("You are an adult.")
        print("You are a minor.")
    • Else-If (elif) Statement:
      • Allows the program to test multiple conditions in sequence.
      • Example (in Python):
      • marks = 92
        if marks >= 90:
        elif marks >= 80:
    • Loops:
      • For Loop:
        • Executes a block of code a specified number of times.
        • Example (in Python):
        • for i in range(5): 
      • While Loop:
        • Repeatedly executes a block of code as long as a specified condition is true.
        • Example (in Python):
        • ctr = 0
          while ctr < 12:
          ctr += 1


  • Conditional Statements: Specify whether certain conditions are true or false before executing specific code blocks.
  • Loops: Facilitate code repetition, reducing redundancy and allowing efficient handling of repetitive tasks.

Note: All the above code is written in Python language.


A function is a named block of reusable code that performs a specific task or set of tasks. Functions modularize code, making programs more organized, readable, and maintainable. They encapsulate a series of operations and can be invoked (called) multiple times within a program.

Key Components:

  • Function Definition:
    • Syntax (in Python):
    • def function_name(parameters):
      # Function body (code block)
      # Perform operations
      return result # Optional return statement
  • Function Call:
    • Syntax (in Python):
    • result = function_name(argument_values)
  • Parameters:
    • Variables that represent input values passed to a function.
    • Example (in Python):
    • def greet(name):
      print(f"Hello, {name}!")
      greet("Shevaun") # "Shevaun" is the argument passed to the 'name' parameter
  • Return Statement:
    • Specifies the value that a function should return to the calling code.
    • Example (in Python):
    • def multiply_numbers(x, y):
      return x * y
      result = multiply_numbers(5, 4) # result will be 20


  • Function Definition: Involves specifying the function's name, parameters, and the sequence of operations it should perform.
  • Function Call: Involves invoking the function by its name and passing appropriate arguments.


  • Functions promote code reusability, as the same set of operations can be executed with different inputs.
  • They enhance code readability by breaking down complex tasks into smaller, manageable functions.
  • Functions encapsulate logic, allowing developers to focus on modular components and reducing the risk of errors.

Example (in Python):

def cube_num(x):
     return x ** 3
def print_cube_numbers(numbers):
    for num in numbers:
     result = cube_num(num)
     print(f"The cube of {num} is {result}.")
cube_numbers = [1, 2, 3, 4, 5]

In this example, cube_numis a function that computes the cube of a number, and "print_cube_numbers" is a function that utilizes the first function to print cubes of a list of numbers.

Function with Default Argument (in Python):

Here, the function greet has a default argument name='Guest', so if no argument is provided, it will greet 'Guest'.


def greet(name='Guest'):
    """This function greets the person passed in as an argument, defaulting to 'Guest' if no argument is provided."""
    return f"Hello, {name}!"
# Example usage:


Hello, Guest!

Note: All the above code is written in Python language.

Data Structures: Arrays and Lists

Arrays and lists are data structures used to store and organize collections of elements in a programming language. They provide a way to group related data items under a single variable, allowing for efficient storage, retrieval, and manipulation of the elements.

Key components:

  • Arrays:
    • An array is a fixed-size, sequential collection of elements of the same data type stored in contiguous memory locations.
    • Characteristics:
      • Elements are accessed by their index.
      • All elements in an array are of the same data type.
      • Memory allocation is contiguous.
    • Example (in Python):
    • numbers = [1, 2, 3, 4, 5]
  • Lists:
    • A list is a dynamic, resizable collection of elements of different data types.
    • Characteristics:
      • Elements are accessed by their index.
      • A list can contain different types of elements.
      • Memory allocation is dynamic and managed by the programming language.
    • Example (in Python):
    • n = ["Brunella", "Osvaldo", "Stephanos", 500]

Common Operations:

  • Accessing elements:
    • Elements in arrays and lists are accessed by their index.
    • Example (in Python):
    • numbers = [1, 2, 3, 4, 5] 
      print(numbers[2]) # Output: 3
  • Added elements:
    • New elements can be added to arrays and lists.
    • Example (in Python):
    • names = ["Dorian", "Euryalos"] 
  • Updating elements:
    • Elements in arrays and lists can be modified.
    • Example (in Python):
    • numbers = [1, 2, 3, 4, 5] 
      numbers[2] = 10
  • Removing elements:
    • Elements can be removed from arrays and lists.
    • Example (in Python):
    • names = ["Brunella", "Osvaldo", "Stephanos"] 


  • Arrays: Fixed-size collections with elements of the same data type, accessed by index.
  • Lists: Dynamic collections with resizable capacity and elements of different data types.

Basic Input/Output:

Basic Input/Output (I/O) refers to the fundamental operations in a programming language that involve reading input from a user or external source and displaying output to the console or another destination. The purpose of these operations is to enable communication between a program and a user or host system.

Key components:

  • Input operations:
    • Reading data from an external source, typically provided by a user or another program.
    • Common input functions:
      • input() function (for user input).
      • Reading from files, databases, or other sources.
    • Example (in Python):
    • user_input = input("Input your name: ")
  • Output operations:
    • Displaying data to the console or sending it to an external destination.
    • Common output functions:
      • print() function (for displaying output to the console).
      • Writing to files, databases, or other output channels.
    • Example (in Python):
    • print("Hello, World!")

User input handling:

  • Reading User Input:
    • Example (in Python):
    • user_input = input("Input a number: ")
  • Converting input types:
    • Input is often read as strings and may need conversion to other data types.
    • Example (in Python):
    • user_input = input("Input an integer: ") integer_value = int(user_input)

Console output:

  • Displaying output:
    • Example (in Python):
    • print("Welcome to the program!")
  • Formatting output:
    • Control the format and layout of displayed information.
    • Example (in Python):
    • name = "Saba" 
      age = 22
      print(f"Name: {name}, Age: {age}")


  • Input Operations: Involve obtaining data from users or external sources.
  • Output Operations: Present information to users or external destinations.

Follow us on Facebook and Twitter for latest update.