1.5 - Operations
You can do operations on numbers in Python. Some basic operations like addition, subtraction, multiplication and division can be done using special symbols.
a + b
additiona - b
subtractiona * b
multiplicationa / b
division
You can also use the pow
function to get the exponent.
print(1 + 5)
print(5 - 3)
print(5 * 5)
print(pow(5, 5)) # pow(5, 5) is 5*5 or 25
print(6 / 2)
print(2 * (5 + 5))
Just like in math outside of computers, order of operations still applies (BODMAS, POMDAS, etc.). 2 * (5 + 5)
does not equal 2 * 5 + 5
.
Operations to strings
You can use some operators on strings too! The +
operator can join strings and the *
operator can repeat text.
In programming, joining strings is commonly referred to as string concatenation.
This example will print out "Hello John!" and "aaaaa"
name = "John"
print("Hello " + name + "!") # Hello John!
print("a" * 5) # aaaaa
You might notice that the string "Hello "
has a space after it. Without the space, the program would show HelloJohn
.
There was meant to be an ad here. Support us by turning off your ad blocker or donating.