Programming fundamentalsLesson 3 of 8 in the course Expressions

Python Numeric Types, Conversion, and String Operations

5 flashcards · 5 quiz questions · 5 match pairs · 4 fill-in sentences · English

Lesson progress
0 / 5

Your progress is kept in this browser only.Sign up to save it

Summary

Python expressions can combine values, but the data types of those values affect which operations are allowed and what type of result is produced. This lesson explains how Python handles integers, floats, and strings, and how to use type conversion when working with them.

An integer represents a whole number, such as 3. A float represents a number with a decimal point, such as 5.0 or 2.75.

All flashcards

What happens when an arithmetic operation combines an integer and a float?
Python performs the numeric operation and produces a float result. For example, `3 * 5.0` evaluates to `15.0`.
What does `input()` return, and why does that matter?
`input()` returns a string, even if the user types digits. Convert the result with `int()` or `float()` before using it in numeric calculations.
What is string concatenation?
String concatenation joins strings using `+`. Both operands should be strings, so convert a number with `str()` before joining it with text.
What does the `/` operator return?
The `/` operator always returns a float, including when the division has no fractional part; for example, `6 / 2` produces `3.0`.
How does string repetition work?
The `*` operator repeats a string when paired with an integer. For example, `"banjo" * 3` produces `"banjobanjobanjo"`.

Quiz questions

  1. What data type results from multiplying an integer by a float in Python?Answer: A float
  2. What data type does `input()` return by default?Answer: A string
  3. What type of value does the `/` operator produce in Python?Answer: A floating-point number
  4. A program reads a quantity with `input()` and needs to multiply it by a price. What should it do first?Answer: Parse the quantity with `int()` or `float()`.
  5. How can a program create the text `"2.345"` from the integer `2` and the string `"345"`?Answer: Convert `2` to a string, then concatenate the pieces.

Based on Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield, Narges Norouzi (OpenStax), licensed CC BY 4.0. Adapted into a summary, flashcards and quizzes.

All Programming fundamentals lessons