Skip to Content

Advertisement

You (Level 0)
0% Complete
Last Badge Earned
None

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!

For a more tangible and better look into the Python language, consider reading the following book. It's an excellent read.

Your code will execute in this window.



Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

Advertisement

If you enjoyed this resource, support me by sharing this page with others.

Comment on

Show Comments
  • User

    HDNua Dec. 14, 2016, 11:03 p.m.

    code simulator results out "print (3/4)" as 0, not 0.25. There might be error...

  • User

    Myshadow Oct. 24, 2015, 9:40 p.m.

    You showed us how to round down in that example, is there a way for us to round up?

  • User

    Simon June 13, 2015, 9:26 p.m.

    What are these comments about? should there be some questions on this page?

  • User

    PPZ June 3, 2015, 10:53 p.m.

    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)

  • User

    PPZ June 3, 2015, 10:53 p.m.

    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)

  • User

    John Snow May 12, 2015, 3:06 a.m.

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

  • User

    onewton April 30, 2015, 2:44 p.m.

    print (50 % 20)

  • User

    Carl April 26, 2015, 11:53 a.m.

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

  • User

    SB March 19, 2015, 1:49 a.m.

    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.

  • User

    dan March 17, 2015, 12:24 p.m.

    print (3 / 4) doesn't work

  • User

    dan March 17, 2015, 12:24 p.m.

    print (3 / 4) doesn't work

  • User

    Barking_Mad March 6, 2015, 10:13 a.m.

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

  • User

    R D S Feb. 10, 2015, 3:01 a.m.

    print float(4/3)

  • User

    Kreel Jan. 8, 2015, 7:51 p.m.

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

  • User

    Ravi Kumar Jan. 5, 2015, 3:21 p.m.

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

  • User

    charioteer Nov. 24, 2014, 8:47 p.m.

    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

  • User

    NoFawkesToGive Nov. 21, 2014, 1:13 a.m.

    @Aeschere thanks that helped

  • User

    cheeki_breeki Nov. 15, 2014, 2:02 p.m.

    blowout soon stalker

  • User

    Halley Nov. 15, 2014, 10:02 a.m.

    This is a very good tutorial.

  • User

    nawab Nov. 4, 2014, 10:12 p.m.

    practice makes perfect

  • User

    Aeschere Oct. 31, 2014, 8:24 p.m.

    @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.

  • User

    Wertb Oct. 29, 2014, 7:40 p.m.

    Why is the result of 3 / 4 = 0?

  • User

    zmoffster Oct. 17, 2014, 9:50 p.m.

    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!

  • User

    zmoffster Oct. 17, 2014, 9:49 p.m.

    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!

  • User

    dunkleman July 13, 2014, 2:09 p.m.

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

  • User

    Michael Opinde July 8, 2014, 4:55 a.m.

    Programmers ain't lazy! rather we make stuff easy b=39 b+=70 print(b) Result 109

  • User

    Clau Lacatus June 28, 2014, 2:14 a.m.

    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)

  • User

    Jacobzone June 22, 2014, 6:28 p.m.

    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!

  • User

    Henk Velthoven June 5, 2014, 5:15 a.m.

    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 :D

  • User

    javed May 27, 2014, 8 a.m.

    what a great learning place

  • User

    outwitester May 24, 2014, 8:40 a.m.

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

  • User

    Josiah L May 9, 2014, 10:55 a.m.

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

  • User

    flussadl May 1, 2014, 8:15 a.m.

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

  • User

    MaximusBo April 23, 2014, 7:34 p.m.

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

  • User

    Fowled_Out April 6, 2014, 5:23 p.m.

    Nice shortcut bro #lazyprogrammingftw

  • User

    rousbel_villar Feb. 26, 2014, 6:56 a.m.

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

  • User

    Lotfi GHAZOUANI Feb. 25, 2014, 1:17 p.m.

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

  • User

    He3eK Feb. 13, 2014, 11:04 a.m.

    A reply to Milos comment; 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".

  • User

    Aqdas Jan. 3, 2014, 4:43 a.m.

    Ever best understanding of operators now :-) Thanks Man

  • User

    Nazrin chronicle Dec. 19, 2013, 11:31 p.m.

    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'

  • User

    Ju Allego Nov. 23, 2013, 10:33 p.m.

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

  • User

    Space Cook Nov. 11, 2013, 6:30 a.m.

    where is the like button?

  • User

    Christopher Dahle Oct. 6, 2013, 5:18 p.m.

    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)

  • User

    Decca Oct. 3, 2013, 12:26 a.m.

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

  • User

    Igor Mazurok Sept. 23, 2013, 11:55 a.m.

    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.

  • User

    Onofre Aquino Aug. 28, 2013, 8:28 a.m.

    this topic is simple but useful.

  • User

    Ryan Medernach May 16, 2013, 10:12 a.m.

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

  • User

    Sudha May 10, 2013, 7:09 a.m.

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



Advertisement