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 value
a = input ("Input the value of a:")
b = input ("Input value b:")
print
# Using the addition operator
c = a + b
print "Result% d +% d =% d"% (a, b, c)
# Reduction Operators
c = a - b
print "Result% d -% d =% d"% (a, b, c)
# Multiplication Operators
c = a * b
print "Result% d *% d =% d"% (a, b, c)
# Division Operators
c = a / b
print "Result% d /% d =% d"% (a, b, c)
# Time Remaining Operators
c = a% b
print "Result% d %%% d =% d"% (a, b, c)
# Rank Operators
c = a ** b
print "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 value
a = input ("Input the value of a:")
print
# ^
# | example assignment operator to fill in a value
print "Value a =% d"% a
# Try to add the value of a to the assignment operator
a += 5
# ^
# |
# example assignment operator to add up
# After the value of a plus 5, try to see the contents
print "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 value
a = input ("Input the value of a:")
# add 2
a += 2
# subtract 3
a -= 3
# 10 times
a *= 10
# divide by 4
a /= 4
# rank 10
a **= 10
# What is the value of a now?
print
print "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.py
a = input ("Input the value of a:")
b = input ("Input value b:")
# Is a the same as b?
c = a == b
print "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> b
print "Is% d >% d :% r"% (a, b, c)
# Is it a <= b?
c = a <= b
print "Is% d <=% d: % r"% (a, b, c)
# is a> = b?
c = a>= b
print "Is% d >=% d: % r"% (a, b, c)
# Is a! = b?
c = a!= b
print "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 ANDand Logika ORor Negation / reversenot
Example code:
a = True
b = False
# Logika AND
c = a and b
print "%r and %r = %r" % (a,b,c)
# Logika OR
c = a or b
print "%r or %r = %r" % (a,b,c)
# Logika Not
c = not a
print "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 AND
c = a & b
print "a & b =% s"% c
# OR operations
c = a | b
print "a | b =% s"% c
# XOR operation
c = a ^ b
print "a ^ b =% s"% c
# Operation Not
c = ~ a
print "~ a =% s"% c
# Left shift operation (exchange binary positions)
c = a << b
print "a << b =% s"% c
# Operation shift right (exchange binary position)
c = a >> b
print "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.
Example:
age = input ( "How old are you?")
I = "boy" if age <10 else "adult"
print I
Readability is not it?
Try also to fill in the value of the age variable with a value below 10 and note its output.
Another way to create a ternary operation can also use Tuple and List.
single = True
status = ("Married", "Single")[single]
print status