What Is the Result of % in Python

What is the result of % in Python?

The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [2].

Taken from http://docs.python.org/reference/expressions.html

Example 1:
6%2 evaluates to 0 because there's no remainder if 6 is divided by 2 ( 3 times ).

Example 2: 7%2 evaluates to 1 because there's a remainder of 1 when 7 is divided by 2 ( 3 times ).

So to summarise that, it returns the remainder of a division operation, or 0 if there is no remainder. So 6%2 means find the remainder of 6 divided by 2.

What does 'result[::-1]' mean?

It represents the 'slice' to take from the result. The first element is the starting position, the second is the end (non-inclusive) and the third is the step. An empty value before/after a colon indicates you are either starting from the beginning (s[:3]) or extending to the end (s[3:]). You can include actual numbers here as well, but leaving them out when possible is more idiomatic.

For instance:

In [1]: s = 'abcdefg'

Return the slice of the string that starts at the beginning and stops at index position 2:

In [2]: s[:3]
Out[2]: 'abc'

Return the slice of the string that starts at the third index position and extends to the end:

In [3]: s[3:]
Out[3]: 'defg'

Return the slice of the string that starts at the end and steps backward one element at a time:

In [4]: s[::-1]
Out[4]: 'gfedcba'

Return the slice of the string that contains every other element:

In [5]: s[::2]
Out[5]: 'aceg'

They can all be used in combination with each other as well. Here, we return the slice that returns every other element starting at index position 6 and going to index position 2 (note that s[:2:-2] would be more idiomatic, but I picked a weird number of letters :) ):

In [6]: s[6:2:-2]
Out[6]: 'ge'

The step element determines the elements to return. In your example, the -1 indicates it will step backwards through the item, one element at a time.

Why the result of “bag” > “apple” is True in Python?

I believe this is True because Python compares the first letter of each word. And b is greater than a in Python.

Difference between // and / in Python 2

// is the floored-division operator in Python. The difference is visible when dividing floating point values.

In Python2, dividing two ints uses integer division, which ends up getting you the same thing as floored division. However, you can still use // to get a floored result of floating point division. live example

# Python 2
>>> 10.0 / 3
3.3333333333333335
>>> 10.0 // 3
3.0

However, in Python3, dividing two ints results in a float, but using // acts as integer division. live example

# Python3
>>> 10 / 3
3.3333333333333335
>>> 10 // 3
3

If you are (still) working in Python2, but want to someday convert to Python3, you should always use // when dividing two ints, or use from __future__ import division to get the Python3 behavior in Python2

Floored division means round towards negative infinity. This is the same as truncation for positive values, but not for negative. 3.3 rounds down to 3, but -3.3 rounds down to -4.

# Python3
>>> -10//3
-4
>>> 10//3
3

Query parameter only returning one result

This won't work since you're using 'return' statement inside the loop, if for example you are looking for 'john' the first time this parameter is found return is executed and function ends.
You could for example save those values and return them all, let me show you:

  • Instead of return, declare a list ids = []at the beginning of the function and every time john is found you add the id to the results list, ids.append(students[id]). Finally after the loop just return ids list, or if len(ids) is 0 just return None for error management.

Code example:

    @app.get("/get-student")
async def get_student_by_name(name : str):
ids = []
for id in students:
if students[id]["name"] == name:
ids.append(students[id])
if len(ids) is 0:
return {"Data": "Student not found"}
else:
return ids


Related Topics



Leave a reply



Submit