I am taking a codeacademy beginners course in Python. What does the character "%" mean in this expression - 45 % 12? I am using Python 2.7.5. i have looked at various references, and cannot find it.
            Asked
            
        
        
            Active
            
        
            Viewed 176 times
        
    -4
            
            
        - 
                    It's a modulus operator. – Anderson Green Jan 23 '14 at 23:09
- 
                    1http://stackoverflow.com/questions/4432208/how-does-work-in-python – mhlester Jan 23 '14 at 23:09
- 
                    @mhlester Yes, that's a duplicate of this question. – Anderson Green Jan 23 '14 at 23:10
- 
                    1Yeah I'm out of flags for the day :) – mhlester Jan 23 '14 at 23:10
- 
                    I have some slight doubt that you have looked at "various references".... – nye17 Jan 23 '14 at 23:11
- 
                    second hit with google, so -1: "% operator python" resulted in "Modulus - Divides left hand operand by right hand operand and returns remainder". I hardly believe you could not find a proper explanation, this operator exists in so many languages. Sorry for your first question. – angabriel Jan 23 '14 at 23:12
2 Answers
0
            
            
        It's the modulus operator - 45 divided by 12 = 3 and 9/12. In short, it would return 9.
It's a neat way of looping up to a certain count
eg the following will run on alternate rows...
for i in range[0,100]
    if i % 2 == 0:
        #Even row
    else:
        #Odd row
 
    
    
        Basic
        
- 26,321
- 24
- 115
- 201
0
            
            
        The % (read "mod" or "modulo") means divide by and return the remainder:
x = 8%3
The above expression says "divide 8 by 3 and compute the remainder. Assign this remainder to x"
 
    
    
        inspectorG4dget
        
- 110,290
- 27
- 149
- 241
