Python Return Integer From Function

5/5 - (1 vote)

Do you need to create a function that returns an integer value but you don’t know how? No worries, in sixty seconds, you’ll know! Go! πŸ”₯πŸ”₯πŸ”₯

A Python function can return any object. To return an integer, use the built-in int() function. Or create your own function with an arbitrary expression within the function body to compute the integer. Put the result behind the return keyword (e.g., return 42).

πŸ‘‰ Recommended Tutorial: The return keyword in Python

Method 1: Using the int() Function

Python’s int() function takes an argument such as a string or a float and converts it to an integer. You don’t need to import a library to use the function; it’s built-in. For example, int('42') converts the string to an integer 42 and int(42.42) converts the float to an integer 42.

Here’s a code snippet exemplifying this approach:

# String to Int
x = '42'
# 42

# Float to Int
x = 42.42
# 42

⭐⭐⭐ This is the most straightforward approach to returning an integer from a function.

You can also watch my explainer video and visit the recommended blog tutorial on the topic:

Python int() Function

🌍 Recommended Tutorial: Python int() Function

Method 2: Create Your Own Function and Return Int

You can create your custom function returning an integer by using the keyword def, followed by a function name, followed by an arbitrarily complicated function body to determine the resulting integer. Say, you’ve stored the resulting integer in the local variable x. To return it from the function, use the expression return x.

Let’s have a look at a minimal example that creates a function my_int() that returns an integer value -42 and does nothing else:

def my_int():
    return -42

# -42

⭐⭐⭐ This is the most flexible approach to returning an integer from a function because you can do anything in the function body, it’s Turing complete!

Method 3: Use Integer Return Expression

In your function body, you can also use arbitrary mathematical or programmatical expressions to determine the integer. For example, the expression return 10//3 computes the integer as the result of the integer division operator and returns it from the function.

Here’s an easy example:

def my_int():
    return 10//3

# 3

Feel free to check out my “Division Deep Dive” video:

Python Division Deep Dive

Method 4: Use Complicated Function Body to Compute Int

For comprehensibility, you can use an arbitrarily complex function body to calculate and return an integer value.

Here’s a dummy example to show you how it takes an integer input and calculates a resulting integer value:

def my_int(x):
    if x>3:
        return x-3
    elif x==3:
        return x*100
        return x+42

for i in range(10):
    print(i, my_int(i))

The output is the following:

0 42
1 43
2 44
3 300
4 1
5 2
6 3
7 4
8 5
9 6

Note that you could also use the same function to compute and return a float value by passing a float as a function argument.

Related Tutorials

Programmer Humor

Q: How do you tell an introverted computer scientist from an extroverted computer scientist?

A: An extroverted computer scientist looks at your shoes when he talks to you.