Introduction to Programming Concepts and Python

Fundamental Computer Programming Concepts
  • Course Overview:

    • Course Code: CSPG1205 - Introduction to Programming.

    • Core Outcome: Define computer programming concepts.

  • Definition of a Computer Program:

    • A computer program is a sequence of explicit instructions specifying how a computer performs a computation.

    • Computations fall into three primary categories:

    • Mathematical: Involves numerical operations and solving algebraic or complex equations.

    • Symbolic: Involves textual manipulations such as searching, editing, or replacing text within a document.

    • Graphical: Involves rendering, manipulating, and processing visual data such as digital images, video streams, and graphical user interfaces.

Problem Solving in Computer Science
  • The Importance of Problem Solving:

    • Problem solving is defined as the ability to formulate clear problems, think creatively about potential solutions, and express those solutions accurately and unambiguously.

    • It represents the single most important core skill for a computer scientist.

    • Learning computer programming serves as a practical framework to cultivate and exercise problem-solving capabilities.

  • Practical Example: Calculating Journey Speed:

    • Scenario: A traveler wants to determine the average speed achieved during a long journey.

    • Procedural Steps:

    1. Determine or measure the total distance traveled.

    2. Determine or measure the total travel duration (time).

    3. Calculate speed by dividing the distance value by the travel time value:        Speed=DistanceTime\text{Speed} = \frac{\text{Distance}}{\text{Time}}

    4. Output or display the resulting speed value.

Human Languages vs. Computer Languages
  • Concept of Language:

    • A language is a structured tool used to express and record thoughts and information.

    • Human interaction relies on natural languages (such as English).

  • Machine Language:

    • Computers operate using machine language (binary code), which consists strictly of sequences of zeros (00) and ones (11).

    • Machine language is directly tied to computer hardware, making it non-portable across different hardware platforms.

  • Bridging the Communication Gap:

    • Programming languages provide a common intermediate medium, allowing humans to express logical instructions in a readable syntax that a computer can translate into machine code for execution.

Programming Languages and the Role of a Programmer
  • Programming Language:

    • A formal language comprising a set of instructions used to produce various kinds of output and create software applications.

    • Examples include: Python, C++, JavaScript, Scala, Java, Node.js, PHP, Ruby, and C#.

  • Programmer:

    • A person skilled in the art of programming who authors source code for computer software and mobile applications.

    • Key responsibilities include writing initial source code, testing programs, maintaining software infrastructure, and debugging errors.

Core Building Blocks of Computer Programs

Regardless of the programming language used, virtually all programs are constructed from six fundamental building blocks:

  • Input: Obtaining data from the external environment (e.g., keyboard, file on disk, hardware sensors like microphones or GPS).

  • Output: Presenting processed data to the user or system (e.g., rendering graphics/text on screen, writing to a file, playing audio through speakers).

  • Sequential Execution: Executing statements step-by-step in the exact sequential order in which they appear in the source code file.

  • Conditional Execution: Evaluating logical conditions to dynamically determine execution paths—executing specific blocks of statements if conditions are met or skipping them if false.

  • Repeated Execution: Executing a block of code multiple times in an iterative loop.

  • Reuse: Packaging a set of instructions into a named unit that can be invoked repeatedly throughout a program.

Introduction to Python
  • Historical Background:

    • Python was created by Guido van Rossum and first released in 1991.

    • It was named after the popular British comedy troupe "Monty Python's Flying Circus".

  • Core Definition:

    • Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.

    • It combines minimalist, elegant syntax with powerful features and high-level data structures.

  • Industry Usage and Applications:

    • Widely deployed across scientific computing, web backends, data science, machine learning, and operating system automation.

    • Major organizations using Python include: Google, NASA, Yahoo! (and Yahoo! Maps), Nokia, Amazon, Dropbox, Reddit, Instagram, Facebook, IBM, Quora, Netflix, and Walt Disney Feature Animation.

Key Features of Python
  • Simple & Easy to Learn:

    • Clean, minimalist syntax that is extraordinarily easy to get started with.

  • Free and Open Source (FLOSS):

    • Distributed under Free/Libré and Open Source Software licenses.

    • Anyone can freely distribute copies, inspect source code, modify it, and reuse components in new free programs.

  • High-level Language:

    • Abstracted from low-level computer hardware features; developers do not need to manually handle low-level memory management.

  • Portable:

    • Cross-platform functionality allows Python code to run unchanged on various operating systems.

  • Interpreted:

    • Executed directly from source code. Internally, the interpreter converts source code into intermediate bytecodes, which are translated into native computer instructions and executed.

  • Object-Oriented & Procedural:

    • Procedural Programming: Execution follows commands sequentially through subroutines/procedures to reuse code and manipulate data.

    • Object-Oriented Programming: Programs are built around objects that combine data and functionality using classes and objects.

  • Extensible:

    • Performance-critical algorithms can be written in compiled languages like C or C++ and integrated directly into Python programs.

  • Embeddable:

    • The Python interpreter can be embedded into C or C++ programs to grant scripting capabilities to end-users.

  • Extensive Standard Library:

    • Includes a broad standard library supporting tasks such as regular expressions, multi-threading, database interaction, web browsing, HTML handling, and Graphical User Interface (GUI) development.

Language Translation Mechanics: Compilers vs. Interpreters
  • Machine Language Requirement:

    • CPU understands machine language consisting of zeros (00) and ones (11).

  • Compiler:

    • Processes the entire high-level source code file at once.

    • Translates source code into an executable machine language file (e.g., .exe or .dll files on Windows) prior to program execution.

    • Workflow: Source Code →\rightarrow Compiler →\rightarrow Machine Code →\rightarrow Output.

  • Interpreter:

    • Reads, parses, and executes source code line-by-line interactively at runtime.

    • Workflow: Source Code →\rightarrow Interpreter →\rightarrow Output.

    • Python execution model: Source Code (.py) →\rightarrow Intermediate Bytecode (.pyc) →\rightarrow Python Virtual Machine (PVM).

Interactive Python Execution via Shell
  • Running Python interactively allows executing code on the fly and evaluating expressions line-by-line.

  • Interactive Shell Session Examples:

    • Addition: >>> 3+4 outputs 7

    • Multiplication: >>> 3*4 outputs 12

    • Division: >>> 15/3 outputs 5.0

    • Absolute value: >>> abs(-5) outputs 5

    • Exponentiation: >>> 3**2 outputs 9

Anatomy of a Basic Python Script
# program begins
print("Hello world!")
# program ends
  • Line 1 (# program begins): Single-line comment initiated by #, ignored by the interpreter and used for code readability.

  • Line 2 (print("Hello world!")): Calls the built-in print() function to output text to the console and appends a newline.

  • Line 3 (# program ends): A closing single-line comment.

Development Environment: Jupyter Notebooks & Anaconda
  • Jupyter Notebook Features:

    • Interactive web application combining executable code, computational output, styled rich text, and data visualizations into a single document.

  • Launching & Creating Notebooks:

    • Open Anaconda Navigator desktop application and click Launch on Jupyter Notebook.

    • In the Jupyter file dashboard, click New and select Python 3.

  • User Interface & Cell Types:

    • Includes Notebook Name, Menu Bar, Toolbar, and Code Cells.

    • Code Cell: Contains executable code and displays output directly below.

    • Markdown Cell: Contains styled text using Markdown and displays formatted text in-place when run.

    • Raw Cell: Contains plain text without formatting or execution.

  • Essential Notebook Shortcuts & Tips:

    • Execute Cell: Press Shift + Enter or Ctrl + Enter.

    • Run All Cells: Navigate to menu option Cell / Runtime →\rightarrow Run All.

    • Line Numbers: Toggle code line numbers via View →\rightarrow Toggle Line Numbers.

    • Exporting: Download notebooks as .ipynb, .py, .pdf, or .html via File →\rightarrow Download As.

Academic References
  1. Severance, Charles R. (2016). Python for Everybody: Exploring Data Using Python 3. Open Education Resource.

  2. Swaroop, C. H. (2013). A Byte of Python. Open Education Resource.

  3. Downey, Alan B. (2016). Think Python: How to Think Like a Computer Scientist. Open Education Resource.

  4. Romano, Fabrizio (2018). Learn Python Programming: A Beginner's Guide to Learning the Fundamentals of Python Language to Write Efficient, High-Quality Code, 2nd Edition. Packt Publishing Limited.