Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Highlights from this chapter include:

  • Expressions and statements can be run interactively using a shell.
  • Input strings can be converted to other types. Ex: int(input()).
  • Strings can be concatenated with other types. Ex: "$" + str(cost).
  • Floats are subject to round-off and overflow errors.
  • Integers can be divided exactly using // and %.
  • Modules like math provide many useful functions.
  • Formatting long lines helps improve readability.

At this point, you should be able to write programs that ask for input of mixed types, perform mathematical calculations, and output results with better formatting. The programming practice below ties together most topics presented in the chapter.

Function Description
abs(x)

Returns the absolute value of x.

int(x)

Converts x (a string or float) to an integer.

float(x)

Converts x (a string or integer) to a float.

str(x)

Converts x (a float or integer) to a string.

round(x, ndigits)

Rounds x to ndigits places after the decimal point. If ndigits is omitted, returns the nearest integer to x.

Operator Description

s * n
(Repetition)

Creates a string with n copies of s. Ex: "Ha" * 3 is "HaHaHa".

x / y
(Real division)

Divides x by y and returns the entire result as a float. Ex: 7 / 4 is 1.75.

x // y
(Floor division)

Divides x by y and returns the quotient as an integer. Ex: 7 // 4 is 1.

x % y
(Modulo)

Divides x by y and returns the remainder as an integer. Ex: 7 % 4 is 3.

Table 2.10 Chapter 2 reference.

Try It

Baking bread

The holidays are approaching, and you need to buy ingredients for baking many loaves of bread. According to a recipe by King Arthur Flour, you will need the following ingredients for each loaf:

  • 1 1/2 teaspoons instant yeast
  • 1 1/2 teaspoons salt
  • 1 1/2 teaspoons sugar
  • 2 1/2 cups all-purpose flour
  • 2 cups sourdough starter
  • 1/2 cup lukewarm water

Write a program that inputs the following variables: bread_weight (float), serving_size (float), and num_guests (int). The output will look like the following:

Note: The measures the program comes up with are exact, but to bake, the baker would have to use some approximation. Ex: 9.765625 cups all-purpose flour really means 9 and 3/4 cups.

    For 25 people, you will need 3.90625 loaves of bread:

      5.859375 teaspoons instant yeast
      5.859375 teaspoons salt
      5.859375 teaspoons sugar
      9.765625 cups all-purpose flour
      7.8125 cups sourdough starter
      1.953125 cups lukewarm water

In the above output, bread_weight is 16.0 ounces, serving_size is 2.5 ounces, and num_guests is 25 people. Use these three variables to calculate the number of loaves needed.

Make sure your output matches the above example exactly. Notice that each line of the ingredients begins with two spaces.

Try It

Tip calculator

Google has a variety of search tricks that present users with instant results. If you search on Google for tip calculator, an interactive tool is included at the top of the results. The goal of this exercise is to implement a similar tip calculator.

Begin by prompting the user to input the following values (user input in bold):

43.21
    Percentage to tip: 18
    Number of people: 2
    Enter bill amount: 43.21
    Percentage to tip: 18
    Number of people: 2

Then calculate the tip amount and total amount for the bill, based on the user input. Output the results using this format:

    Tip amount: $7.78
    Total amount: $50.99
    
    Tip per person: $3.89
    Total per person: $25.49

Your program should output all dollar amounts rounded to two decimal places. The output should be exactly six lines, as shown above. Notice the blank line before each section of the output. Notice also the space before but not after the dollar sign.

Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.