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.,
👉 Recommended Tutorial: The
return keyword in Python
Method 1: Using the int() Function
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' print(int(42)) # 42 # Float to Int x = 42.42 print(int(x)) # 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:
🌍 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
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 print(my_int()) # -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 print(my_int()) # 3
Feel free to check out my “Division Deep Dive” video:
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 else: 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.
- Python Return String From Function
- Python Return Dict From Function
- Python Return Set From Function
- Python Return List From Function
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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.