Algorithmic Thinking and Python Programming Basics

Algorithmic Thinking and Problem-Solving Framework

  • Algorithmic Thinking Concept:

    • Algorithmic thinking involves breaking down a complex problem into a sequence of clearly defined, logical, step-by-step instructions that can be executed systematically by a computational system.

  • Problem Analysis: Calculating Distance Between Two Points:

    • Consider two points located on a two-dimensional Cartesian coordinate system:

    • Point 1: (x1,y1)(x_1, y_1)

    • Point 2: (x2,y2)(x_2, y_2)

    • The objective is to calculate the Euclidean straight-line distance (cc) separating these two point coordinates.


Two points on a 2D plane connected by a line segment
  • Mathematical Formulation (Pythagorean Theorem):

    • To solve for distance, construct a right-angled triangle where the line segment connecting Point 1 and Point 2 forms the hypotenuse (cc).

    • The horizontal side length (aa) represents the difference between the x-coordinates:     a=x1−x2a = x_1 - x_2

    • The vertical side length (bb) represents the difference between the y-coordinates:     b=y1−y2b = y_1 - y_2

    • The length of hypotenuse (cc) is computed using the Pythagorean formula:     c=a2+b2c = \sqrt{a^2 + b^2}


Right-angled triangle formed between two points (x1, y1) and (x2, y2) showing side lengths a, b, and hypotenuse c
  • Concrete Worked Example:

    • Given Point 1 at (1,5)(1, 5) and Point 2 at (5,2)(5, 2), where x1=1x_1 = 1, y1=5y_1 = 5, x2=5x_2 = 5, and y2=2y_2 = 2:

    • Calculate horizontal difference aa:     a=1−5=−4a = 1 - 5 = -4

    • Calculate vertical difference bb:     b=5−2=3b = 5 - 2 = 3

    • Calculate straight-line distance cc:     c=(−4)2+(3)2=16+9=25=5c = \sqrt{(-4)^2 + (3)^2} = \sqrt{16 + 9} = \sqrt{25} = 5


Right triangle example with coordinates (1,5) and (5,2)

Process Description Methods: Flowcharts and Pseudocode

  • Three Core Components of Program Design:

    • Output: The final result or target value required to solve the problem (e.g., the distance value cc).

    • Process: The logical sequence and computational rules used to transform inputs into outputs (e.g., applying the Pythagorean theorem).

    • Inputs: The explicit data items needed to carry out the process (e.g., coordinate values x1,y1,x2,y2x_1, y_1, x_2, y_2).

  • Flowcharts:

    • Flowcharts utilize universal graphical symbols to illustrate the exact sequence of operational steps in a program algorithm.

    • Standard Flowchart Block Definitions:

    • Start / End: Represented by an oval shape. Indicates where the execution flow begins or terminates.

    • Process: Represented by a rectangle shape. Denotes internal calculation, assignment, or manipulation steps.

    • Input / Output: Represented by a parallelogram shape. Specifies reading data into the system or displaying output data.

    • Flow: Represented by a directional arrow line. Defines the path and direction of program execution flow.


Flowchart Start and End block symbol


Flowchart Process block symbol


Flowchart Input/Output block symbol


Flowchart Flow direction arrow symbol
  • Step-by-Step Flowchart Construction for Distance Computation:

    1. Draw the Start block (oval).

    2. Read input data values sequentially using input blocks (parallelograms): Read x1, Read y1, Read x2, Read y2.

    3. Perform calculations in process blocks (rectangles):

    • a=x1−x2a = x_1 - x_2

    • b=y1−y2b = y_1 - y_2

    • c=a2+b2c = \sqrt{a^2 + b^2}

    1. Display output in an output block (parallelogram): Display c.

    2. Draw the End block (oval).

  • Pseudocode Specification:

    • Pseudocode is an informal, human-readable outline of a computer algorithm described in structured plain text statements.

    • Formal Pseudocode Steps for Distance Calculation:

    1. Read x1x_1

    2. Read y1y_1

    3. Read x2x_2

    4. Read y2y_2

    5. Calculate a=x1−x2a = x_1 - x_2

    6. Calculate b=y1−y2b = y_1 - y_2

    7. Calculate c=a2+b2c = \sqrt{a^2 + b^2}

    8. Display cc

User Prompting and Interactive Program Execution

  • Definition of User Prompting:

    • Prompting is the action of displaying descriptive output text to guide, persuade, or remind a user to enter specific required input data into the program.

  • Incorporating Prompts into Flowcharts and Pseudocode:

    • Prompting adds dedicated input/output interaction steps before reading each variable.

    • Updated Pseudocode Sequence with User Prompts:

    1. Prompt the user to enter the x coordinate of point 1

    2. Read x1x_1

    3. Prompt the user to enter the y coordinate of point 1

    4. Read y1y_1

    5. Prompt the user to enter the x coordinate of point 2

    6. Read x2x_2

    7. Prompt the user to enter the y coordinate of point 2

    8. Read y2y_2

    9. Calculate a=x1−x2a = x_1 - x_2

    10. Calculate b=y1−y2b = y_1 - y_2

    11. Calculate c=a2+b2c = \sqrt{a^2 + b^2}

    12. Display cc


Flowchart with explicit user prompting steps for calculating distance
  • Command-Line Interface Execution Trace:

    • Execution command: python Calculate_Distance.py

    • Interaction sequence:

    • Please Enter the value for x1: 1

    • Please Enter the value for y1: 5

    • Please Enter the value for x2: 5

    • Please Enter the value for y2: 2

    • Final Output: The distance between the two points is 5

Python Variables, Naming Conventions, and Data Types

  • Python Variables and Identifier Rules:

    • Variables store computational values in memory and are referenced using identifiers (names).

    • Allowed characters for identifiers: Letters (a–z, A–Z), digits (0–9), and underscores (_).

    • Identifier Rules:

    • Must start with a letter or an underscore (_).

    • Cannot start with a numeric digit.

    • Identifiers are case-sensitive (e.g., Speed is completely distinct from speed).

    • Cannot use reserved Python keywords (e.g., True, int, for, class).

    • Examples of valid identifiers: speed, item_1, _pirate, nam3.

    • Variable Assignment: Data is stored in variables using the single equals sign assignment operator (=): ```python