Intro to python: using - in / and %

  • Context: Python 
  • Thread starter Thread starter Brown Arrow
  • Start date Start date
  • Tags Tags
    Intro Python
Click For Summary
SUMMARY

The discussion clarifies the behavior of integer division in Python, specifically with the expression -17/10. In Python, this expression evaluates to -2, with a remainder of 3, which ensures that the remainder is positive. The explanation highlights that while one might expect the quotient to be -1, Python's rules for integer division dictate that the result must maintain a positive remainder, leading to the conclusion that -17 = -2 * 10 + 3.

PREREQUISITES
  • Understanding of Python 3.x syntax and operations
  • Familiarity with integer division concepts
  • Knowledge of the modulus operator (%) in Python
  • Basic arithmetic and long division techniques
NEXT STEPS
  • Explore Python 3.x integer division and its behavior with negative numbers
  • Learn about the Python modulus operator and its applications
  • Study the differences between integer division and floating-point division in Python
  • Practice long division and remainder calculations with various integer pairs
USEFUL FOR

Beginner Python programmers, educators teaching Python basics, and anyone looking to understand integer division and the modulus operator in Python.

Brown Arrow
Messages
101
Reaction score
0
im reading a book intro to python, I am confused by what is ment by the following
>>>-17/10
-2
i do not understand this explain pls

NOT A HW
 
Technology news on Phys.org
The expression -17/10 uses integer division, in which the value returned (the quotient) is an integer.

It's probably a bit easier to understand if both numbers involved are positive. For example, 17/10 evaluates to 1. If you do the long division, you'll see that the quotient is 1 and the remainder is 7, which you can find by evaluating the expression 17 % 10.

With -17/10, it would seem reasonable that the quotient would be -1, but this would result in a remainder of -7. Python seems to like the remainders to be positive, and for this problem gives a remainder of + 3. I.e., -17/10 == 3. This means that -17/10 == -2 plus a remainder of 3. To check this, note that -17 = -2 * 10 + 3.

Hope this helps.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
Replies
6
Views
3K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K