Python‎ > ‎

operator

http://www.adilwaseem.com/

Introduction
Lutz(2013) states that python automatically loads the functions of the operators when function is called such as '+' performing the numeric functions, while concatenating the string values. The author discussed the polymorphism which implies that operator functions depends on the type of the object is operated on.

Comparative Operators
For comparison, Python uses comparative operator to evaluate the true or false statement, or execute the statement.
  • x==y, equal or not
  • x !=y, equal not
  • x<> y, equal or not
  • x >= y, x is greater or equal
  • x <= y, x is Smaller or equal
  • x>y, x greater than y
  • x< y, smaller than x
  • Example 
  • Look at this code:
  • >>> a=5
  • >>> b="Python Lake"
  • >>> a==b
  • False
Both a and b variable have different value that's why, Python returned with the false statement. In Python == operator used for the equal, while single = is used for assignment of the value.

Simple computation
For computation, Python use states operators and their functions are depends on the object and its function. 
  • x +y Addition
  • x-y Subtraction
  • x*y Multiplication
  • x/y Division
  • x//y Floor division
  • x**x Exponent
Examples
Here in this example python, uses the math operators to calculate the value of variable a, b,c.
 >>> a=44
>>> b=23
>>> c=56
>>> a+b
67

Similarly, subtraction and multiplication function can be used to evaluate the multiplication functions.
 >>> a+c
100
>>> c-b
33
>>> c/b
2.4347826086956523
>>> c*a
2464

Python does not deviate from by creating newer numeric operators but used the math convention to further the goals of computer programming. These are easy to operate and performs the functions according to object.

Concatenation
It joins two variable and placing into the strings format as requested in double or single quotes.

The example of concatenation as:
 >>> a="pyhton"
>>> b="lake"
>>> a+b
'pyhtonlake'
>>> 
>>> a+"lake"
'python lake
>>> "Python "+"Lake"
'Python Lake'
>>>

Assignment
These are assignment operators in Python and their operation depends on their functions.
  • = implies x=y
  • += implies x=x+y
  • -= implies x=x-y
  • *= implies x=x*y
  • /= implies x=x/y
  • **= implies x=x**y
  • //= implies x=/y*y
In this first example, the python add the value of the x to existing value of y and come up with 58 value.
 >>> 
>>> b=3
>>> b+=55
>>> 
>>> b
58

In this assignment operator, the python calculated the value by x-y.
 >>> a=4
>>> a-=5
>>> a
-1

The multiplication function is performed to variable which returns with the value 99.
 >>> 
>>> a=9
>>> a *=11
>>> a
99

Division function is also operated in similar function. Python have number of very nicely defined operator to make the programming more concise and control to reduce the programming complications.
 >>> 
>>> a=6
>>> a/=6
>>> 
>>> a
1.0

Comparison
Python Compare the values of the statement and determines whether these are true or not. The determination is predicted on the numeric values. There are number of comparison operators in Python. The sample of these are:
 x==y, equal or not
x !=y, equal not
x<> y, equal or not
x >= y, x is greater or equal
x <= y, x is Smaller or equal
x>y, x greater than y
x< y, smaller than x

In example 1, the comparative operator returns with the false results after computing the numeric values assigned to each variable.
 >>> a=21
>>> b=32
>>> c=3
>>> d=66
>>> (a+c) > (b+d)
False

In first case, Python returns with false result because two lists are different, while second true.
 >>> [1,2,3,5] == [1,2,3,4]
False
>>> [1,2,3,4,5] is [1,2,3,4,5]
True

In example 3, Python returns with false returns because the variable a value is different. Python has comparative operator for the purpose of evaluating the validity of the values. These return with False or True value or execute the statement according to the object function.
 a="Python Lake"
b=input("Enter your ID: ")
if a==b:
print("You're welcome")
else:
print("You're not authorized")
>>> 
Enter your ID: Adil Waseem
You're not authorized


Subpages (48): View All
Comments