An Introduction to Programming

Python for All

Chapter Three — Functions & Methods

Thanasis Troboukis  ·  All Chapters

Chapter Three

Functions & Methods

Python comes with a powerful toolkit of ready-made functions and methods. Learn what they are and how to use them.

Functions vs. Methods

You have already used print() and len(). These are functions — standalone commands that you call by writing their name followed by parentheses, with an input inside: len("hello").

A method is different: it belongs to a specific piece of data and is called using dot notation — the data first, then a dot, then the method name: "hello".upper(). Think of a method as something the data knows how to do to itself.

Python · Try it

      
Rule of thumb: If it starts with the data and a dot — something.method() — it's a method. If the data goes inside the parentheses — function(something) — it's a function.

Built-in Functions

Python ships with dozens of functions that are always available. Here are the ones you will reach for most often.

len() — the length of things

len() returns the number of items in a list, or the number of characters in a string.

Python · Try it

      

type() — what kind of thing is this?

type() tells you what data type a variable holds. Useful when something isn't behaving as expected.

Python · Try it

      

int(), float(), str() — converting between types

These functions convert a value from one type to another. This is essential when, for example, you read a number that is stored as text and need to do arithmetic with it.

Python · Try it

      

round() — rounding numbers

round(number, digits) rounds a number to the given number of decimal places. If you omit the second argument it rounds to the nearest whole number.

Python · Try it

      

max() and min() — largest and smallest

These work on a list of numbers (or strings) and return the largest or smallest value.

Python · Try it

      

sum() — total of a list

Python · Try it

      

String Methods

Strings in Python come loaded with methods. Because strings are so common in real programs — names, addresses, messages, file contents — these methods save enormous amounts of work.

.upper() and .lower() — changing case

Python · Try it

      

.title() — title case text

.title() makes the first letter of each word uppercase and the rest lowercase.

Python · Try it

      

.strip() — removing extra whitespace

Real-world data often has unwanted spaces at the start or end. .strip() removes them.

Python · Try it

      

.replace() — substituting text

.replace(old, new) returns a new string with every occurrence of old swapped for new.

Python · Try it

      

.split() — breaking a string into a list

.split(separator) cuts the string wherever it finds the separator and returns a list of the pieces. If you omit the separator, it splits on any whitespace.

Python · Try it

      

.startswith() and .endswith() — checking beginnings and endings

These return True or False, making them ideal for use inside if statements.

Python · Try it

      

.count() — counting occurrences

Python · Try it

      

Slicing strings — taking parts of text

You can extract part of a string with slicing: text[start:end]. It starts at start and stops before end.

Python · Try it

      

.camel() — making camelCase (custom helper)

Python does not include a built-in .camel() string method, but you can make the same result with a small helper function.

Python · Try it

      
Methods do not change the original. Strings in Python are immutable — methods always return a new string and leave the original untouched. If you want to keep the result, assign it to a variable: clean = name.strip().

List Methods

Lists also have their own set of methods. Unlike string methods, several list methods modify the list in place — they change the original directly, rather than returning a new one.

.append() — adding an item

You have seen this before. It adds one item to the end of the list.

Python · Try it

      

.sort() — sorting in place

.sort() rearranges the list alphabetically (for strings) or numerically (for numbers). It modifies the list directly.

Python · Try it

      

.count() — counting occurrences in a list

Python · Try it

      

Removing items — .remove(), .pop(), and del

There are different ways to remove from a list. .remove(value) removes by value, .pop(index) removes by position and returns the removed item, and del deletes by index.

Python · Try it

      

Putting it together

Python · Try it

      
In place vs. returning a new value: .sort() and .append() modify the list directly and return nothing. .upper(), .strip(), .replace() leave the original unchanged and return a new value. When in doubt, print the result to see what you got.

Chapter Navigation

Move between chapters.

Loading Python environment — this may take a moment…