From 538108b0403b452e14f034ff93070d46a8f6f74e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 20 Aug 2019 16:02:37 +0300 Subject: [PATCH] Add sample snippet --- snippets/sample.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/sample.md diff --git a/snippets/sample.md b/snippets/sample.md new file mode 100644 index 000000000..4f86c5ec3 --- /dev/null +++ b/snippets/sample.md @@ -0,0 +1,19 @@ +--- +title: sample +tags: list,random,beginner +--- + +Returns a random element from an array. + +Use `randint()` to generate a random number that corresponds to an index in the list, return the element at that index. + +```py +from random import randint + +def sample(lst): + return lst[randint(0, len(lst) - 1)] +``` + +```py +sample([3, 7, 9, 11]) # 9 +```