5. Operators

In this lesson you will learn about how different types of variables respond to different operators.

Integers and Floats

Arithmetic Operators

Integers and floats both interact with arithmetic operators like you would normally see them. For example 3 + 2.5 = 5.5. In addition to your normal arithmetic operators there is also modulus (often called mod) and floor division. You can think of modulus as the remainder. So if you have 10 mod 3, the answer is 1 since 3 times 3 is 9 so 10 - 9 is 1. Floor division is when you divide and round down. For example, 50 floor divided by 3 is 16 since it would normally be 16.667 when normally divided. Below is a list of all the arithmetic operators:

print(1 + 2)  # Addition, = 3
print(2 - 1)  # Subtraction, = 1
print(2 * 2)  # Multiplication, = 4
print(4 / 2)  # Division, = 2.0
print(2 ** 3) # Exponential, = 8
print(3 % 2)  # Modulus, = 1
print(5 // 2) # Floor division, = 2

Note how division becomes a float.

Assignment Operators

Floats and integers can also have operators when assigning the variable. Below are some examples:

x = 0
x += 1  # Same as x = x + 1
x -= 1  # Same as x = x - 1
x *= 2  # Same as x = x * 2
x /= 2  # Same as x = x / 2
x **= 2 # Sane as x = x ** 2
x %= 2  # Same as x = x % 2
x //= 2 # Same as x = x // 2

Strings

Concatenate

Concatenation is a way of combining two strings. If you recall from the last lesson we actually were able to concatenate a string and a number through casting. To concatenate, use +. For example:

print("My name is " + "Andrew") # "My name is Andrew"

Just like integers and floats, you can use assignment operators to add on a string to another string in assignment. Below is an example:

sentence = "My name is "
sentence += "Andrew" # Same as sentence = sentence + "Andrew"
print(sentence) # "My name is Andrew"

Slicing

Slicing for a string is when you obtain a substring of a string. For example a substring of "My name is Andrew" could be "Andrew". To slice a string use [, :, and ]. Inside of the brackets, we need to specify the range of indexes we want. An index is in a way like an id of that character. In most programming languages, indexes start at 0. For example the indexes of "Andrew" would be 0: "A", 1: "n", 2: "d", 3: "r", 4: "e", and 5: "w". The first number of the bracket is the first index and the second is the second index of the range we want. For example:

name = "Andrew"
print(name[0:3]) # "And"

Note how the upper limit of the range is 3 and not 2.

You can also get the character of a single index:

name = "Andrew"
print(name[5]) # "w"

You can also slice from the start and the end by not typing an index. For example:

sentence = "My name is Andrew"
print(sentence[:10]) # slice from start to 10, "My name is"
print(sentence[11:]) # slice from 11 to end, "Andrew"

You can also index with negative numbers. Think of it like starting from the end and counting backwards. Below is an example:

sentence = "My name is Andrew"
print(sentence[8:-3]) # slice from 8 to -3, "is And"
❮ PreviousNext ❯