Operators and input
Build calculations deliberately, distinguish division operations, and convert interactive input.
By the end, you’ll be able to…
- Use arithmetic operators and parentheses to express a calculation.
- Explain the different results of /, //, and %.
- Convert text returned by input before doing numeric work.
01Write the relationship before the expression
An operator combines or transforms values. You already know +, -, and *; Python also provides / for division and ** for exponentiation. Begin with the relationship in ordinary language. If a delivery costs a fixed fee plus a price per kilometer, the total is fixed_fee + distance * rate. Naming those parts makes the expression easier to inspect than a line of unexplained numbers.
Python follows precedence rules: multiplication and division bind more tightly than addition and subtraction. Parentheses let you state grouping explicitly. The expression (2 + 3) * 4 calculates a different relationship from 2 + 3 * 4. Do not add parentheses randomly until an answer looks plausible. Explain which values belong together and predict an intermediate result first.
02Division can answer different questions
Ordinary division with / produces a float for integer operands: 17 / 5 is 3.4. Floor division with // gives the quotient rounded down toward negative infinity, so 17 // 5 is 3. The remainder operator % supplies what is left: 17 % 5 is 2. For positive counts, these operations help answer 'how many full groups?' and 'how many items remain?'.
Be careful with negative numbers: floor division does not simply remove the decimal part. -7 // 3 is -3, whereas int(-7 / 3) is -2. You rarely need negative item counts, but the distinction prevents an inaccurate mental model. Division by zero raises an error, so the divisor must be meaningful for your problem. Test a calculation with an easy case you can solve by hand.
03Input crosses the boundary from text
The input function displays an optional prompt, waits for a line, and returns the entered text without its trailing newline. Even when a person types 12, the returned value is a string. Convert that string with int or float before numeric calculation. Otherwise + joins strings and multiplying a string by an integer repeats it, which can produce a plausible-looking but wrong result.
This program is interactive. Save it as study_minutes.py and run it in a terminal using your installed Python interpreter. Enter 12 when prompted to reproduce the expected transcript below. We deliberately assume valid nonnegative integer input in this first version. Entering twelve would raise ValueError during conversion. Later, exceptions and validation will let you respond helpfully instead of assuming every entry is suitable.
minutes_text = input("Minutes per session: ")minutes = int(minutes_text)sessions = 3print("Total minutes:", minutes * sessions)Minutes per session: 12 Total minutes: 36
04Inspect the result and its units
A correct expression should have sensible units as well as a sensible number. Multiplying minutes per session by a session count yields minutes, not hours. To convert to hours, divide the total minutes by 60. For deterministic examples in the course, replace input with a fixed string so everyone can reproduce the same result without typing.
Floating-point values approximate many decimal fractions; formatting a result later will control its display but will not make the stored calculation exact. For counting objects, integers are often the clearest choice. Keep intermediate values under descriptive names, then print the finished result with its label. That structure makes a wrong conversion or misplaced parenthesis much easier to locate.
The ideas worth keeping.
- Use parentheses to express intended grouping and verify the units of a calculation.
- Use / for ordinary division, // for a floor quotient, and % for a remainder.
- input returns text, so convert deliberately before numeric work.
Make it yours.
Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.
Groups and leftovers
There are 38 notebooks and each bundle holds 6. Print the number of complete bundles and the number left over, each with a label.
Explore a worked solution
notebooks = 38bundle_size = 6print("Complete bundles:", notebooks // bundle_size)print("Left over:", notebooks % bundle_size)Floor division produces 6 full bundles and remainder produces 2 notebooks. The check 6 * 6 + 2 == 38 accounts for every notebook.
Calculate a trip duration
Use distance_text = '150' and speed_text = '60' as if they came from input. Convert them to numbers and print the duration in hours. Assume a constant speed and valid positive numeric text.
Explore a worked solution
distance_text = "150"speed_text = "60"distance_km = float(distance_text)speed_km_per_hour = float(speed_text)hours = distance_km / speed_km_per_hourprint("Hours:", hours)Converting both strings enables division. Kilometers divided by kilometers per hour gives 2.5 hours; keeping units in the names helps verify the relationship.
What stayed with you?
Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.
Groups and leftovers
There are 38 notebooks and each bundle holds 6. Print the number of complete bundles and the number left over, each with a label.
Complete bundles: notebooks // bundle_size Left over: notebooks % bundle_size
Keep exploring.
The official references behind this chapter, when you want a second angle or the full details.
Pause. Put the pieces together.
You’ve reached the end of Getting started with Python. Before moving on, use this task to check what you can do independently.
Change an input, predict the calculation, and confirm the saved file produces your expected output.
Try it in your own environment and write down the result. This is a self-check; reading completion does not evaluate your code.
Review this moduleOne chapter closer.
Reading is a start. Trying it yourself makes the connection.