This chapter introduced the basics of programming in Python, including:
print()
andinput()
.- Variables and assignment.
- Strings, integers, and floats.
- Arithmetic, concatenation.
- Common error messages.
- Comments and docstrings.
At this point, you should be able to write programs that ask for input, perform simple calculations, and output the results. The programming practice below ties together most topics presented in the chapter.
Function | Description |
---|---|
print(values) |
Outputs one or more values, each separated by a space, to the user. |
input(prompt) |
If present, |
len(string) |
Returns the length (the number of characters) of a string. |
type(value) |
Returns the type (or class) of a value. Ex: |
Operator | Description |
|
Assigns (or updates) the value of a variable. In Python, variables begin to exist when assigned for the first time. |
|
Appends the contents of two strings, resulting in a new string. |
|
Adds the values of two numbers. |
|
Subtracts the value of one number from another. |
|
Multiplies the values of two numbers. |
|
Divides the value of one number by another. |
|
Raises a number to a power. Ex: |
Syntax | Description |
|
All text is ignored from the # symbol to the end of the line. |
|
Strings may be written using either kind of quote. Ex: |
|
Used for documentation, often in multi-line strings, to summarize a program's purpose or usage. |
Try It
Fun facts
Write a program that assigns a variable named number
to any integer of your choice. Ex: number = 74
. Then, use this variable to calculate and output the following results:
74 squared is 5476 74 cubed is 405224 One tenth of 74 is 7.4 74 plus 123 is 197 74 minus 456 is -382
Run the program multiple times, using a different integer each time. Your output should be mathematically correct for any integer that you choose.
The point of this exercise is to perform basic arithmetic within a print statement. Do not use any other variables besides number
. Your program should have only one assignment statement (at the beginning).
Try It
Mad lib
A mad lib is a word game in which one person asks others for words to substitute into a pre-written story. The story is then read aloud with the goal of making people laugh.
This exercise is based the Vacations Mad Lib available on the Printables section of MadLibs.com. Write a program that asks the user to input two adjectives and two nouns (user input in bold):
Adjective: tranquil Adjective: scandalous Noun: pancake Noun: field
Use input()
to display each prompt exactly as shown. The user's input should be on the same line as the prompt. Each colon must be followed by exactly one space. After reading the input, the program should output the following three lines:
A vacation is when you take a trip to some tranquil place with your scandalous family. Usually you go to some place that is near a/an pancake or up on a/an field.
Notice that the first line should be completely blank. Replace the bold words (from the above example) with the actual words input by the user.
Your final program should have four input statements, three print statements, and at least two comments. For completeness, write an appropriate docstring at the top of the program.