Operators -
Operator
|
Description
|
Example
|
+, -
|
Addition, Subtraction
|
10 -3
|
*, /, %
|
Multiplication, Division,
Modulo
|
27 % 7
Result: 6 |
//
|
Truncation Division (also
known as floordivision or floor division)
The result of this division is the integral part of the result, i.e. the fractional part is truncated, if there is any. If both the divident and the divisor are integers, the result will be also an integer. If either the divident or the divisor is a float, the result will be the truncated result as a float. |
>>> 10 // 3
3 >>> 10.0 // 3 3.0 >>> |
+x, -x
|
Unary minus and Unary plus
(Algebraic signs)
|
-3
|
~x
|
Bitwise negation
|
~3 - 4
Result: -8 |
**
|
Exponentiation
|
10 ** 3
Result: 1000 |
or, and, not
|
Boolean Or, Boolean And,
Boolean Not
|
(a or b) and c
|
in
|
"Element of"
|
1 in [3, 2, 1]
|
<, <=, >, >=, !=,
==
|
The usual comparison
operators
|
2 <= 3
|
|, &, ^
|
Bitwise Or, Bitwise And,
Bitwise XOR
|
6 ^ 3
|
<<, >>
|
Shift Operators
|
6 << 3
|
Important Operators –
Before starting with Data Types ,it is necessary to under stand a Couple of
Operators used in Python.
Member ship operators – The
Membership operators are the ones that check whether a String or Number is
available in a Data Type or any other Type. This “IN” operator is one of the
most used one as
>>> str1 ="i have
a Lazy Dog"
>>> str1
'i have a Lazy Dog'
>>> "dog" in
str1
False
>>> "Dog" in
str1
True
>>> list = [1,2,3,4,5]
>>> '1' in list
False
>>> 2 in list
True
As seen above the “IN” Operator
can be used to find if an element exists in a Type
Identity Operators – The
Identity operators can be used to check the memory location and tell if both
are equal. The “IS” Operator is used to perform the operation of checking. This
checks whether the values are same as well are the data types.
>>> i = 10
>>> b = 20
>>> if i is b:
... print "hai"
... else:
... print "bye"
...
bye
>>> i = 10
>>> b = 10
>>> if i is b:
... print "hai"
... else:
... print "Bye"
...
Hai
There is another Variables called
“Not” which can be used to check whether they are not available or not
If not a :
Print “not available”
Docstring - a Doc String can be considered as a
Documentation String that provide a convenient way of associating documentation
with python modules , functions , classes and methods. An object's docsting is
defined by including a string constant as the first statement in the object's
definition.
>>> def myFucntion():
... """ This is My Sample
Function Doc String"""
... print "this is Sample Function"
...
>>> myFucntion()
this is Sample Function
No comments :
Post a Comment