Arithmetic Operators in Python
Arithmetic operators are symbols used in arithmetic operations such as addition, subtraction, division, and logical operations performed on variables. There are several types of operators.
Normal arithmetic operations
The most common arithmetic operations that we use in our daily life, such as addition, subtraction, multiplication, division, the remainder of division, and others Among these operations:
- Addition We use (+).
- Subtraction We use (-).
- Multiplication We use (*).
- Division We use (/).
- For the remainder of the integer division Floor Division we use (//).
- Modulus remainder We use a sign (%).
- Force Exponent We use (**).
Example 1 combines all previous operations
>>> a = 10
>>> d = 5
>>> a + d
15
>>> a - d
5
>>> a * d
50
>>> a / d
2.0
>>> a // d
2
>>> d % a
5
>>> a ** d
100000
As we have noted, we used all the arithmetic operations in the example and after each operation it gives us the result.
Important arithmetic operations
The important operations (Assignment) are some slightly complex operations such as more or equal to and subtract or equal to ... etc., and among these operations:
- The Add and operator we use the sign (+=).
- The Subtract and operator we use the sign (=+).
- The parameter Multiply and we use the sign (=-).
- The Divide and operator we use the sign (=/).
- Modulus and we use the sign (=%).
- The Floor Division parameter We use the sign (=//).
- The operator Exponent and we use the sign (=**).
Example 2 combines all previous operations
>>> a = 10
>>> a
10
>>> a -= 1
>>> a
9
>>> a *= 2
>>> a
18
>>> a //= 2
>>> a
9
>>> a /= 2
>>> a
4.5
>>> a //= 2
>>> a
2.0
>>> a %= 3
>>> a
2.0
As we have noted, we have assigned the value of the variable by adding an = sign instead of writing it in each method.
Comparisons Operations
Comparison operations in Python and it is like comparing two things and we use equality and inequality and greater and less than and the output is two values and if the comparison is true it is true and if it is not true it prints false Among these operations:
- The operand is equal We use a sign (==).
- The operand is not equal We use a sign (!=).
- The parameter is greater than greater then we use a sign (<).
- The parameter is less than less than we use a sign (>).
- The parameter greater than or equal we use a sign (=<).
- The parameter is less than or equal We use a sign (=>).
Example 3 combines all previous operations
>>> ("programmer" == "programmer")
True
>>> ("programmer" == "tech")
False
>>> a = 10
>>> d = 6
>>> a < d
False
>>> a > d
True
>>> a >= d
True
>>> a <= d
False
>>> a != d
True
I think that everything is clear and needs no explanation, if the condition is true it prints true and if it doesn't it prints false.
Membership Operations
It means to ask the program whether this element is present or not. If it exists, it prints true and if it does not exist, it prints false by in means in and not in does not exist.
Example 4
>>> name = "rima"
>>> ("R" in name)
False
>>> ("m" in name)
True
>>> ("r" in name)
True
>>> ("d" not in name)
True
>>> ("r" not in name)
False
In the previous example, we were asking the program whether the character ## is present in the value of the variable name, and if it is present, it gives us true, and if it does not exist, it gives us false.
Data types in Python
There are five types of data in Python that we will mention because we will use them frequently in any advanced or novice program.
- Integer.
- String.
- List.
- Tuple.
- Dictionary.
Example 5
>>> 10 #integer
10
>>> "name" #String
'name'
>>> (1,2,3,4) #tuple
(1, 2, 3, 4)
>>> [1,2,3,4] #list
[1, 2, 3, 4]
>>> {"name":"databasehk"} #Dectionary
{'name': 'databasehk'}
In this example, we took five examples, and each example is a data type that we mentioned above in order.