Thanks to visit codestin.com
Credit goes to www.pythonmorsels.com

Functions and Methods in Python

Share
Copied to clipboard.
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
4 min. read 3 min. video Python 3.10—3.14
Tags

Let's talk about functions and methods in Python.

Operators

Python includes operators, like the plus operator (+) and the minus operator (-):

>>> n = 9
>>> language = "Python"
>>> language + " is fun"
'Python is fun'
>>> n - 4
5

Some functions included with Python

But Python also includes many functions.

type

For example, there's the type function, which will return the type of a given object:

>>> type(2)
<class 'int'>
>>> type(2.5)
<class 'float'>
>>> type("Python")
<class 'str'>

The type of an integer is int, the type of a floating point number is float, and the type of a string is str.

print

Python also has a print function, which will display output in our terminal window:

>>> print("Let's print this text.")
Let's print this text.

The interactive Python interpreter, also known as the Python REPL, that we're in here, will print the result of each statement by default.

So print isn't very handy in the Python REPL, but it is useful outside of the REPL. Once we start making Python programs, we'll be using print quite a bit.

len

Some functions only work on certain types of objects.

For example, Python includes a len function that only works on objects that have a length. Strings have a length:

>>> len("Python")
6

But numbers don't have a length:

>>> len(8)
Traceback (most recent call last):
  File "<python-input-155>", line 1, in <module>
    len(8)
    ~~~^^^
TypeError: object of type 'int' has no len()

Conversions with int and str

Python also includes functions for converting from one type of object to another.

For example, there's an int function for converting an object to an integer. The int function can convert strings to integers, and it can also convert floating point numbers to integers:

>>> string = "2"
>>> n = 3.7
>>> int(string)
2
>>> int(n)
3

Python also has a str function for converting objects to strings. Every object in Python can be converted to a string using the built-in str function:

>>> str(n)
'3.7'

Calling functions

We use functions by calling them.

We can call a function by typing the name of the function and putting parentheses after it:

>>> print()

Function arguments

Many functions accept arguments. Arguments go inside the parentheses when we call a function.

The print function accepts any number of arguments, but the len function only accepts one argument.

>>> print(1, 2, 3)
1 2 3
>>> len("Python")
6

If we wanted to pass multiple arguments to a function, we put commas between each argument.

We'll talk more about function arguments after we define our own functions later.

Functions either return a value or perform an action

When a function is called, it will usually return a value back to us, which we could then assign to a variable if we wanted to:

>>> text = "Python"
>>> length = len(text)
>>> length
6

This is value is often called the function's return value.

Some functions don't return anything at all, but instead perform an action.

For example, the print function prints something to the screen:

>>> print(text, "has", length, "characters")
Python has 6 characters

Though technically, every function does return something, but the default function return value is a special value called None:

>>> value = print("Trey")
Trey
>>> print(value)
None

None is a value that basically means this function doesn't have a return value.

Methods are a special type of functions

Some functions live on a specific type of object. These are called methods.

For example, strings have a replace method that creates a new string with matching parts of a string replaced.

Here we've used the string replace method to made a new string that has all spaces from the original string replaced by two dashes:

>>> message = "We're using Python"
>>> message.replace(" ", "--")
"We're--using--Python"

Just like other functions, methods are called by putting parentheses after them. Though unlike other functions, methods are attached to the object that they operate on.

To use the len function, we pass a string to it:

>>> message = "We're using Python"
>>> len(message)
18

But to use the replace method, we put a period (.) after a reference to our string, and then we type the method name to call it:

>>> message.replace(" ", "--")
"We're--using--Python"

Functions are everywhere in Python

Python includes about 70 built-in functions, and most objects in Python have one or more methods.

We'll see more functions and methods as we dive deeper into Python later, and eventually we'll create our own functions.

5 Keys to Python Success 🔑

Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!