Discover how to effortlessly convert raw minutes into IMDB-style movie durations using Python's modulus operator and integer truncation. Simplify your coding workflow with clear steps and concise formatting techniques.
Key Insights
- Utilize the modulus operator (%) in Python to extract remainder minutes; for example, dividing 104 minutes by 60 yields 1 hour and a 44-minute remainder.
- Apply the int() function to truncate decimals effectively, converting numbers like 1.73 into whole numbers by removing the decimal portion.
- Add helpful inline notes formatted with hashtags and bold text, as demonstrated in the article, to clearly document coding methods such as converting 145 raw minutes into "2 hours 25 min" IMDB-style formats.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
Let's talk a little bit more about how we got the minutes in this modulus operator. And also, we don't need to wrap it in int. It'll return a number.
A remainder is an integer anyway. So, that's going to be the remainder of movie mins divided by 60, which is whatever the number is. So, if you take 104 and divide it by 60, you get 1 remainder 44.
So, that's the little trick to get the minutes, the remainder minutes. So, let's say you go back to a 2-hour movie, 156 minutes. Run, 2-hours 36 minutes.
Really long movie, 3-hours and 4 minutes. So, a shorter movie, it's totally working. So, we're using int, because kind of the theme that we've been working with here is dealing with numbers, right? Turning stringy numbers into ints.
In this case, we're not turning a stringy number into an int. We're truncating a decimal. So, like 1.73, whatever, turns into just drops.
Truncating or drop decimal by inting it. And then the remainder, and you CONCATENATE the two of them together, and that's a little move to get IMDB time. In fact, why don't we add a note? It's good practice for you.
Let's do this. We'll say triple hashtag double star using int num and modulus mod operator to format raw minutes as IMDB time. Maybe we just make that normal size.
So, we would have, say, 145 minutes would become converts to IMDB time of 2-hours 25 min. So, it's good to be able to come in here anytime you want and write little notes. Using num, int rather, and mod to format raw minutes to get, and we'll say, yeah, that's fine.
Okay. So, every now and then you want to come in and write your own notes, I would think, right? Which is great. Just hit the little text thing and remember the rules.
Hashtag for size, double stars for bold, dash for bullet point. Make this shorter. We'll say get IMDB time, make IMDB time a movie duration format from raw minutes or total minutes.
So, 145 total minutes in IMDB time is boom. So, I think that's good that we just did that. Save a copy and drive.
Okay. What's going on here? All right. Keep it rolling.