## Python 3 Notes

[ HOME | LING 1330/2330 ]

## Tutorial 2: Arithmetic Operators

On this page: print(), arithmetic operators (+, -, %, *, /, **). Script vs. shell environment.

### Video Tutorial

 Python 3 Changes print(x,y) instead of print x, y In Python 3, "/" uniformly works as a float division operator. So, it always returns the float type: 10/3 returns 3.333333 instead of 3, 6/3 returns 2.0 instead of 2. No more confusion!

### Video Summary

• In a script environment, the "print" command must be explicitly given in order for the result to be printed out.
• Basic operations: "+" is used for addition, "-" for subtraction, "*" for multiplication, "/" for division, "%" for remainder.
• "^" is NOT used for exponents, it is a bitwise operator (NOTE: you don't need to know this). For exponents, use "**".
• The division operator "/" works as integer division if both inputs are integers. Therefore, 5/3 returns 1. You must supply a floating point number ('float') with decimal points if an answer other than a whole number is desired: 5.0/3 returns 1.666666. This changed in Python 3. See the note above.

 ```print(1 + 2) print(3 - 4) print(5 * 6) print(7 / 8) foo.py ```
 ```>>> print(1 + 3) 4 >>> print(3 - 4) -1 >>> print(5 * 6) 30 >>> print(7 / 8) 0 ```