WK2: Procedural Programming and Control Structures in Python (Session 1)

Welcome
Procedural Programming and Control Structures in Python
Week 2
Module Lecturer: Dr Raghav Kovvuri
Email: raghav.kovvuri@ieg.ac.uk

1 / 21
next
Slide 1: Slide
ProgrammingHigher Education (degree)

This lesson contains 21 slides, with text slides.

Items in this lesson

Welcome
Procedural Programming and Control Structures in Python
Week 2
Module Lecturer: Dr Raghav Kovvuri
Email: raghav.kovvuri@ieg.ac.uk

Slide 1 - Slide

Retrospect: Flowcharts
  • A flowchart is a graphical representation of an algorithm or process
Key components:
  • Oval: Start/End
  • Rectangle: Process/Action
  • Diamond: Decision
  • Parallelogram: Input/Output
  • Arrows: Flow direction
Benefits:
  • Visual clarity of process flow
  • Easy to understand and communicate
  • Helpful in identifying bottlenecks

Slide 2 - Slide

Example: Flow Chart

Slide 3 - Slide

Retrospect: Pseudocode Pseudocode 
  • Pseudocode is a informal, high-level description of an algorithm
Key Components
  • Input/Output statements
  • Assignment operations
  • Conditional statements (IF-THEN-ELSE)
  • Loops (WHILE, FOR)
  • Function calls
Characteristics
  • Written in a mixture of natural language and simple programming constructs
  • Not tied to any specific programming language
  • Focuses on logic rather than syntax

Slide 4 - Slide

Example: Pseudocode

Slide 5 - Slide

Procedural Programming (1)
Definition: Programming paradigm based on the concept of procedure calls
Key Characteristics:

  • Sequence: Instructions executed in order
  • Selection: Decision making (if-else statements)
  • Iteration: Repetition of a process (loops)
  • Modularity: Breaking code into reusable procedures or functions

Slide 6 - Slide

Comparison with Other Paradigms:
  • Object-Oriented: Focuses on objects that contain data and code
  • Functional: Emphasizes the application of functions to solve problems
Procedural Programming (2)
Advantages:
  • Simplicity and ease of implementation
  • Code reusability through functions
  • Straightforward flow of control

Slide 7 - Slide

Python Functions
Basics and Parameters
  1. Defining Functions:

Slide 8 - Slide

Function Parameters 
  • Positional arguments: Order matters
  • Keyword arguments: Order doesn't matters
  • Default parameters:

Slide 9 - Slide

Return Values
  • Single return value:
  • Multiple return value:

Slide 10 - Slide

Scope and Lifetime of Variables (1)
  • Local variables: Defined inside a function, only accessible within that function 
  • Global variables: Defined outside functions, accessible throughout the program

Slide 11 - Slide

Lifetime of Variables (2) 
  • The global keyword: Used to modify global variables within a function

Slide 12 - Slide

Control Structures
Conditional Statements
  • If Statements:
  • If-Else Statements:

Slide 13 - Slide

Conditional Statements (2)
  • Nested If Statements
  • If-Elif-Else Statements

Slide 14 - Slide

Conditional Statements(3)
  • Ternary Operator:

Slide 15 - Slide

Control Structures 
Loops (1)
'For' Loop:
  • Iterating over a range:
  • Iterating over a list:

Slide 16 - Slide

Loops (2)
  • 'While' Loop
  • Nested Loops

Slide 17 - Slide

Loops (3)
Loop Control Statements:

break: Exit the loop prematurely
continue: Skip the rest of the current iteration
else clause in loops: Executed when loop completes normally

Slide 18 - Slide

Practical Exercise
Simple Calculator
Task: Create a simple calculator that can perform basic arithmetic operations.
  • Download Simple Calculator.py from Canvas and comment and run the code accordingly

Slide 19 - Slide

Key Takeaways
Procedural Programming Fundamentals:
  • Sequence, Selection, Iteration
  • Modularity through functions
Python Implementation:
  • Functions: Definition, parameters, return values, scope
  • Control Structures: Conditional statements, loops
Importance of Algorithms:
  • Flowcharts and pseudocode for planning
  • Translating algorithms into code

Slide 20 - Slide

Slide 21 - Slide