Numbers and Conversion
Lesson 4 of 48 in Coddy's Python for JavaScript Developers course.
Convert numeric text explicitly and choose the division rule your calculation needs.
JavaScript
const temperature = Number("-7");
console.log(temperature / 2);
console.log(Math.floor(temperature / 2));Python
temperature = int("-7")
print(temperature / 2)
print(temperature // 2)What changes
Both programs print -3.5 followed by -4. Python integers have type int; a literal such as 2.5 is a float. Use int("12") for integer text and float("12.5") for decimal text. Arithmetic does not generally convert strings for you: "12" + "3" joins text, while mixing a string and an integer with + raises a type error. For integer operands, / produces a floating-point result even when the quotient is whole. Integer // produces the floor of the quotient. Multiplication, subtraction, and parentheses look familiar from JavaScript.
Common mistake
Flooring a negative quotient moves downward, not toward zero. Also, int("3.5") is invalid integer text; it does not quietly truncate that string. Converting the numeric value -3.5 with int truncates toward zero, which is a different operation from floor division.
Your task
Print both division results from supplied integer text. The negative test cases distinguish flooring from truncation; the zero case checks that numeric conversion happens before arithmetic.
Challenge
BeginnerRead an integer numerator and a positive integer divisor on separate lines. Convert both input strings to integers. Print true division on line one and floor division on line two. Numerators are between -100 and 100; divisors are 1, 2, or 4, so the quotients have exact binary representations. Use Python's default printed number format.
Print only the requested output, with a newline after each line. The supplied input lines read the test data; keep them and add your work below.
Your code goes in main.py. The locked driver.py starts your program and cannot be edited.
Try it yourself
# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module
run_module("main", run_name="__main__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for JavaScript Developers
1From JavaScript to Python
Run Your First Python ProgramPrint, Comments, and StringsVariables Without let or constNumbers and ConversionBlocks Use IndentationRecap: Rewrite a Receipt4Iteration and Transformations
for Loops Iterate ValuesIndexes and Dict Iterationwhile, break, and continueComprehensions and SortingRecap: Build a Ranking2Values and Decisions
None, True, and FalseEquality and IdentityTruthiness and Short-CircuitBranches and ExpressionsRecap: Eligibility Rules3Collections and References
Lists Instead of ArraysTuples and UnpackingDictionaries and RecordsSets, Aliasing, and CopiesRecap: Clean an Inventory6Reliable Python Programs
Input and Output ContractsExceptions and ValidationModules and the Main GuardReading Type HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler