The Python Modulo Operator - What Does the % Symbol Mean in Python? (Solved) (2024)

/ #Python

When you see the % symbol, you may think "percent". But in Python, as well as most other programming languages, it means something different.

The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem.

The modulo operator is considered an arithmetic operation, along with +, -, /, *, **, //.

The basic syntax is:

a % b

In the previous example a is divided by b, and the remainder is returned. Let's see an example with numbers.

7 % 2

The result of the previous example is one. Two goes into seven three times and there is one left over.

The diagram below shows a visual representation of 7 / 2 and 7 % 2 (The "R" stands for "remainder"). The single logo on the right side (with the green arrow pointing at it) is the remainder from the division problem. It is also the answer to 7 % 2.

The Python Modulo Operator - What Does the % Symbol Mean in Python? (Solved) (1)

Here is another example:

3 % 4

This will result in three. Four does not go into three any times so the original three is still left over. The diagram below shows what is happening. Remember, the modulo operator returns the remainder after performing division. The remainder is three.

The Python Modulo Operator - What Does the % Symbol Mean in Python? (Solved) (2)

Example using the Modulo Operator

One common use for the Modulo Operator is to find even or odd numbers. The code below uses the modulo operator to print all odd numbers between 0 and 10.

for number in range(1, 10): if(number % 2 != 0): print(number)

Result:

13579

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

The Python Modulo Operator - What Does the % Symbol Mean in Python? (Solved) (3)
Beau Carnes

I'm a teacher and developer with freeCodeCamp.org. I run the freeCodeCamp.org YouTube channel.

If this article was helpful, .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

The Python Modulo Operator - What Does the % Symbol Mean in Python? (Solved) (2024)

FAQs

The Python Modulo Operator - What Does the % Symbol Mean in Python? (Solved)? ›

Modulus operator, it is used for remainder division on integers, typically, but in Python can be used for floating point numbers. 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.

What is the symbol for modulo in Python? ›

In Python, the modulus operator is a percent sign ( % ). The syntax is the same as for other operators.

What does %= mean in Python? ›

% Is the modulus operator. It returns the remainder of dividing number1 by number2.

What is a modulo (%) operator? ›

It is used to find the remainder when one integer is divided by another. The syntax of the modulus operator is as follows: result = dividend % divisor; Here, dividend is the number being divided, divisor is the number by which the dividend is divided, and % is the modulus operator.

What is the symbol for modulo? ›

Many programming languages, and calculators, have a mod operator, typically represented with the % symbol. If you calculate the result of a negative number, some languages will give you a negative result. e.g. -5 % 3 = -2.

What does the symbol mean in Python? ›

The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand.

What does %% do in Python? ›

%% stands in for a literal percent character, which renders as % .

What does %= mean in code? ›

%= modulus and assignment. x%=y. x= x%y. Takes modulus using the two operands and assigns the result to the left side operand.

What does (%) mean? ›

The percent sign % (sometimes per cent sign in British English) is the symbol used to indicate a percentage, a number or ratio as a fraction of 100.

What does %s represent in Python? ›

%s indicates a conversion type of string when using Python's string formatting capabilities. More specifically, %s converts a specified value to a string using the str() function. Compare this with the %r conversion type that uses the repr() function for value conversion.

What does mod (%) do? ›

The modulo operation (abbreviated “mod”, or “%” in many programming languages) is the remainder when dividing. For example, “5 mod 3 = 2” which means 2 is the remainder when you divide 5 by 3.

What is the answer of modulo? ›

The modulo is defined as a remainder value when two numbers are divided. The mathematical representation of the modulo function is given as a mod b, where a and b are two numbers. When 16 is divided by 3, the quotient obtained is 5, and it leaves the remainder 1. Hence, the 16 mod 3 is equal to 1.

What calculation does the modulo (%) operator perform? ›

The modulo operation finds the remainder, so if you were dividing a by b and there was a remainder of n, you would say a mod b = n.

What is the modulus symbol in Python? ›

Python Modulus Operator is an inbuilt operator that returns the remaining numbers by dividing the first number from the second. It is also known as the Python modulo. In Python, the modulus symbol is represented as the percentage (%) symbol. Hence, it is called the remainder operator.

What is the mod symbol called? ›

The Modulus is the remainder of the euclidean division of one number by another. % is called the modulo operation. For instance, 9 divided by 4 equals 2 but it remains 1 . Here, 9 / 4 = 2 and 9 % 4 = 1 .

What is the modulo function? ›

b = mod( a , m ) returns the remainder after division of a by m , where a is the dividend and m is the divisor. This function is often called the modulo operation, which can be expressed as b = a - m.

What does %D represent in Python? ›

2 : What is %s and %D in Python? Answer: The %s and %d are format specifiers used to format strings and numbers respectively. The %s operator allows you to add a value to a python string and can be used to concatenate multiple strings. The %d operator allows you to add numbers within the strings.

What is mod () in Python? ›

mod is short for modulo, a math function that means to get the remainder of division. It is represented with a % in python and written X % Y this can be read as X mod Y or X modulo Y. It is used to get the remained from a division. For example: 15 % 4 = 3 >> 15 / 4 = 12 with remainder 3.

What is symbol module in Python? ›

The symbol module, used in Example 13-20, contains a listing of non-terminal symbols from the Python grammar. It's probably only useful if you're dabbling with the parser module.

How do you write modulo? ›

The modulo operation (abbreviated “mod”, or “%” in many programming languages) is the remainder when dividing. For example, “5 mod 3 = 2” which means 2 is the remainder when you divide 5 by 3.

Top Articles
Margin Trading
“Buy land, they're not making it anymore” Mark Twain - Hilliker Corp
Section 4Rs Dodger Stadium
Kansas City Kansas Public Schools Educational Audiology Externship in Kansas City, KS for KCK public Schools
Terrorist Usually Avoid Tourist Locations
Tabc On The Fly Final Exam Answers
Top Financial Advisors in the U.S.
Retro Ride Teardrop
Flights to Miami (MIA)
Wal-Mart 140 Supercenter Products
CA Kapil 🇦🇪 Talreja Dubai on LinkedIn: #businessethics #audit #pwc #evergrande #talrejaandtalreja #businesssetup…
Baseball-Reference Com
The Blind Showtimes Near Showcase Cinemas Springdale
Hmr Properties
Med First James City
I Wanna Dance with Somebody : séances à Paris et en Île-de-France - L'Officiel des spectacles
Guilford County | NCpedia
2 Corinthians 6 Nlt
Wicked Local Plymouth Police Log 2022
Vistatech Quadcopter Drone With Camera Reviews
Voy Boards Miss America
U Arizona Phonebook
Keurig Refillable Pods Walmart
Cbssports Rankings
A Person That Creates Movie Basis Figgerits
Craigslist Apartments Baltimore
TeamNet | Agilio Software
Bra Size Calculator & Conversion Chart: Measure Bust & Convert Sizes
Wku Lpn To Rn
John Philip Sousa Foundation
Ncal Kaiser Online Pay
Hannah Jewell
Duke Energy Anderson Operations Center
Landing Page Winn Dixie
Gideon Nicole Riddley Read Online Free
Gabrielle Enright Weight Loss
404-459-1280
Tendermeetup Login
Sunrise Garden Beach Resort - Select Hurghada günstig buchen | billareisen.at
Sabrina Scharf Net Worth
Clausen's Car Wash
Emily Browning Fansite
N33.Ultipro
Gt500 Forums
The Blackening Showtimes Near Ncg Cinema - Grand Blanc Trillium
Kate Spade Outlet Altoona
Gander Mountain Mastercard Login
The top 10 takeaways from the Harris-Trump presidential debate
Smoke From Street Outlaws Net Worth
Www Ventusky
Morbid Ash And Annie Drew
Festival Gas Rewards Log In
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5973

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.