Python - Core Language

if Statements

Like many programming languages, Python supports conditionals using if statements. The simplest if statement in Python involves the keyword, a conditional, and the code to run if the conditional result is True:

x = 24
if x > 20:
  print('X is greater than 20.')

Additional conditions can be tested with elif, short for else if:

x = 16
if x > 20:
  print('X is greater than 20.')
elif x > 10:
  print('X is between 10 and 20.')

Finally, the else statement is a "catchall" that is called if the initial conditional(s) fail.

x = 5
if x > 20:
  print('X is greater than 20.')
elif x > 10:
  print('X is between 10 and 20.')
else:
  print('X is less than 10.')

Python - random

Standard library Python module for pseudorandom number generation.

Functions

random.randint(a, b):

Return a random integer N between a and b inclusive.

random.choice(seq):

Return a random element from the non-empty sequence provided as an argument. If the sequence is empty, raises an IndexError. Either check the length of the sequence before passing it to this function, or be prepared to handle the error. "Sequence" means this function supports lists, strings, and tuples.