Skip to content
Fix Code Error

What is the result of % in Python?

March 13, 2021 by Code Error
Posted By: Anonymous

What does the % in a calculation? I can’t seem to work out what it does.

Does it work out a percent of the calculation for example: 4 % 2 is apparently equal to 0. How?

Solution

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.

Answered By: Anonymous

Related Articles

  • Ukkonen's suffix tree algorithm in plain English
  • Is CSS Turing complete?
  • Assembly Language - How to do Modulo?
  • Can't install via pip because of egg_info error
  • What does this symbol mean in JavaScript?
  • How to properly assert that an exception gets raised…
  • Why does integer division in C# return an integer…
  • insert tables in dataframe with years from 2000 to…
  • Design DFA accepting binary strings divisible by a…
  • Throw HttpResponseException or return…
  • Adding numbers and receiving a NaN
  • What's the difference between eval, exec, and compile?
  • Java ElasticSearch None of the configured nodes are…
  • How would I be able to multiple select and pass data…
  • Components - set style width of component
  • JavaScript gives NaN error on the page but variable…
  • What's the difference between a single precision and…
  • How to format a number as percentage in R?
  • How can I pass a wct test while rearranging children…
  • How to calculate Cohen's D across 50 points in R
  • Retain precision with double in Java
  • Reference — What does this symbol mean in PHP?
  • Change column type in pandas
  • Why does C++ code for testing the Collatz conjecture…
  • What is a NullReferenceException, and how do I fix it?
  • Understanding The Modulus Operator %
  • How do I log a Python error with debug information?
  • Is floating point math broken?
  • What is an IndexOutOfRangeException /…
  • How to convert a data frame column to numeric type?
  • How to truncate float values?
  • sql query to find priority jobs
  • Install pip in docker
  • How can I throw CHECKED exceptions from inside Java…
  • Why does a fixed-point scaling factor tend to be a…
  • How do I merge two dictionaries in a single…
  • How to use java.Set
  • What are the undocumented features and limitations…
  • Why does changing 0.1f to 0 slow down performance by 10x?
  • Plotly: Show value in hoverlabel instead of…
  • Finding all possible combinations of numbers to…
  • Asynchronous props in vue
  • How to update Python?
  • Manually raising (throwing) an exception in Python
  • Ember.RSVP.All error Array Methods must be provided an Array
  • How can I determine whether a 2D Point is within a Polygon?
  • Printf width specifier to maintain precision of…
  • no match for ‘operator
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Sorting 1 million 8-decimal-digit numbers with 1 MB of RAM
  • Why does modulus division (%) only work with integers?
  • Python code from C code does not give the same results
  • Making custom functions in R involving summation
  • Best way to compare two complex objects
  • Convert seconds value to hours minutes seconds?
  • How to create a validation with conditionals if a…
  • How to convert a factor to integernumeric without…
  • Do you (really) write exception safe code?
  • What is the difference between '/' and '//' when…
  • Plotting multiple time series on the same plot using…
  • How to code a modulo (%) operator in C/C++/Obj-C…
  • For IEEE-754 floating point arithmetic, is the…
  • Easy interview question got harder: given numbers…
  • C compile error: Id returned 1 exit status
  • setTimeout in Vue method not working
  • How using try catch for exception handling is best practice
  • pandas: label based on custom percentile for light tail data
  • How to compare arrays in JavaScript?
  • Equals implementation with override Equals,…
  • Fastest way to determine if an integer's square root…
  • Tkinter Duration Timer—What method will allow this…
  • Insert a (mostly) blank row into an R dataframe for…
  • Text Progress Bar in the Console
  • Difference between numeric, float and decimal in SQL Server
  • What is the correct way to split an aurelia app into…
  • Why do I need to override the equals and hashCode…
  • Modulo operator with negative values
  • Division in Python 2.7. and 3.3
  • C++ Best way to get integer division and remainder
  • Binding complex object to a component
  • Smart way to truncate long strings
  • percentage of two int?
  • How can I force division to be floating point?…
  • Is it a good practice to use try-except-else in Python?
  • Reference - What does this regex mean?
  • Are dictionaries ordered in Python 3.6+?
  • Doubts about js number representation and his bit…
  • What is the copy-and-swap idiom?
  • How to generate a random string of a fixed length in Go?
  • Not Equal to This OR That in Lua
  • How to format a phone number in a textfield
  • Vue.js. Passing dynamically changing data to child component
  • How can I use modulo operator (%) in JavaScript?
  • How Does Modulus Divison Work
  • How to display a 32-bit DIV result stored in DX:AX
  • Toggle floating filter in Ag-Grid?
  • CSS table column autowidth
  • How do you read a Python Traceback error?
  • How to avoid a System.Runtime.InteropServices.COMException?
  • Binding between two arbitrary objects in Ember.js

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How To Auto-Format / Indent XML/HTML in Notepad++

Next Post:

How to make IPython notebook matplotlib plot inline

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error