Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Explain how the interpreter uses implicit type conversion.
  • Use explicit type conversion with int(), float(), and str().

Implicit type conversion

Common operations update a variable such that the variable's data type needs to be changed. Ex: A GPS first assigns distance with 250, an integer. After a wrong turn, the GPS assigns distance with 252.5, a float. The Python interpreter uses implicit type conversion to automatically convert one data type to another. Once distance is assigned with 252.5, the interpreter will convert distance from an integer to a float without the programmer needing to specify the conversion.

Checkpoint

Example: Book ratings

Concepts in Practice

Implicit type conversion in practice

Consider the example above.

1.
What is book_rating's data type on line 7?
  1. float
  2. integer
2.
What would book_rating's data type be if update = 1.0 instead of 0.5?
  1. float
  2. integer
3.
What is the data type of x after the following code executes?
x = 42.0
x = x * 1
  1. float
  2. integer

Explicit type conversion

A programmer often needs to change data types to perform an operation. Ex: A program should read in two values using input() and sum the values. Remember input() reads in values as strings. A programmer can use explicit type conversion to convert one data type to another.

  • int() converts a data type to an integer. Any fractional part is removed. Ex: int(5.9) produces 5.
  • float() converts a data type to a float. Ex: float(2) produces 2.0.
  • str() converts a data type to a string. Ex: str(3.14) produces "3.14".

Checkpoint

Example: Ordering pizza

Concepts in Practice

Example: Ordering pizza

Consider the example above.

4.
Which function converts the input number of slices to a data type that can be used in the calculation?
  1. float()
  2. input()
  3. int()
5.
How could line 3 be changed to improve the program overall?
  1. Use float() instead of int().
  2. Add 1 to the result of int().
  3. Add str() around int().

Concepts in Practice

Using int(), float(), and str()

Given x = 4.5 and y = int(x), what is the value of each expression?

6.
y
  1. 4
  2. 5
7.
str(x)
  1. 4.5
  2. "4.5"
8.
float(y)
  1. 4.0
  2. 4.5

Try It

Grade average

The following program computes the average of three predefined exam grades and prints the average twice. Improve the program to read the three grades from input and print the average first as a float, and then as an integer, using explicit type conversion. Ignore any differences that occur due to rounding.

Try It

Cups of water

The following program should read in the ounces of water the user drank today and compute the number of cups drank and the number of cups left to drink based on a daily goal. Assume a cup contains 8 ounces. Fix the code to calculate cups_drank and cups_left and match the following:

  • ounces is an integer representing the ounces the user drank.
  • cups_drank is a float representing the number of cups of water drank.
  • cups_left is an integer representing the number of cups of water left to drink (rounded down) out of the daily goal of 8 cups.

Try It

Product as float

The following program reads two integers in as strings. Calculate the product of the two integers, and print the result as a float.

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

© Mar 15, 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.