In this chapter you’ll learn about the different kinds of data you can work with in your Python programs. You’ll also learn how to store your data in variables and how to use those variables in your programs.
Write a separate program to accomplish each of these exercises. These Try It Yourself exercises can be shortened as TIY
. Save
each program with a filename that follows standard Python conventions,
using lowercase letters and underscores, such as tiy1.py
and
tiy2.py
.
2-1. Simple Message: Store a message in a variable, and then print that message.
2-2. Simple Messages: Store a message in a variable, and print that message. Then change the value of your variable to a new message, and print the new message.
Save each of the following exercises as a separate file with a name like name_cases.py. If you get stuck, take a break or see the suggestions in Appendix C.
2-3. Personal Message: Store a person’s name in a variable, and print a message to that person. Your message should be simple, such as, “Hello Eric, would you like to learn some Python today?”
2-4. Name Cases: Store a person’s name in a variable, and then print that person’s name in lowercase, uppercase, and titlecase.
2-5. Famous Quote: Find a quote from a famous person you admire. Print the quote and the name of its author. Your output should look something like the following, including the quotation marks:
Albert Einstein once said, “A person who never made a mistake never tried anything new.”
2-6. Famous Quote 2: Repeat Exercise
2-5, but this time store the famous person’s name in
a variable called famous_person
. Then compose your message and store
it in a new variable called message
. Print your message.
2-7. Stripping Names: Store a person’s
name, and include some whitespace characters at the beginning and end of
the name. Make sure you use each character combination, "\t"
and
"\n"
, at least once.
Print the name once, so the whitespace around the name is displayed.
Then print the name using each of the three stripping functions,
lstrip()
, rstrip()
, and strip()
.
2-8. Number Eight: Write addition,
subtraction, multiplication, and division operations that each result in
the number 8. Be sure to enclose your operations in print
statements
to see the results. You should create four lines that look like this:
print(5 + 3)
Your output should simply be four lines with the number 8 appearing once on each line.
2-9. Favorite Number: Store your favorite number in a variable. Then, using that variable, create a message that reveals your favorite number. Print that message.
2-10. Adding Comments: Choose two of the programs you’ve written, and add at least one comment to each. If you don’t have anything specific to write because your programs are too simple at this point, just add your name and the current date at the top of each program file. Then write one sentence describing what the program does.
2-11. Zen of Python: Enter import this
into a Python terminal session and skim through the additional
principles.