O'Reilly logo

A Concise Introduction to Programming in Python by Mark J. Johnson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 2

Python Program Components

Let’s look at another Python program that creates short nonsense sentences:

Listing 2.1: Mad Lib

 1 # madlib.py
 2 # Your Name
 3
 4 adjective = input("Enter an adjective: ")
 5 noun = input("Enter a noun: ")
 6 verb = input("Enter a verb: ")
 7 adverb = input("Enter an adverb: ")
 8 print("A", adjective, noun, "should never", verb, adverb)

Type this program into your Python environment, save it as madlib.py, and run it a few times to play with it. Enter any responses you like when the interpreter asks for them. Then consider the following aspects of both this program and Listing 1.1.

Variables

Every program accomplishes its work by manipulating data. Variables are names that refer to data in memory. Variable ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required