Python Programming Fundamentals and Practical Exercises

Lecture 1: Program Design

Flowchart and Pseudocode

  • Pseudocode: Abbreviated plain English version of actual computer code.
  • Symbols used in flowcharts are replaced by English-like statements.
  • Allows the programmer to focus on the steps required to solve a problem.

Hierarchy Chart

  • Shows the overall program structure.
  • Depicts the organization of the program, omitting specific processing logic.
  • Describes what each part, or module, of the program does.
  • Each module is subdivided into a succession of submodules.

Three Structures

  • Sequence structure
  • Decision structure
  • Repetition/loop structure

Lecture 2: Python Basics and Error Handling

Common math functions: abs(-23), math.ceil(3.0001) (round up), math.floor(3.9999) (round down), round(4.5), random.randint(1, 100).

Type conversion: type(6) (integer), int(2.7) (2), float(2) (2.0).

Three Types of Errors

  • Syntax errors: Grammatical and punctuation errors (e.g., extra parentheses, wrong syntax like ;).
  • Runtime errors: Errors discovered while the program is running (e.g., invalid data input or inaccessible files).
  • Logic errors: Occur when a program does not perform as intended (e.g., average = (firstNum + secondNum) / 2).

Lecture 3: String Manipulation

Strings are indexed sequences. school = "CityU", school[0] is 'C'.

  • Slicing: str1[2:7], str1[2:], str1[:8].
  • Negative Index: Positive Index – Length.
  • Methods: len(), upper(), lower(), count(), capitalize(), title(), rstrip().

Lecture 4: Lists and Tuples

  • Lists: Mutable sequences defined with square brackets [].
  • Tuples: Immutable sequences defined with parentheses ().
  • Operations: append(), extend(), del, remove(), clear(), sort(), split(), join().

Lab Exercises

Lab 2: Financial Calculations

Includes Future Value, Present Value, and Ordinary Annuity calculations using user input for interest rates and time periods.

Lab 3: Randomization and Encryption

Includes generating random letters, finding alphabet positions, creating random passwords, and a basic 3-letter Caesar-style cipher.

Lab 4: Advanced List and Tuple Processing

Includes string splitting, nested list manipulation, and paragraph analysis (counting sentences and words).

Lab 5: Conditional Logic

Includes alphabet indexing, grade lookups, and complex tax calculation logic based on salary and marital status.

Lab 6 & 7: Algorithms

Includes finding the largest of three numbers, sorting integers, and leap year validation.