Appearance
C3 · Built-in Functions
Spec reference: Section C - Programming Paradigms
Key idea: Use arithmetic, string and general built-in functions to process data.
Variables and Data Types in Python
What are built-in functions?
Built-in functions are pre-written functions provided by a programming language that perform common operations. You call them by name without needing to write the underlying code yourself.
They fall into three categories: arithmetic, string handling, and general.
Arithmetic functions
| Function | What it does | Example | Result |
|---|---|---|---|
ROUND(x, n) | Round x to n decimal places | ROUND(3.7654, 2) | 3.77 |
INT(x) / int(x) | Truncate to integer (remove decimal) | INT(7.9) | 7 |
ABS(x) | Absolute value (remove negative sign) | ABS(-5) | 5 |
SQRT(x) | Square root | SQRT(16) | 4.0 |
MAX(a, b, ...) | Largest of the values given | MAX(3, 9, 1) | 9 |
MIN(a, b, ...) | Smallest of the values given | MIN(3, 9, 1) | 1 |
python
import math
print(round(3.7654, 2)) # 3.77
print(int(7.9)) # 7
print(abs(-15)) # 15
print(math.sqrt(25)) # 5.0
print(max(4, 9, 2)) # 9String handling functions
Strings are sequences of characters. String functions let you inspect, extract, and modify text.
| Function | What it does | Example | Result |
|---|---|---|---|
LEN(s) | Number of characters | LEN("Hello") | 5 |
UPPER(s) | Convert to uppercase | UPPER("hello") | "HELLO" |
LOWER(s) | Convert to lowercase | LOWER("HELLO") | "hello" |
SUBSTRING(s, start, len) | Extract part of a string | SUBSTRING("Hello", 1, 3) | "Hel" |
LEFT(s, n) | First n characters | LEFT("Hello", 3) | "Hel" |
RIGHT(s, n) | Last n characters | RIGHT("Hello", 3) | "llo" |
CONTAINS(s, t) | Does s contain t? | CONTAINS("Hello", "ell") | True |
python
name = "Hello, World!"
print(len(name)) # 13
print(name.upper()) # HELLO, WORLD!
print(name.lower()) # hello, world!
print(name[0:5]) # Hello (substring)
print("World" in name) # True (contains)
print(name.replace("World", "Python")) # Hello, Python!String indexing
Strings are zero-indexed in most languages:
H e l l o
0 1 2 3 4So "Hello"[0] = 'H' and "Hello"[4] = 'o'.
General functions
| Function | What it does | Example | Result |
|---|---|---|---|
STR(x) | Convert to string | STR(42) | "42" |
INT(x) | Convert to integer | INT("42") | 42 |
FLOAT(x) | Convert to float | FLOAT("3.14") | 3.14 |
INPUT(prompt) | Get text input from user | INPUT("Enter name: ") | user's text |
RANDOM(a, b) | Random integer between a and b | RANDOM(1, 6) | 1 to 6 |
python
import random
user_age = int(input("Enter your age: ")) # input() + INT conversion
dice = random.randint(1, 6)
print("You rolled:", dice)Worked example: Name formatter
python
def format_name(first, last):
# Capitalise first letter of each, combine
first = first.strip().capitalize()
last = last.strip().capitalize()
full = first + " " + last
initials = first[0] + last[0]
return full, initials
name, init = format_name("alice", "smith")
print(name) # Alice Smith
print(init) # ASTest Yourself
Question 1 of 5
What does LEN("Computing") return?