Python 3 – Basic Operators
Python relies heavily on the use of operators to execute common tasks, whether it is basic arithmetic or manipulating text. There are a variety of different operators to choose from, making Python a versatile and powerful programming language. In this article, we will explore some of the basic operators in Python 3 and provide examples of how to use them.
Arithmetic Operators
The arithmetic operators in Python 3 allow you to perform basic mathematical calculations. The standard operators include:
+
addition-
subtraction*
multiplication/
division%
modulo**
exponentiation
Addition
The addition operator allows us to add two values together. Here is an example:
a = 5
b = 10
c = a + b
print(c)
In this example, we assign the value of 5 to the variable a
and the value of 10 to the variable b
. We then add the values of a
and b
together and assign the result to the variable c
. Finally, we print the value of c
to the console which outputs 15
.
Subtraction
The subtraction operator allows us to subtract one value from another. Here is an example:
a = 10
b = 5
c = a - b
print(c)
In this example, we assign the value of 10 to the variable a
and the value of 5 to the variable b
. We then subtract the value of b
from a
and assign the result to the variable c
. Finally, we print the value of c
to the console which outputs 5
.
Multiplication
The multiplication operator allows us to multiply two values together. Here is an example:
a = 5
b = 10
c = a * b
print(c)
In this example, we assign the value of 5 to the variable a
and the value of 10 to the variable b
. We then multiply the values of a
and b
together and assign the result to the variable c
. Finally, we print the value of c
to the console which outputs 50
.
Division
The division operator allows us to divide one value by another. Here is an example:
a = 10
b = 5
c = a / b
print(c)
In this example, we assign the value of 10 to the variable a
and the value of 5 to the variable b
. We then divide the value of a
by b
and assign the result to the variable c
. Finally, we print the value of c
to the console which outputs 2.0
.
Modulo
The modulo operator returns the remainder after division. Here is an example:
a = 10
b = 3
c = a % b
print(c)
In this example, we assign the value of 10 to the variable a
and the value of 3 to the variable b
. We then divide the value of a
by b
and assign the remainder to the variable c
. Finally, we print the value of c
to the console which outputs 1
.
Exponentiation
The exponentiation operator allows us to raise a value to a certain power. Here is an example:
a = 2
b = 3
c = a ** b
print(c)
In this example, we assign the value of 2 to the variable a
and the value of 3 to the variable b
. We then raise the value of a
to the power of b
and assign the result to the variable c
. Finally, we print the value of c
to the console which outputs 8
.
Comparison Operators
Comparison operators are used in Python to compare two values. They return a Boolean value of either True
or False
. The standard comparison operators include:
>
greater than<
less than==
equal to!=
not equal to>=
greater than or equal to<=
less than or equal to
Here are some examples of comparison operators in use:
a = 5
b = 10
print(a > b) # False
print(a < b) # True
print(a == b) # False
print(a != b) # True
print(a >= b) # False
print(a <= b) # True
In the first example, we use the greater than operator to compare the values of a
and b
. Since a
is not greater than b
, the result is False
. We use the same logic for the remaining examples, except this time we get True
or False
depending on the comparison.
Logical Operators
Logical operators are used in Python to evaluate multiple conditions. The standard logical operators include:
and
logical ANDor
logical ORnot
logical NOT
Here are some examples of logical operators in use:
a = 5
b = 10
c = 20
print(a < b and b < c) # True
print(a < b or b > c) # True
print(not a < b) # False
In the first example, we use the logical AND operator to determine if both a
is less than b
and b
is less than c
. Since both conditions are true, the result is True
. In the second example, we use the logical OR operator to determine if either a
is less than b
or b
is greater than c
. Since at least one of the conditions is true, the result is True
. In the third example, we use the logical NOT operator to invert the result of the comparison a
is less than b
, which results in False
.
Assignment Operators
Assignment operators are used in Python to assign a value to a variable. The standard assignment operators include:
=
simple assignment+=
add and assign-=
subtract and assign*=
multiply and assign/=
divide and assign
Here are some examples of assignment operators in use:
a = 5
a += 2
print(a) # 7
b = 10
b -= 3
print(b) # 7
c = 5
c *= 3
print(c) # 15
d = 20
d /= 2
print(d) # 10.0
In the first example, we assign the value of 5 to the variable a
. We then use the +=
operator to add 2 to the value of a
and assign the result back to a
. Finally, we print the value of a
which outputs 7
. We use the same logic for the remaining examples, except we use a different assignment operator each time.
Identity Operators
Identity operators are used in Python to compare the memory locations of two objects. They return a Boolean value of either True
or False
. The identity operators include:
is
returns true if two variables are the same objectis not
returns true if two variables are not the same object
Here are some examples of identity operators in use:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True
print(a is not b) # False
print(a is c) # False
print(a is not c) # True
In the first example, we assign a list to the variable a
. We then assign a
to the variable b
. Since a
and b
refer to the same list object, the result of a is b
is True
. In the second example, we use the is not
operator to demonstrate that a
and b
are not different objects. In the third example, we assign a new list to the variable c
. Since a
and c
refer to different list objects with the same values, the result of a is c
is False
. In the fourth example, we use the is not
operator to demonstrate that a
and c
are different objects.
Membership Operators
Membership operators are used in Python to check if a value or variable is a member of a collection. The membership operators include:
in
returns true if a value is found in the collectionnot in
returns true if a value is not found in the collection
Here are some examples of membership operators in use:
a = [1, 2, 3, 4, 5]
print(2 in a) # True
print(6 in a) # False
print(6 not in a) # True
In the first example, we check if the value of 2
is in the list a
. Since it is, the result is True
. In the second example, we check if the value of 6
is in the list a
. Since it is not, the result is False
. In the third example, we use the not in
operator to invert the result of the previous example, which results in True
.
Conclusion
In this article, we’ve covered the basic operators in Python 3, including arithmetic, comparison, logical, assignment, identity, and membership operators. Experiment with these operators to get a feel for their intended use, and take advantage of Python’s flexibility and power in your own programming projects. Happy coding!