In the previous chapter we learned about what a Boolean value is. In this chapter we will see how to make the program more dynamic by skipping blocks of statements depending on Boolean values, and random conditions. Creating optional code in a program is known as selection. Also, this chapter will show how to handle run time errors and introduce random numbers.

Objectives

Upon completion of this chapter’s exercises, you should be able to:

  • Use the in operator to see if a list or tuple contains a value.
  • Include the ‘Random’ module in your Python programs.
  • Generate random integers, random floating-point numbers, and shuffle lists using the Random module.
  • Create complex if statements that use Boolean expressions to conditionally perform suites of code.
  • Develop exception handling suites to capture run-time errors and perform special suites if code when they are thrown.
  • Construct loops using the while structure.
  • Include the ‘sys’ module in your programs and use the exit method to terminate a program early.