Sorry, for the typo in title. I wrote this while about to sleep.
I copied this code from a phone app I use to practice C++. My only question here is with the number (2). I know that it is a prime number but the function will test 2%2 and gets 0, so it should output (not prime), which is wrong, but it is working perfectly and it outputs that 2 is prime. Can someone please just explain this part to me? Thank you

Technically speaking, we are exploring a domain that is,

Real

Integer

Natural

Emphasis on the last one. Prime numbers are found only within the Natural number set. At least there are some constraints.

Any value less than plus two is not worth evaluating since it is moot.

if n < 2: raise ValueError

That’s pseudo code so don’t try to run it unless it’s in Python.

The technique used in the above code (OP) is known as ‘brute force’. It chews through all the values from a to b and checks for divisibility. Failing that yields a prime.

If one is looking for criticism of this algorithm, none is forthcoming. It is what it is and we know how it works. That is enough critical discussion.

In any language we are going to want to keep any iterative process in tight check, and demand very little of such processes. Primeness is a great CS101 subject to explore at length, or reasonably great depth, even as a breather from one’s course load. Tease your brain.