Resources
Lesson Objectives
Learn about Operators:
String Operators
Numeric Operators
List Operators
Are you Ready?
Summary
Operators in Python
Let's talk about basic operators in Python.
Operator - is a special symbol or keyword that can be used to perform a specific operation on one or more variables. They are used in expressions to perform arithmetic, comparison or logical evaluations.
Experessions in Python
Expression - is a piece of code that produces a value when evaluated. It can be as simple as adding 2 numbers together (a+b
).
For example, we can define a few variables, and then calculate another one by creating an expression. In this case let's sum 2 numbers together.
Also note that you don't always have to assign your expression to a variable. You can use it directly as an argument in functions.
For example you will get same results here:
The only notable difference is that you will be able to reuse total variable later in the code if necessary. Otherwise it serves no purpose.
🔠String Operators
We are ready to dive into operators, and let's begin with String operators.
Firstly, you need to know that you can concatenate(fancy word for join in programming) 2 strings together.
1.Join Strings:
You can also define strings during expression. You don't have to always define variables if they are only used once.
2.Multiple Strings:
You can also multiply your strings. Then they will be repeated n-times.
I often use it to create a long line of dashes in the console when I print.
💡Notice that \n
is a special combination inside a string to create a New-Line.
3.Membership Operators:
Strings also have membership operators to check if certain value is part of another string. There are 2 keyword:
in
not in
Here is how to use them:
💡Note that lowercase and uppercase are 2 different characters.
4.Equality Operators:
Similar to membership operators, we can also check if strings are equal or not equal. And we use the following symbols:
==
- Equal!=
- Not Equal
⚠️ Do not confuse double(==) and single(=) equal signs. One is used to assign data or expression to a variable the other is used in logical checks.
Here is an example:
🔢Numeric Operators
Now let's look into Numeric Operators.
There are 2 main categories:
Arithmetic Operators
Comparison Operators.
1.Arithmetic Operators:
Let's start with Arithmetic. It's simple mathematical operations to add, subtract values. I won't go into detail, you all already know if well:
However, notice that there are 3 new ones:
//
- Floor division which rounds a value to a lower integer.%
- Mod, which returns the remainder after the division.**
- Power, which set the value in the power (2-square, 3-cube…)
The rest is just simple math.
2.Comparison Operators:
There are also comparison operators to create logical checks.
We will cover how to create logical statements in another lesson, for now just focus on the logical operators. The goal is to get True
or False
value.
Here is an example:
These are simple operators (More, Less, More Than, Equal, Not Equal).
Just make sure you don't confuse single(=) and double(==) equal signs.
For example:
📦List/Tuple Operators
Lastly, let's look into list and tuple operators and keep in mind that they use the same ones. So, I will only show examples using lists, and you can adjust them for tuples.
Lastly, let's also look into lists and we are going to begin with a basic one.
1.Join Lists (+)
You can take multiple lists and join them together using + symbol. Logical, right?
2.Membership Operators in Lists
Similar to strings we can also use Membership operators in lists using in
and not in
keywords. It works exactly the same:
3.Membership Operators in Lists
And we can also check if our lists are equal or not equal using ==
or !=
symbols.
Here is a comparison.
4.Membership with Nested Lists
Also, remember that you can create nested lists, and you can also check if certain list is a part of another list. But it only works if you have a list inside of a list.
If you are trying to check if all values of one list are inside of another list , then you would need to do it differently using loops. More on loops in another lesson…
HomeWork
Review the summary of this lesson and experiment with the operators in your own python code.
Try it out and see what you get.
⌨️ Happy Coding!