Python (Fall 2026) Module 4-1: Introduction and Type Conversions
Overview
Bill Moseley introduces Python type-conversion functions as a prerequisite for manipulating data, showing why input such as a GPA of "3.5" must be converted from a string to a float before arithmetic. He demonstrates conversion behavior for strings, numbers, and booleans, including errors from incompatible inputs, truthiness rules, and how converting a float to an integer truncates rather than rounds.
Key takeaways
- Python's input function always returns a string, so numeric input such as a GPA of 3.5 must be converted with float() before arithmetic.
- Type-conversion functions can raise errors when the supplied text does not match the target type's format, as with converting "4 0" to a float.
- bool() tests whether a value is empty or zero, not whether text spells a logical value: bool("false") is True, while bool("") is False.
- The only numeric value that converts to False in the examples is zero; positive and negative nonzero values convert to True.
- Converting a float directly to int truncates the fractional component rather than rounding, so int(356.10) returns 356.
- A decimal string can be converted to an integer indirectly by first converting it to float and then converting that float to int.
Chapters
- Module 4 focuses on operators and expressions, concepts used to manipulate data.
- A type-conversion function returns a value in a different type, such as converting a string to a float.
- Python's input function returns a string even when the user enters digits and a decimal point.
- Entering 3.5 for a GPA produces the string "3.5", so subtracting it directly from 4.0 raises an error.
- Calling float(strGPA) creates a numeric value that can be used to calculate the difference from 4.0.
- A GPA input of 4 0 cannot be converted to a float because it does not match a valid float format.
- The calculation with 3.2 produces a small floating-point precision artifact, which Moseley notes will be explained later.
- Converting any nonempty string to bool gives True, including the strings "false" and "Bill"; only an empty string gives False.
- For numeric values, bool(0) is False, while nonzero values such as 10 and -10 are True.
- A decimal string cannot be converted directly to int, but it can be converted to float and then to int.
- Converting a float such as 356.10 to int drops the fractional part and returns 356 rather than rounding to 357.
Transcript excerpt
0:02 All right, welcome back. Today we're going to start with module four. Hopefully you were able to complete module three last week. Today is going to be the entire module four. So, we'll pick up module five on Thursday. So, today I'm going to give you some new material. I'm going to go over the sandbox and the assignment, but you should still have a decent amount of time to work on those things if you choose to use the work time today. So today we're going to be talking about two concepts mainly, called operators and expressions. And operators and expressions fundamentally are all about manipulating data. So, doing things to data. I'll get into that in just a second. Before we go into that, though, I want to introduce to you a concept called type conversion
1:02 functions. And I think we've touched on this a little bit, but a type conversion function is a function that changes the type of data, or it returns the data that you give it in a different type. So, for example, if I have a string that somebody entered, like maybe I asked them in an input for their GPA and they enter 3.5. We've talked about this before, but all inputs return only strings. So if they type 3.5 as a response to an input, what I end up with is the string 3.5, right? It's not a float, but it's a string that could be a float. So if I want to do math and say,
1:58 tell that person how many grade points they are below a 4.0, you know, I would have to first change that to a float, and then I could do that math where I subtract 3.5 from 4.0 and tell them, hey, you are 0.5 below a 4.0. So let's just, let's just look at that in practice. So I've got
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.