# Operators

With every programming language we have our operators, and Python is no different. Operators in a programming language are used to vaguely describe 5 different areas: arithmetic, assignment, increment/decrement, comparison, logical. There isn’t really much need to explain all of them as I have previously in the other categories, which means we will only cover a few of them.

## Arithmetic Operators

Example
```print (3 + 4)
print (3 - 4)
print (3 * 4)
print (3 / 4)
print (3 % 2)
print (3 ** 4) # 3 to the fourth power
print (3 // 4) #floor division
```
Result

7
-1
12
0.75
1
81
0

See, they are pretty standard. I included how to do exponents because it’s a little funky, but other than that, they are fairly normal and work as expected. Don’t forget that you can use the + sign to concatenate strings. The addition, subtraction, multiplication, and division work just like expected. You might have not seen the modulus operator before (%). All the modulus operator does is to divide the left side by the right side and get the remainder. So, that remainder is what is returned and not the number of times the right number went into the left number The double * is just an easy way to provide exponents to Python. Finally, the floor division operator (//) just divides the number and rounds down.

## Assignment Operators

These are pretty identical to the previous languages also, but a refresher never hurt anyone. Example please!

Example
```a = 0
a+=2
print (a)
```
Result

2

Of course, you can do this with any of the previous arithmetic operators as an assignment operator followed by the = We just told Python to add 2 to the value of a without having to say something like a = a + 2. We are programmers, and we are proud to be called lazy!

### 49 thoughts on “Operators”

1. could you please let me know what is the symbol for not equal to

• != this sign represents not equal

2. Sudha not equal to can be written in multiple ways <> or != I prefer the != Method

3. this topic is simple but useful.

4. Great. But must say what ++ or — is absent in Python. And += and others is separate statement. Par exemple b = (a+=2) * (a*=4) is not a valid.

5. These tutorials are helping me on my way to becoming a software developer, thanks for the help!

6. Might be better if your modulo, floor division, and division examples all used the same divisor and dividend to clarify the difference between division, floor division, and modulo (6/4=1r2, 6%4=2, and 6//4=1)

7. where is the like button?

8. Is it correct that the result should br .75?
Should it be casted as a float to have that result?

9. wonderful

10. the line ‘ We are programmers, and we are proud to be called lazy!’ is something I never heard……all I have heard is ‘we are programmers , and people see us as alien’

11. Ever best understanding of operators now 🙂 Thanks Man

12. The "a+=2" expression increases the value of the original "a" by the numerical value.
If you set "a" at a value of "2", and then later need the value of "a" to be larger or smaller you would use the "aÂ±=2" (respective of what you need, add or subtract), to increase or decrease the earlier set value of "a".

13. This is quit different of other programming languages like C or VB. It seams to me Python is more easier.

14. Thanks for the pointer He3ek i understand the (a+=2) now

15. Nice shortcut bro

#lazyprogrammingftw

16. Lol I have no clue what you guys are talking about… Wish me luck plz!

17. I am new to python, but i think this lesson so easy.

18. Ok i was a little confused for a bit with the a+=2, but then i read it over twice.

19. This site help me a lot to understand python. It is very helpful for self learning.

20. what a great learning place

21. I have ZERO experience on python or any programming tool for that matter. I have done some simulators and i see a lot of options 😀

22. I have studied Python for like… a hundred times! And i also created like… 3 Games and 10 Programs! (Im not Bragging) But still im gonna study it over and over again to improve my skill so keep up the good work programmers!

23. Lovely!!

Some other examples:
a=3
a-=-1
print (a)

b=2
b+=-2
print (b)

c=2
c**=3
print (c)

d=6
d//=2
print (d)

24. Programmers ain’t lazy! rather we make stuff easy

b=39
b+=70
print(b)

Result 109

25. I can see this coming in handy later on.
a=4
a+=3
print(a)

Result 7

26. Could someone provide an example of when the assignment operator would come in handy? Why wouldn’t I just change my code to be what I need it to be? so instead of

a = 1
a+ = 2
print (a)

3

Why wouldn’t I just do a = 3?

Thanks!

27. Why is the result of 3 / 4 = 0?

28. @Wertb

If you look at the example the result of 3 / 4 = 0.75, this is the result of a normal division.
The result of 3 // 4 = 0 since this is a so called floor division which rounds down the answer.

If you are trying the code on your own system and get 0 as a result when you are executing 3 / 4 it means that you are using Python 2 instead of Python 3. You can get the correct result in Python 2 by using 3.0 / 4.0.

29. practice makes perfect

30. This is a very good tutorial.

31. blowout soon stalker

32. @Aeschere thanks that helped

33. print (3 / 4) gives 0 on my local setup since it is Python 2.7. But why is the code simulator also giving 0? code simulator should be using Python 3, isn’t it ? Thanks

34. print(1/2) is generating output 0.I think that’s not desired result

35. 3//4=0 its because it was rounded down. so 0.75 rounded down is 0

36. print float(4/3)

37. I just spent some time trying to learn Java (and gave up). It hurt my head with all the syntax to remember.

38. print (3 / 4) doesn’t work

39. print (3 / 4) doesn’t work

40. Where can I learn what the other operators do? I need more information I’m just starting out and I want to cover all my bases.

41. print(a/b) where a and b are both numerics doesn’t show the disire result.

42. print (50 % 20)

43. a = 0
a+=777
a-=3
a*=2
print (a)

44. print(3+4)
print(3-4)
print(3*4)
print (3/4)
print(3%4)
print(3**4)# 3 to the forth power
print (3//4)# flour division

a=0
a+=2
print(a)

45. print(3+4)
print(3-4)
print(3*4)
print (3/4)
print(3%4)
print(3**4)# 3 to the forth power
print (3//4)# flour division

a=0
a+=2
print(a)

46. 47. 48. 