Operators in Python - ProgrammerTech
Language:

Search

Operators in Python

  • Share this:
Operators in Python

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:

  1. Addition We use (+).
  2. Subtraction We use (-).
  3. Multiplication We use (*).
  4. Division We use (/).
  5. For the remainder of the integer division Floor Division we use (//).
  6. Modulus remainder We use a sign (%).
  7. 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:

  1. The Add and operator we use the sign (+=).
  2. The Subtract and operator we use the sign (=+).
  3. The parameter Multiply and we use the sign (=-).
  4. The Divide and operator we use the sign (=/).
  5. Modulus and we use the sign (=%).
  6. The Floor Division parameter We use the sign (=//).
  7. 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:

  1. The operand is equal We use a sign (==).
  2. The operand is not equal We use a sign (!=).
  3. The parameter is greater than greater then we use a sign (<).
  4. The parameter is less than less than we use a sign (>).
  5. The parameter greater than or equal we use a sign (=<).
  6. 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.

  1. Integer.
  2. String.
  3. List.
  4. Tuple.
  5. 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.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com