Basic Operators

This section explains how to use basic operators in Python.

Arithmetic Operators

Just as any other programming languages, the addition, subtraction, multiplication, and division operators can be used with numbers.
Try to predict what the answer will be. Does python follow order of operations?
number = 1 + 2 * 3 / 4.0
print number
Note: Copy and Paste Code into Compiler
 Open Compiler 

Another operator available is the modulo (%) operator, which returns the integer remainder of the division. dividend % divisor = remainder.
remainder = 11 % 3
print remainder
Note: Copy and Paste Code into Compiler
 Open Compiler 

Using two multiplication symbols makes a power relationship.
squared = 7 ** 2
cubed = 2 ** 3
print squared
print cubed
Note: Copy and Paste Code into Compiler
 Open Compiler 

Using Operators with Strings

Python supports concatenating strings using the addition operator:
helloworld = "hello" + " " + "world"
print helloworld
lotsofhellos = "hello" * 10
print lotsofhellos
Note: Copy and Paste Code into Compiler
 Open Compiler 

PreviousNext