In this Python article we want to learn about Python function return, so as you know Python is one of the most popular programming languages, and that is because it is simple and easy to use.one of the important concepts in Python is function, for example we should know that functions work ? and how they accept arguments and return values. in the article we will write some practical Python function examples with Python return.
Python function returns
When it comes to Python return, one important note that you should remember is that in Python return value can be anything. while most functions return some specific value or object, like string or list, Python functions can return almost any type of data. for example a function can return a complex data structure like dictionary or set, a custom object that you have defined, or even an instance of builtin Python class like int or float.
Now let’s talk about an example, what if a Python return could be truly unpredictable ? what if it could generate something unique and random every time it’s called.
This is an example of function that demonstrates this idea.
1 2 3 4 5 6 7 8 9 10 11 |
import random def random_return(): rand_int = random.randint(1, 100) rand_float = random.uniform(1.0, 100.0) rand_bool = random.choice([True, False]) rand_str = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=10)) return rand_int, rand_float, rand_bool, rand_str print(random_return()) |
This function uses random module to generate random integer, random floating point number, random boolean value and random string. after that it returns all of these values as a tuple.
This will be the result
![Python function return](https://geekscoders.com/wp-content/uploads/2023/04/python-return-value.jpg)