From 8debdfd2bc337ee6b393404fa4a4b292fc22f69f Mon Sep 17 00:00:00 2001 From: Terho <35961163+Naapi@users.noreply.github.com> Date: Mon, 11 Feb 2019 05:23:42 +0200 Subject: [PATCH] Added basic random examples (#32568) * Added basic random examples Python random library examples * Rename guide/english/python/Random/index.md to guide/english/python/random/index.md --- guide/english/python/random/index.md | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 guide/english/python/random/index.md diff --git a/guide/english/python/random/index.md b/guide/english/python/random/index.md new file mode 100644 index 0000000000..90db0aa283 --- /dev/null +++ b/guide/english/python/random/index.md @@ -0,0 +1,32 @@ +--- +title: Random +--- +## Random + +Using ``random`` library allows to randomize integers and lists. + + +#### Randomize integers between range of 'a' and 'b': + +Simply replace a and b variables. This will print a random number between 1 and 10. +```python +import random +a = 1 +b = 10 +print(random.randint(a,b)) + ``` +or just use the numbers instead of variables: +```python +import random +print(random.randint(1,10)) + ``` + +#### Randomize items in a list + +Using ``choice()`` fuction to random select a list item. These can be a mix of different types, such as integers, booleans and strings. This will print random item from the ``list``. +```python +import random +list = ["hello", 2, False, "world", 1337] +print(random.choice(list)) +``` +More about random library for Python 3 at python docs.