Note that this site is in currently in version 1.0.0-alpha.   Some functionality may be limited.

12. A Little Motivation

Early on, we learned a bit about lists, which look like this:

["Gender Trouble", "Cruising Utopia", "Living a Feminist Life"]

We’re going to create a small application that will print a random motivational saying every time a user presses enter. Our first step will be to create a list of positive sayings:

motivational_phrases = [
        "Importing modules is easy!",
        "Programming! Yay!",
        "You write lists like a pro!",
    ]

You’re still using the same list format. Remember lists open with a square bracket [, have items seperated with commas, and end with a square bracket ], like this:

[1, 2, 3, 4, 5]

However, our positivity list above spreads the list out over multiple lines for greater readability, which is allowed in Python. Remember that you can change the strings in the list to whatever phrases you choose.

Importing a module

Now that we have our sayings, let’s use it in conjunction with some functionality from a module that’s built into Python: the random module.

import random

motivational_phrases = [
        "Importing modules is easy!",
        "Programming! Yay!",
        "You write lists like a pro!",
    ]

print(random.choice(motivational_phrases))

The random.choice function chooses a random item from a list and returns it. The . syntax indicates that the function is coming from the random library.

Challenges for lesson 12

Assignment: Challenge

  1. As with our library app, this positive saying generator could be improved by making it so the program doesn’t have to run again every time to get new output. Add a while loop for the final version. Remember to include a break statement or use control + c to get out of the loop! Read more about while loops here.

  2. The real point of this section is to learn import, which is where Python really starts to get interesting. Python comes with many libraries (importable collections of code), written by others that can be pulled into your program, allowing you to use that functionality. In this challenge, do a little research on Python libraries that might solve a problem for you or address a domain that you’re interested in.

Think of something you’re interested in doing (statistics, text analysis, web scraping, quantitative analysis, processing Excel/PDF/image files) and search google “<thing you are interested in> python library”. You’re almost certain to find some useful results. For example, if you wanted to find Python libraries for dealing with cleaning up HTML files, you might search one of these:

working with html python library

html parser python library

In your research, you may also want to look at the libraries that come with Python. You can find a list of libraries in these libraries here.

  1. Here’s how you could add a while loop to our positive saying generator:

    import random
    
    while True:
        motivational_phrases = [
            "Importing modules is easy!",
            "Programming! Yay!",
            "You write lists like a pro!",
            ]
    
        # Because this is input, the user will need to hit enter to see a new phrase
        input(random.choice(motivational_phrases))

Questions

Try again!

What is a module? Select all that apply:

Terms Used in Lesson

Can you define the terms below? Hover over each of them to read a preview of the definitions.

Modules

A module is a python file (like a script) that contains a number of functions and expressions that you want to include in your program. Programmers often import modules written by other programmers and use …

See term page

random

The random module or random library allows one to generate random output. The module is useful for to choose a random element from a list, or number from a range, or between two potential values …

See term page

while loops

Like for loops, while loops are a way of going through items in a list. The difference is that while loops will continue to execute as long as a certain condition is true. To get …

See term page

Workshop overall progress