Python 3 – Number shuffle() Method
If you are a Python 3 developer or enthusiast, you might be interested in knowing about the shuffle() method for the “numbers” data type. In this article, we will take a look at the number shuffle() method and its practical usage.
What is shuffle() Method in Python 3?
The shuffle() method is a built-in function in Python 3, which can be used to rearrange numbers in a given list. It works by randomly rearranging the numbers in the list, and thus is used to shuffle values. It can be used on any iterable object like list, tuple, etc.
Here is the syntax of the shuffle() function:
random.shuffle(list)
Parameters of shuffle() Function
The shuffle() method works on a single parameter, which is the list to be shuffled. The method does not return anything, instead, it shuffles the list in-place.
Example usage of shuffle() Function
Let us now take a look at an example where we will use the shuffle() method to rearrange the numbers in a list.
import random
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random.shuffle(numbers)
print("Shuffled list :", numbers)
Output:
Shuffled list : [6, 8, 7, 10, 9, 1, 5, 4, 2, 3]
In the above example, we have initialized a list named “numbers” with the values 1 to 10. We then imported the random module and used the shuffle() function to randomly shuffle the numbers list. Finally, we printed the shuffled list using the print function.
Conclusion
The shuffle() function is a built-in function in Python 3 that can be used to shuffle numbers in a given list. It shuffles the list in place by randomly rearranging the values. It is used to randomly shuffle elements of a list or tuple. So, if you are a Python programmer, you must know how to use the shuffle() method on iterable objects. I hope this article has been helpful to you in understanding the shuffle() function in Python 3.