Operator is anything which acts on operands and produces a result.
a = b + 5
In the above statement, '=' and '+' are operators, 'a' and 'b' are variables and '5' is a constant. Together, 'a', 'b' '5' are called operands. '=' is called an assignment operator as it assignes 'b + 5' to 'a'. In computer science, the assignment is always from right to left i.e. in case of an assignment, the right expression is computed and is assigned to left expression. Expression is a combination of operators and operands.Operation on strings
'+' operator is used to add two or more strings.
a = "Electronics Revealed"
b = "The biggest online portal"
c = a + b
Now 'c' will contain 'InternshalaThe biggest internship portal'. A key thing to note here is that, with '+' operator, two strings are just concatenated (added) without any extra spaces being introduced.b = "The biggest online portal"
c = a + b
'*' operator is used to replicate the string.
d = "Revealed"
e = d*3
print (e)
The output for the above case will be 'RevealedRevealedRevealed'. '*' is very useful if string replication is needed.e = d*3
print (e)
Operation on numbers
The basic operations on numbers are addition, subtraction, multiplication and divison.
a = 6
b = 5
print (a + b)
print (a - b)
print (a * b)
print (a / b)
b = 5
print (a + b)
print (a - b)
print (a * b)
print (a / b)
11
1
30
1.2
Sometimes, we just want to get an integer value even after divison. In that case '//'' operator is used to divide two numbers, and return an integer value. This is called integer divison.1
30
1.2
NOTE: '//' operation is generally used in divison of two integers.
As stated before if a user enters a number, it is considered as a string. So, we need to change it from string to real number.
l = float(input('Enter a floating number : '))
m = int(input('Enter an integer : '))
If a real number needs to be converted to a string, str() function is used.m = int(input('Enter an integer : '))
a = 5.2
s = str(a)
To see what data type a variable is, type() function is used.s = str(a)
k = 5.27
print (type(k))
Note: All data types are not interconvertible. If in a string, there are some alphabets and you convert it to an integer, an error will be returned.print (type(k))
Operator Precedence Table
Operator | Description |
---|---|
lambda | Lambda Expression |
or | Boolean OR |
and | Boolean AND |
not x | Boolean NOT |
in, not in | Membership tests |
is, is not | Identity tests |
<, <=, >, >=, !=, == | Comparisons |
| | Bitwise OR |
^ | Bitwise XOR |
& | Bitwise AND |
<<, >> | Shifts |
+, - | Addition and subtraction |
*, /, % | Multiplication, Division and Remainder |
+x, -x | Positive, Negative |
~x | Bitwise NOT |
** | Exponentiation |
Operators with the same same precedence are listed in the same row in the above table. For example,
+
and -
have the same precedence.
Post a Comment