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.
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
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