Slides badge

Predefined functions

Learning Intentions

  • Understand the term 'predefined function'

  • Identify and use appropriate predefined functions

Success Criteria

  • Be able to explain the term predefined function
  • Be able to identify and describe the predefined functions specified at Higher level

Predefined functions

  • A pre-defined function carries out an operation or calculation using the data we give (pass) it and returns a single value.  The functions listed below are relevant to the course content for National 5:
    • Round
    • Length
    • Random
  • The SQA sometimes refers to built-in object functions as predefined functions, when in actual fact they are not. If you want to find out more about this, look up object methods in Python.

An example: the YASS language

National 5

  • At National 5 you looked at the following predefined function:

    • Random

    • Round

    • Length

  • Let's take a quick look at them again


  • The random predefined function generates a random number between a range in Python:

import random

#Generates a number between 10 and 100
print(random.randint(10, 100))


  • The round function rounds a number to a specified number of decimal places.

x = 3.145

#Will give us 3.15
print(round(x, 2))

#Will give us 3.0
print(round(x, 0))


  • The length function is used to calculate the length of an array.

fib = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
#Should print 10
print (len(fib))

In your jotter, write down an example of how each of the predefined function shown before could be used in Python


Higher Predefined functions

  • For Higher level, you need to know three more predefined functions:

    • Substrings

    • Convert ASCII characters to integers and vice versa

    • Casting function to convert a floating point to integer

    • Modulus


name = "John Smith"

  • A substring is a part of string. For example, the word “Hello world” has substrings such as:
    • Hello
    • World
    • Hell
    • llo
  • To obtain a substring in Python, you need to use the subscript operator syntax (substring syntax):


name = "John Smith"


Python's index selection here, just like with a normal array, is from the start character (non-inclusive) index to the last character index (inclusive).

For each of the following, write what you think the output from the substring predefined function would be when:

name = "Hello Class"





Warm up

Character to ASCII

char1 = "A"
  • As you may remember from National 5, ASCII is the encoding system used by computers to represent characters (both special characters and characters from the keyboard)
  • In ASCII, the letter A is represented by the integer 65.
  • The Python ord function can convert a character to it's ASCII integer representation:

aSCII to Character

ascii_no = 65
  • To convert an integer representation of an ASCII value back into a character, we use the chr function:

Using Python, convert the following characters to their ASCII integer representation:


  1. P
  2. !
  3. $

Warm up

Using Python, convert the integers to characters



  1. 93
  2. 45
  3. 71

Converting an floating point to an integer value

  • Another predefined function is the conversion of a floating point number to an integer.
  • This is done with the floor function
  • Floor simply rounds down at all times.
import math
x = 5.31

Modular arithmetic

  • Modular arithmetic is one of the most useful kinds of mathematics in existence today, as such most programming languages exploit a method of returning a remainder value of a sum.
  • Performing a mod operation results in the remainder being returned.
  • In Python, the modulus operator (modulo) is represented by a % sign.
  • For example:
    • 5 % 2 = 1
    • 10 % 7 = 3
    • 21 % 7 = 0

Modular arithmetic

  • What's really handy about modulus is how easy it is to figure out if a number is even or not by simply performing a % 2 operation on the number:
    • 0 % 2 = 0
    • 1 % 2 = 1
    • 2 % 2 = 0
    • 3 % 2 = 1
    • 4 % 2 = 0
    • 5 % 2 = 1
  • Any time that the return value is 1 the number is negative.

Using modular arithmetic, write a program in Python to display all numbers which are factors of 20 up to 200.


Presentation Overview
Predefined Functions
© 2020 - 2024 J Balfour
09:10 | 21-05-2024
Join Live Session
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
Widget Screen
Canvas Controls
Random Selector
Volume Meter
Binary Converter
Python Editor
Show Knox 90
Provide Feedback
    DragonDocs Management
    Random selector
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍

      Apply a filter:

      All slideshow files