Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

1.9: Chapter summary

( \newcommand{\kernel}{\mathrm{null}\,}\)

This chapter introduced the basics of programming in Python, including:

  • print() and input().
  • 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, prompt is output to the user. The function then reads a line of input from the user.

len(string)
Returns the length (the number of characters) of a string.
type(value)

Returns the type (or class) of a value. Ex: type(123) is <class 'int'>.

Operator Description

=
(Assignment)

Assigns (or updates) the value of a variable. In Python, variables begin to exist when assigned for the first time.

+
(Concatenation)

Appends the contents of two strings, resulting in a new string.

+
(Addition)

Adds the values of two numbers.

-
(Subtraction)

Subtracts the value of one number from another.

*
(Multiplication)

Multiplies the values of two numbers.

/
(Division)

Divides the value of one number by another.

**
(Exponentiation)

Raises a number to a power. Ex: 3**2 is three squared.

Syntax Description

#
(Comment)

All text is ignored from the # symbol to the end of the line.

' or "
(String)

Strings may be written using either kind of quote. Ex: 'A' and "A" represent the same string. By convention, this book uses double quotes (") for most strings.

"""
(Docstring)

Used for documentation, often in multi-line strings, to summarize a program's purpose or usage.

Table 1.6 Chapter 1 reference.
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.


This page titled 1.9: Chapter summary is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

  • Was this article helpful?

Support Center

How can we help?