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
suivant
Slide 1: Diapositive
ProgrammingHigher Education (degree)

Cette leçon contient 21 diapositives, avec diapositives de texte.

Éléments de cette leçon

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

Slide 1 - Diapositive

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 - Diapositive

Example: Flow Chart

Slide 3 - Diapositive

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 - Diapositive

Example: Pseudocode

Slide 5 - Diapositive

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 - Diapositive

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 - Diapositive

Python Functions
Basics and Parameters
  1. Defining Functions:

Slide 8 - Diapositive

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

Slide 9 - Diapositive

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

Slide 10 - Diapositive

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 - Diapositive

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

Slide 12 - Diapositive

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

Slide 13 - Diapositive

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

Slide 14 - Diapositive

Conditional Statements(3)
  • Ternary Operator:

Slide 15 - Diapositive

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

Slide 16 - Diapositive

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

Slide 17 - Diapositive

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 - Diapositive

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 - Diapositive

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 - Diapositive

Slide 21 - Diapositive