# Learn Types of Operators in Python

Once you know the variables and data types in Python, then you will be acquainted with the Operator.

What is a carrier?

Here are the symbols that are used to perform certain operations.

There are six types of operators in every programming shall ye know:

Arithmetic operators
Operator Appellant / Relation
Operator Assignment
operator for Logic
operators Bitwise
operators Ternary

Let's discuss everything.

#### 1. Arithmetic

Operator arithmetic operator for including in the most frequently used in programming.

Operator for arithmetic consists of:

 Operator Symbol addition + Reduction – Multiplication * division / Remaining Share % promotion **
Let's try the python program:

Example code:
``# file: operator_aritmatika.py# Take input to fill in the valuea = input ("Input the value of a:")b = input ("Input value b:")print# Using the addition operatorc = a + bprint "Result% d +% d =% d"% (a, b, c)# Reduction Operatorsc = a - bprint "Result% d -% d =% d"% (a, b, c)# Multiplication Operatorsc = a * bprint "Result% d *% d =% d"% (a, b, c)# Division Operatorsc = a / bprint "Result% d /% d =% d"% (a, b, c)# Time Remaining Operatorsc = a% bprint "Result% d %%% d =% d"% (a, b, c)# Rank Operatorsc = a ** bprint "Result% d **% d =% d"% (a, b, c)``

The result:
In the python source code above, you use string formatting to print the results of each Operator.

Operator% only used for formatting strings, these operators are also used to calculate the rest of the operation.

For example: 5% 2, then the result is 1. For the remainder of the quotient between 5 by 2 is 1.

#### 2. Assignment Operator

As the name suggests, this operator is used to assign tasks to the variable.

For example:

age = 18

Then the variable age you had given the task to save the number 18.

In addition to storing or charging value, there is also a summing, subtracting, multiplication, division, etc.

More can be seen in the following table.
 Operator Symbol filling = addition += Reduction -= Multiplication *= division /= Remaining Share %=

For more details, let you try the following example in python program:
``# file: operator_penugasan.py# Take input to fill in the valuea = input ("Input the value of a:")print# ^# | example assignment operator to fill in a valueprint "Value a =% d"% a# Try to add the value of a to the assignment operatora += 5# ^# |# example assignment operator to add up# After the value of a plus 5, try to see the contentsprint "Value after added 5:"print "a =% d"% a``

The result:

At first you fill a variable value by 4. Then do a summation or plus 5.

a += 5
The sum is the same as saying something like this:

a = a + 5
That is, you fill out a variable value with a previous value, then plus 5.

Understand…?

Well, if you already know please try again for another operator example below:
``# Take input to fill in the valuea = input ("Input the value of a:")# add 2a += 2# subtract 3a -= 3# 10 timesa *= 10# divide by 4a /= 4# rank 10a **= 10# What is the value of a now?printprint "Value a is% d"% a``

#### 3. Comparison Operator

This operator is used to compare two values. This operator is also known as relational operators and is often used to create a logic or condition.

Opertor consists of:
 operator Symbol Symbol Greater than > Smaller < Equal to == Not Equal != Larger Equals >= Smaller Equals <=

Example:

a = 9
b = 5
c = a < b

Are the contents of the variable c?

Its content is False, because the value of 9 is less than 5 (9 <5) is one of (False).

For more details, let you try example the python program below:
``# file: operator_comparator.pya = input ("Input the value of a:")b = input ("Input value b:")# Is a the same as b?c = a == bprint "Is% d ==% d:% r"% (a, b, c)# is it a c = a print "Is% d <% d:% r"% (a, b, c)# what is a> b?c = a> bprint "Is% d >% d :% r"% (a, b, c)# Is it a <= b?c = a <= bprint "Is% d <=% d: % r"% (a, b, c)# is a> = b?c = a>= bprint "Is% d >=% d: % r"% (a, b, c)# Is a! = b?c = a!= bprint "Is% d !=% d: % r"% (a, b, c)``
The results are to input a 4 and inputs b 8:

#### 4. Logical Operators

Logical operators are used to create a logic operation, such as AND, OR, and NOT.

Logic operator comprising:
 Name Symbols in Python Logika AND and Logika OR or Negation / reverse not

Example code:
``a = Trueb = False# Logika ANDc = a and bprint "%r and %r = %r" % (a,b,c)# Logika ORc = a or bprint "%r or %r = %r" % (a,b,c)# Logika Notc = not aprint "not %r  = %r" % (a,c)``

The result:

#### 5. Operator Bitwise

Bitwise operator is an operator to perform an operation based on bit / binary.

This operator consists of:
 name symbol Symbols in Python AND & OR | XOR ^ Negation / reverse ~ Left Shift << Right Shift >>

The results of operations of these carriers is rather difficult to understand, if you do not understand binary operation.

Let you try to understand with a simple example:

For example, you have a variable a = 60 and b = 13.

When made in binary form, will be like this:

a = 00111100
b = 00001101

Then, the bitwise operation

AND operation

a = 00111100
b = 00001101
a & b = 00001100

OR operation

a = 00111100
b = 00001101
a | b = 00111101

XOR operation

a = 00111100
b = 00001101
a ^ b = 00110001

Operating NOT (Negation / reverse)

a = 00111100
~a  = 11000011

The concept is similar to the operator for Logic. However, Bitwise used for binary.

Let's try the python program the following:
``a = input ("Input value a:")b = input ("Enter value b:")print# Operation ANDc = a & bprint "a & b =% s"% c# OR operationsc = a | bprint "a | b =% s"% c# XOR operationc = a ^ bprint "a ^ b =% s"% c# Operation Notc = ~ aprint "~ a =% s"% c# Left shift operation (exchange binary positions)c = a << bprint "a << b =% s"% c# Operation shift right (exchange binary position)c = a >> bprint "a >> b =% s"% c``

The results are to input a 4, and B 8:

#### 6. Operator Ternary

Also known as the ternary operator conditional operator, because it is used to create an expression branch conditions such as IF / ELSE.

Ternary operator does not exist in Python, but Python has no way to replace this operator.

In other programming languages ​​ternary operator use a question mark (?) And colon (:).

conditions? :

Example:

I = (age <10)? “Boy”, “adult”
In Python different forms, namely use IF / ELSE in a row.

if condition else
Example:

age = input ( “How old are you?”)
I = “boy” if age <10 else "adult"
print I Next post Revealed! This is the Release Date for Genshin Impact – Pre-Register Now