Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:

Slides:



Advertisements
Similar presentations
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Advertisements

Selection (decision) control structure Learning objective
Tutorial #6 PseudoCode (reprise)
Flow Charting, Structured English and PseudoCode
Lecture 7 Sept 17 Goals: Complete Chapter 4 Chapters 5 and 6.
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Chapter 5 - Exercises Exercise 5.2. Write a script swap.m that swaps the values of variables a and b. For example:
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Python quick start guide
Programming Training Main Points: - Working with Functions in Python
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Iteration: WHILE Loop Damian Gordon. WHILE Loop Consider the problem of searching for an entry in a phone book with only SELECTION:
Searching Damian Gordon. Google PageRank Damian Gordon.
1 What We Will Learn  Introduction  Passing input parameters  Producing output  Scope of variables  Storage Class of variables  Function usage example.
Lecture 7 Sept 22 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Python: Arrays Damian Gordon. Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”.
Modularisation Damian Gordon. Modularisation Let’s imagine we had code as follows:
Introduction to Python Damian Gordon. The Python Programming Language Python was developed by Guido van Rossum in the Netherlands in Van Rossum.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 27 Classes and Functions 6/17/09 Python Mini-Course: Lesson 27 1.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CS1301 Introduction To Computer Programming (11-12 Semester B) Lecture Exercise (Wk8) Q.1 Write the code that asks the user.
Recursion Damian Gordon. Recursion Recursion: Factorial Recursion is a feature of modularisation, when a module can call itself to complete a solution.
Python: Stacks and Queues (as a Linked List) Damian Gordon.
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Recursion Damian Gordon. Recursion Factorial Fibonacci Decimal to Binary conversion Travelling Salesman Problem Knight’s Tour.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Introduction to Python Damian Gordon e:
Python Programming Module 3 Functions Python Programming, 2/e1.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Iteration: FOR, DO, LOOP Loop Damian Gordon. FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a.
Python: Menu-Driven Programs Damian Gordon. Menu-Driven Programs These are programs that display a menu (or list of options), and allows the user to choose.
Introduction to Python
More on Functions (Part 2)
Modules and Packages Damian Gordon.
Python: Logic Gates Damian Gordon.
Linked Lists Damian Gordon.
Organization of Programming Languages
Programming – syllabus knowledge
Writing Functions( ) (Part 5)
Adapted from slides by Marty Stepp and Stuart Reges
Boolean Logic Damian Gordon.
Passing Parameters by value
Queues: Implemented using Arrays
Patterns to KNOW.
Nate Brunelle Today: Functions again, Scope
Python: Algorithms Damian Gordon.
Programming Training Main Points: - Working with Functions in Python
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
More on Functions (Part 2)
More on Functions (Part 2)
Module 3 Selection Structures 4/4/2019 CSE 1321 Module 3.
Module 4 Loops and Repetition 4/4/2019 CSE 1321 Module 4.
Structured Programming
Circular Queues: Implemented using Arrays
Singleton Pattern Damian Gordon.
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Python: Stacks and Queues (as an Array)
Some Common Issues: Common Algorithms
Queues: Implemented using Linked Lists
REPETITION Why Repetition?
Python Reserved Words Poster
Presentation transcript:

Python: Structured Programming Damian Gordon

Structured Programming Remember the modularised version of the prime number checking program:

################ # Main Program # ################ # PROGRAM CheckPrime: if IsItPrime() == True: # THEN print("Prime number") else: print("Not a prime number") # ENDIF; # END.

######################### # Prime Checking Module # ######################### def IsItPrime(): a = int(input("Please input value: ")) b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; return IsPrime # END IsItPrime.

Structured Programming In this version, the MAIN program the MAIN program calls the MODULE IsItPrime() which does the reading in of the value, and the checking to see if it is prime or not. Because good module design says each module should do one thing well, as opposed to two or three things kinda well, we should rewrite so that the MODULE called IsItPrime() just checks if a number is prime (and that number is passed into it).

################ # Main Program # ################ # PROGRAM CheckPrime: a = int(input("Please input value: ")) if IsItPrime(a) == True: # THEN print("Prime number") else: print("Not a prime number") # ENDIF; # END.

################ # Main Program # ################ # PROGRAM CheckPrime: a = int(input("Please input value: ")) if IsItPrime(a) == True: # THEN print("Prime number") else: print("Not a prime number") # ENDIF; # END.

######################### # Prime Checking Module # ######################### def IsItPrime(a): b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; return IsPrime # END IsItPrime.

######################### # Prime Checking Module # ######################### def IsItPrime(a): b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; return IsPrime # END IsItPrime.

Variable Scope The scope of a variable – is the part of a computer program where the binding is valid: where the variable name can be used to refer to the entity. In other parts of the program the variable name may refer to a different entity (it may have a different binding), or to nothing at all (it may be unbound). The scope of a binding is also known as the “visibility” of a variable.

Structured Programming # PROGRAM Global and Local Variables global_var = "This is a global variable" print(global_var) # END.

Structured Programming # PROGRAM Global and Local Variables global_var = "This is a global variable" print(global_var) # END. This is a global variable

Structured Programming # PROGRAM Global and Local Variables global_var = "This is a global variable" def MyMethod(): print(global_var) # END MyMethod MyMethod() print(global_var) # END.

Structured Programming # PROGRAM Global and Local Variables global_var = "This is a global variable" def MyMethod(): print(global_var) # END MyMethod MyMethod() print(global_var) # END. This is a global variable

Structured Programming # PROGRAM Global and Local Variables global_var = "This is a global variable" def MyMethod(): global_var = “Local copy of the global variable" print(global_var) # END MyMethod MyMethod() print(global_var) # END.

Structured Programming # PROGRAM Global and Local Variables global_var = "This is a global variable" def MyMethod(): global_var = “Local copy of the global variable" print(global_var) # END MyMethod MyMethod() print(global_var) # END. This is a local copy of the global variable This is a global variable

Structured Programming # PROGRAM Global and Local Variables global_var = "This is a global variable" def MyMethod(): global_var = “Local copy of the global variable" print(global_var) global global_var print(global_var) # END MyMethod MyMethod() print(global_var) # END.

Structured Programming # PROGRAM Global and Local Variables global_var = "This is a global variable" def MyMethod(): global_var = “Local copy of the global variable" print(global_var) global global_var print(global_var) # END MyMethod MyMethod() print(global_var) # END. This is a local copy of the global variable

Structured Programming # PROGRAM Global and Local Variables global_var = 4 print(global_var) # END.

Structured Programming # PROGRAM Global and Local Variables global_var = 4 print(global_var) # END. 4

Structured Programming # PROGRAM Global and Local Variables global_var = 4 def MyMethod(): print(global_var) # END MyMethod MyMethod() print(global_var) # END.

Structured Programming # PROGRAM Global and Local Variables global_var = 4 def MyMethod(): print(global_var) # END MyMethod MyMethod() print(global_var) # END. 4444

Structured Programming # PROGRAM Global and Local Variables global_var = 4 def MyMethod(): global_var = 8 print(global_var) # END MyMethod MyMethod() print(global_var) # END.

Structured Programming # PROGRAM Global and Local Variables global_var = 4 def MyMethod(): global_var = 8 print(global_var) # END MyMethod MyMethod() print(global_var) # END. 8484

etc.