FREE INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
LESSON 13
PAGE 2 / 3
Random numbers
Home >>> Free Online Lessons, Python 3

The random module contains a consistent and advanced set of functions to generate random numbers.

The randint Function

One of these functions saves us from the previous calculations to obtain integers.

The randint(start, end) function returns a random integer (hence of type int) from the interval specified by the parameters.

The choice Function

We can choose one of the objects stored in a list or a string using the choice(data_collection) function. The function randomly returns an element from the object:
Editor - lesson_13.py
       
Console/Output done

The shuffle Function

Suppose we have a list and we want to shuffle it, like a new deck of playing cards, so that the default order of elements is randomly changed.

We can use the shuffle(data_collection) function, which takes as a parameter an object of type list or string. This function does not return anything but has a direct effect on the object passed as a parameter:



It does not work for the online interpreter, so test it on your computer in Python IDLE!
Proceed to the next page.
 home   list  CONTENTS   perm_identity   arrow_upward