Computer science
  • Exam boards
    • OCR past papers >
      • Paper1 (Sample)
    • Edexcel past papers >
      • 2020 Current spec (coming soon) >
        • Programming Language Subset (PLS)
      • Old 2016 SPEC >
        • Paper 1 (specimen1)
        • Paper 1 (specimen2)
        • Paper 1 (2018)
        • Paper 1 (2019)
        • Paper 2 (specimen1)
        • Paper 2 (2018)
        • Paper 2 (2019)
      • OLD 2013 SPEC >
        • Sample assessment 1 (old spec)
        • Sample assessment 2 (old spec)
        • June 2017 (old spec)
        • June 2016 (old spec)
        • June 2015 (old spec)
    • Paper 1 (by topic) >
      • Algorithms
      • Application software
      • Ascii
      • Binary
      • Cache
      • Client Server
      • Cloud
      • Compression
      • Computational thinking
      • CPU
      • Cyber security
      • Database
      • Fetch decode execute cycle
      • Embedded system
      • Encryption
      • File size
      • Flow chart
      • Hexadecimal
      • High Level language
      • ISP/Internet
      • Logic
      • Magnetic HDD
      • Networking
      • Open source
      • Operating system
      • Peer to peer
      • Images (Data representation)
      • Protocols
      • RAM
      • ROM
      • Run length encoding
      • Simulation software
      • Solid state HDD
      • Sound
      • Stored program concept
      • Utility software
    • Curriculum >
      • Binary
      • Hex
      • Data Representation
      • Compression
      • Encryption
      • What is a computer
      • How does a computer work
      • Hardware
      • Operating system
      • The CPU
      • Input devices
      • Data storage
      • Networks
      • Microcontrollers
      • Computational models
      • Emerging trends
      • Virtual machine
      • Models and simulators
      • Algorithms
    • Python >
      • Finding python
      • GCSE Python questions
      • Python coding >
        • Comments
        • Data types
        • Variables
        • If Else
        • If Elif Else
        • Functions
        • For loop
        • While loop >
          • While loop challenges
        • Data structures >
          • Looping thru 2D array
        • String slicing/indexing
        • Subprograms
        • Local/global variable
        • Validation and try
        • methods
        • File I/O
        • Pseudocode
        • Modules >
          • Turtle
        • Classes
        • NEA/Projects
  • KS3/Other
    • KS3 >
      • Online Safety >
        • Screen time
        • Online Safety questions
      • IT >
        • Word
        • PowerPoint
      • Hardware >
        • All Hardware
        • Magnetic hard disk drive
        • Optical disk drive
        • Solid state drive
      • Software
      • Binary/Hex/Algorithms >
        • Binary
        • Binary explained
        • Hexadecimal - 1 digit
        • Hexadecimal - 2 digits
        • Algorithms
        • ASCII
        • Tests
      • Networks
      • Protocols/Cyber attacks
      • Logic >
        • CPU
        • AND gate
        • OR gate
        • NOT gate
        • Logical statements
      • Coding >
        • HTML
        • Scratch >
          • Scratch questions
        • Python >
          • Getting started
          • Functions
          • Variables
          • VariablesToo
          • inputs
          • Selection
          • Flowcharts
          • Data types
          • Pseudocode1
          • Pseudocode2
          • Subprogram1
          • Subprogram2
          • Coding challenges
    • BTEC CS
    • OCR GCSE CS >
      • Past paper JUNE 2015
      • Past paper June 2014
      • Past paper MAY 2012
      • Past paper JAN 2011
      • Past paper JUN 2011
    • AQA AS-Level CS >
      • AS Curriculum >
        • Multiple choice questions
    • Business studies >
      • Keywords
      • BS Revision - Multiple choice
      • Paper 1 (9-1)
      • Paper 2 (9-1)
  • Contact/More
    • Code editor
    • Contact us
    • Microbit ideas
    • More
  • Self test
    • 01 Random topics
    • 02 Programming
    • 03 Data
    • 04 Computers
    • 05 Networks
    • 06 Legal/Ethics/Enviro'
    • Coding/Programming

Python coding

PYTHON DICTIONARY


Comments

Data types

Variables

If Else

If Elif Else

Functions

For loop

While loop

Data structures

String slicing/indexing

User defined function

Local/Global variable

Validation and Try

Methods

File I/O

Pseudocode

Modules​

What is a subprogram?

Python comes with 60+ built in functions but also lets the programmer make their own functions (also called sub programs). A user defined function/sub program is a piece of code that can be used over and over again.

Picture

Python challenge - User defined procedure

Click here to see the lyrics of the worlds most annoying song.
Picture
Now imagine if you had to write some code to display all the lyrics.  Programmers are a bit lazy and love to copy and paste as much as possible to work quicker. In this annoying song, the chorus is sung 6 times. If you were writing some code to display the lyrics, you would have to display the chorus 6 times! There is a quicker way and it is to use a "user defined function". The example below uses a user defined function to hold the chorus "def chorus():"and whenever we want the chorus to be displayed we "call" it using the code "chorus()".
Picture
Try to code this. Remember to "indent" all the content inside the "def chorus():" Do NOT type out the lyrics, copy and paste them from here. Just add the code as you see it above.

​Python challenge - User defined procedure - with input()

Enter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Picture
Here we can see a user defined function called "happyBirthday" and it is being passed an argument (keyword that means data) called "person". "Person" is the name of a variable that gets is value from a user via the "input()" function. 

​Python challenge - User defined procedure - with input()

Below is a poor joke. Can you create some code that allows a user to enter their name instead of "Dave"? Use the example above to help you. Goodluck!
​
"Do you want to hear a really good Batman impression?" asked My mate Dave.
"Go on then," I replied.
"NOT THE KRYPTONITE!" he screamed.
I said, "That's Superman."
He said, "Thanks, I've been practising."

User defined function and procedure

Enter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Picture
A function has a return statement/value. A procedure does not.
So, "def add_then_return()" is a function
"def main()" is a procedure

Picture
A function has a return statement/value. A procedure does not

User defined function - with return statement

Enter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Picture
Try the code with and without the return value (return result). What does "return result" do?

User defined function - return statement/value explained

Enter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Picture

​Return statement/value used

Function = a subprogram with a return statement/value
Procedure = a subprogram without a return statement/value
Notice how "main()" gets the other subprograms to do something. For example, line 10  calls the subprogram called getFirstName()  on line 1 which asks for a name and returns the name back to line 10 into variable called "FirstName".
​


User defined procedure - The bubble sort

Enter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Picture

Parameters vs arguements

Parameters explained
When you make a user defined function or a user defined procedure, you put brackets at the end and then a colon. If the brackets are empty then your code does not have any parameters, e.g.:

def DisplayYes():
    print("yes")

In the code below, the brackets have two parameters, itemWidth and itemheight. 
A parameter is a variable name inside the brackets of a function "header" that receive a value.

Arguments explained
Argument is the name given to the data that is passed to the function in the call command. In the code below, the call command is on line 5 (yes, the call command exists in a variable called itemArea, but dont let this confuse you ). The call calArea() has two arguments in it, 2 and 5. These two arguments are passed to the parameters in calcArea function that is on line 1.

Arguments are passed to parameters

Subprogram for menu

Lots of software use a menu system to allow the user to choose what they want to do. The code below contains 4 subprograms/user defined functions. The main one displays the options available then branches the program to another subprogram to execute the chosen option.

Look out for the while loop which keeps re-displaying the options. Also look out for the how the loop is controlled with the sentry variable on line 11 which is changed on line 22 to exit the loop.

Home
Contact
Upload
Log in
  • Exam boards
    • OCR past papers >
      • Paper1 (Sample)
    • Edexcel past papers >
      • 2020 Current spec (coming soon) >
        • Programming Language Subset (PLS)
      • Old 2016 SPEC >
        • Paper 1 (specimen1)
        • Paper 1 (specimen2)
        • Paper 1 (2018)
        • Paper 1 (2019)
        • Paper 2 (specimen1)
        • Paper 2 (2018)
        • Paper 2 (2019)
      • OLD 2013 SPEC >
        • Sample assessment 1 (old spec)
        • Sample assessment 2 (old spec)
        • June 2017 (old spec)
        • June 2016 (old spec)
        • June 2015 (old spec)
    • Paper 1 (by topic) >
      • Algorithms
      • Application software
      • Ascii
      • Binary
      • Cache
      • Client Server
      • Cloud
      • Compression
      • Computational thinking
      • CPU
      • Cyber security
      • Database
      • Fetch decode execute cycle
      • Embedded system
      • Encryption
      • File size
      • Flow chart
      • Hexadecimal
      • High Level language
      • ISP/Internet
      • Logic
      • Magnetic HDD
      • Networking
      • Open source
      • Operating system
      • Peer to peer
      • Images (Data representation)
      • Protocols
      • RAM
      • ROM
      • Run length encoding
      • Simulation software
      • Solid state HDD
      • Sound
      • Stored program concept
      • Utility software
    • Curriculum >
      • Binary
      • Hex
      • Data Representation
      • Compression
      • Encryption
      • What is a computer
      • How does a computer work
      • Hardware
      • Operating system
      • The CPU
      • Input devices
      • Data storage
      • Networks
      • Microcontrollers
      • Computational models
      • Emerging trends
      • Virtual machine
      • Models and simulators
      • Algorithms
    • Python >
      • Finding python
      • GCSE Python questions
      • Python coding >
        • Comments
        • Data types
        • Variables
        • If Else
        • If Elif Else
        • Functions
        • For loop
        • While loop >
          • While loop challenges
        • Data structures >
          • Looping thru 2D array
        • String slicing/indexing
        • Subprograms
        • Local/global variable
        • Validation and try
        • methods
        • File I/O
        • Pseudocode
        • Modules >
          • Turtle
        • Classes
        • NEA/Projects
  • KS3/Other
    • KS3 >
      • Online Safety >
        • Screen time
        • Online Safety questions
      • IT >
        • Word
        • PowerPoint
      • Hardware >
        • All Hardware
        • Magnetic hard disk drive
        • Optical disk drive
        • Solid state drive
      • Software
      • Binary/Hex/Algorithms >
        • Binary
        • Binary explained
        • Hexadecimal - 1 digit
        • Hexadecimal - 2 digits
        • Algorithms
        • ASCII
        • Tests
      • Networks
      • Protocols/Cyber attacks
      • Logic >
        • CPU
        • AND gate
        • OR gate
        • NOT gate
        • Logical statements
      • Coding >
        • HTML
        • Scratch >
          • Scratch questions
        • Python >
          • Getting started
          • Functions
          • Variables
          • VariablesToo
          • inputs
          • Selection
          • Flowcharts
          • Data types
          • Pseudocode1
          • Pseudocode2
          • Subprogram1
          • Subprogram2
          • Coding challenges
    • BTEC CS
    • OCR GCSE CS >
      • Past paper JUNE 2015
      • Past paper June 2014
      • Past paper MAY 2012
      • Past paper JAN 2011
      • Past paper JUN 2011
    • AQA AS-Level CS >
      • AS Curriculum >
        • Multiple choice questions
    • Business studies >
      • Keywords
      • BS Revision - Multiple choice
      • Paper 1 (9-1)
      • Paper 2 (9-1)
  • Contact/More
    • Code editor
    • Contact us
    • Microbit ideas
    • More
  • Self test
    • 01 Random topics
    • 02 Programming
    • 03 Data
    • 04 Computers
    • 05 Networks
    • 06 Legal/Ethics/Enviro'
    • Coding/Programming