Operators are symbols that perform operations to variables and values.
Contents
Types of Operators
Arithmetic Operators
They are used to perform math in Python. Some of the basic ones are-
+ : Adds two integers/combines two strings together (concatenation)
num = 1 + 1
print(num)
2
– : Subtracts two integers
* : Multiplies two integers
/ : Divides two integers
% (modulo) : Finds the remainder of two integers; ex. 11%5 (modulo is 1)
num_1 = 11
num_2 = 5
remainder = num_1 % num_2
print(remainder)
1
Assignment Operators
Assignment Operators assign values to variables. Some basic ones are-
= : Assigns a value to a variable; ex. fruit = banana (banana is the value of fruit)
+= : The value added with itself, then added with another value; ex. x += 5 is the same as x = x + 5
x = 1
x += 5
print(x)
6
-= : The value subtracted with itself, then subtracted with another value; ex. x -= 5 is the same as x = x – 5.
Comparison Operators
Comparison Operators compare between two values. You can see them the most in loops and if/else statements. They are-
== : To check if two values are equal; not the same as ‘=’; set conditions to True
!= : To check if two values are not equal; sets conditions to False
x = 1
y = 2
print(x,y)
if x != y:
print("these numbers aren't the same")
1 2
these numbers aren't the same
> : Greater than
< : Less than
>= : Greater than or equal to
<= : Less than to equal to
Logical Operators
Logical operators combine conditional statements. You can also see them in loops and if/else staements-
and : Checks if both statements are true
x = 2
print(x)
if x > 1 and x < 3:
print("this number is greater than 1 and lower than 3")
2
this number is greater than 1 and lower than 3
or : Checks if only one statement is true
Identity Operators
Identity operators are used to compare the objects to see if they are actually the same object-
is : Checks if both objects are the same; can be a substitute to {variable} == {variable_2}
x = 1
y = 1
print(x,y)
if x is y:
print("these numbers are the same")
1 1
these numbers are the same
is not : Checks if both objects aren’t the same; can be a substitute to {variable} != {variable_2}
Membership Operators (what kind of names are these????)
Membership operators are used to test if a value is presented in an sequence (tuples, lists dictionaries). commonly seen in For loops.
in : Checks if a value is present in the sequence
not in : Checks if a value is not present in the sequence
Misc. But Still Important
Comment out: Commenting out code with a hash tag (#) tells Python to ignore the said line of code. You can set a comment by typing a hash tag (#) at the front of the code. For example-
#print("This is comment a!")
print("This is comment b!")
This is comment a!
Newline (\n) : In Python, “\n” is used to print something on a new line. You can type it into a string, and all characters after “\n” will be printed onto a new line.
print("This is comment a!\nThis is comment b!")
This is comment a!
This is comment b!
Conclusion
That’s all of the important operators! I hope you found this guide useful.
