Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the percentage sign mean in Python

In the tutorial there is an example for finding prime numbers:

>>> for n in range(2, 10): ...     for x in range(2, n): ...         if n % x == 0: ...             print(n, 'equals', x, '*', n//x) ...             break ...     else: ...         # loop fell through without finding a factor ...         print(n, 'is a prime number') ... 

I understand that the double == is a test for equality, but I don't understand the if n % x part. Like I can verbally walk through each part and say what the statement does for the example. But I don't understand how the percentage sign falls in.

What does if n % x actually say?

like image 984
Lonnie Price Avatar asked Jun 07 '09 06:06

Lonnie Price


People also ask

What does %s mean in Python?

%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string.

What does percent sign mean in coding?

JavaScript has many operators. One of them is the percent sign: % . It has a special meaning in JavaScript: it's the remainder operator. It obtains the remainder between two numbers. This is different from languages like Java, where % is the modulo operator.

What does %- 10s mean in Python?

As an example the value of %10s reserves 10 characters, with the extra spacing on the left side of the placeholder, and a value of %-10s puts any extra space to the right of the placholder. The single padding character is a space, and cannot be changed.

What Is percent sign in Jupyter notebook?

The percent format is a representation of Jupyter notebooks as scripts, in which all cells are explicitly delimited with a commented double percent sign # %% . The percent format is currently available for these languages.


1 Answers

The % does two things, depending on its arguments. In this case, it acts as the modulo operator, meaning when its arguments are numbers, it divides the first by the second and returns the remainder. 34 % 10 == 4 since 34 divided by 10 is three, with a remainder of four.

If the first argument is a string, it formats it using the second argument. This is a bit involved, so I will refer to the documentation, but just as an example:

>>> "foo %d bar" % 5 'foo 5 bar' 

However, the string formatting behavior is supplemented as of Python 3.1 in favor of the string.format() mechanism:

The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer str.format() interface helps avoid these errors, and also provides a generally more powerful, flexible and extensible approach to formatting text.

And thankfully, almost all of the new features are also available from python 2.6 onwards.

like image 107
SingleNegationElimination Avatar answered Oct 08 '22 22:10

SingleNegationElimination