Introduction to Python Programming

# 2.3Mixed data types

## Learning objectives

By the end of this section you should be able to

• Identify the data types produced by operations with integers, floats, and strings.
• Use operators and type conversions to combine integers, floats, and strings.

## Combining integers and floats

Programmers often need to combine numbers of different data types. Ex: A program computes the total for an online shopping order:

    quantity = int(input())
price = float(input())
total = quantity * price
print(total)

quantity is an integer, and price is a float. So what is the data type of total? For input 3 and 5.0, total is a float, and the program prints 15.0.

Combining an integer and a float produces a float. A float is by default printed with at least one figure after the decimal point and has as many figures as needed to represent the value. Note: Division using the / operator always produces a float.

## Concepts in Practice

### Operations combining integers and floats

1.
8 * 0.25
1. 2
2. 2.0
2.
2 * 9
1. 18
2. 18.0
3.
20 / 2
1. 10
2. 10.0
4.
7 / 2
1. 3.0
2. 3.5
5.
12.0 / 4
1. 3
2. 3.0
6.
8 - 1.0
1. 7.0
2. 7
7.
5 - 0.25
1. 4.5
2. 4.75

## Combining numeric types and strings

Easy type conversion in Python can lead a programmer to assume that any data type can be combined with another. Ex: Noor's program reads in a number from input and uses the number in a calculation. This results in an error in the program because the input() function by default stores the number as a string. Strings and numeric data types are incompatible for addition, subtraction, and division. One of the operands needs to be explicitly converted depending on the goal of arithmetic or string concatenation.

The * operator also serves as the repetition operator, which accepts a string operand and an integer operand and repeats the string. Ex: "banjo" * 3 produces "banjobanjobanjo".

## Concepts in Practice

### Operations combining numeric types and strings

8.
int('34') + 56
1. '3456'
2. 90
3. '90'
9.
str(12) + ' red roses'
1. '12 red roses'
2. '12'
3. Error
10.
'50' * 3
1. '150'
2. 150
3. '505050'
11.
str(5.2) + 7
1. 12.2
2. '12.2'
3. Error
12.
80.0 + int('100')
1. 180
2. 180.0
3. '180'
13.
str(3.14) + '159'
1. 162.14
2. '3.14159'
3. Error
14.
2.0 * 'this'
1. 'this'
2. 'thisthis'
3. Error

## Try It

### After the point

Write a program that reads in a string of digits that represents the digits after the decimal point of a number, num. Concatenate the input string together with '.' and num, and print the result. Ex: If input is 345, the program will print 2.345.

## Try It

### Print n times

Write a program that reads in two strings, str1 and str2, and an integer, count. Concatenate the two strings with a space in between and a newline ("\n") at the end. Print the resulting string count times.

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.