Intro to python: using - in / and %

In summary, the expression -17/10 uses integer division, which returns an integer quotient. In this case, the quotient is -2 and the remainder is 3. This is because Python prefers positive remainders and -17 can be expressed as -2 multiplied by 10, plus 3.
  • #1
Brown Arrow
101
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
  • #2
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.
 

1. What is Python?

Python is a widely-used programming language that was first released in 1991. It is known for its simplicity, versatility, and readability, making it a popular choice for beginners and experienced programmers alike.

2. What is the difference between using -, /, and % in Python?

The minus (-) operator is used for subtraction, the forward slash (/) operator is used for division, and the percent (%) operator is used for modulus (finding the remainder after division).

3. How do I use the - operator in Python?

To use the minus operator in Python, you simply place it between two numbers or variables that you want to subtract. For example, 10 - 5 will result in 5.

4. Can I use the / operator with both integers and floats?

Yes, the / operator can be used with both integers and floats. However, when used with integers, it will always return a whole number (integer) as the result.

5. How do I use the % operator to find the remainder of a division?

To use the % operator, simply place it between two numbers that you want to divide and find the remainder of. For example, 10 % 3 will result in 1, as 10 divided by 3 leaves a remainder of 1.

Similar threads

  • Programming and Computer Science
Replies
10
Views
2K
Replies
6
Views
609
  • Programming and Computer Science
Replies
5
Views
979
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
3
Views
555
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
424
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top