payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Operators   in Category: Python   by amit

🕙 Posted on 2023-06-29 at 21:19:14     Read in Hindi ...


Some Basic Operations

    Python is loose data-type (execution ready) programming language. Herein below, you will learn about some basic mathematical operations, which can be performed on two variable names, assigned with numbers, or directly on those numbers.

print( 3 + 4 )   # Outputs 7 (ADDITION)

print( 3 - 4 )   # Outputs -1 (SUBTRA­CTION)

print( 3 * 4 )   # Outputs 12 (MULTIPLI­CATION)

print( 3 / 4 )   # Outputs 0.75 (DIVISION)

print( 3 // 4 )   # Outputs 0 (FLOOR DIVISION)

print( 4 // 3 )   # Outputs 1 (Floor Division outputs the integer value of result, that is 1.3333333333333333 is changed to 1)

print( 5 // 2 )   # Outputs 2 (FLOOR DIVISION)

print( 5 % 2 )   # Outputs 1 (MODULUS or remainder)

print( 7 % 4 )   # Outputs 3 (MODULUS or remainder)

print( 3 ** 4 )   # 3*3*3*3 = 81 (POWER OF or exponential, that is, raising to)

print( 4 ** (1/2) )   # Outputs 2.0 (SQUARE ROOT: always enclose the second operand in parentheses, that is (1/2) )

print( 2 ** (1/2) )   # Outputs 1.4142135623730951 (SQUARE-ROOT of 2)

print( 4 ** (1/4) )   # Outputs 1.4142135623730951 (QUADRANT-ROOT of 4, that is, first squre root of 4 is obtained and then square-root of result, that is 2 is OUTPUT.)

print( 2 ** (1/3) )   # Outputs 1.2599210498948732 (CUBE-ROOT of 2)

    You have seen these arithe­matical operations on integer and floating point (decimal) numbers. When any integer is added, subtracted, or multiplied to an integer, then result will of integer data-type. However, when an integer is divided by another integer, then result will be of float data-type.

x = 4/2
print( x, type(x) ) # Outputs 2.0 <class 'float'>

    Square-root, cube-root, quadrant-root, etc. work opposite of operation on some number (results), which is square (3*3 = 9), cube (3*3*3 = 27), and fourth-power that is 3*3*3*3 = 81, and so on... Then square-root of 9 is 3, cube-root of 27 is 3, and quadrant-root of 81 is 3, and so on...

    Raising powers on 1 that is, square, cube, square-root, cube-root, etc. of 1 is always 1. Therefore, square-root, cube-root, quadrant-root, etc. of 2 can never be equal to 1, no matter how small its value is. In advanced programming techniques in Python and other programming languages, you should have clear concept, and good knowledge of mathematics.

    Arithematic operation are executed from left to right (associativity), when two or more operators have same precedence. But, when two operands are enclosed within parentheses, then both are operated first, and then remaining expression is executed.

print( 3 / 2 * 4 )   # Outputs (1.5*4) = 6.0

print( 3 / (2 * 4) )   # Outputs 0.375

Assignment Operators

    In previous pages, you have seen that some string literal is assigned to variable names. = is assignment operator in many programming languages, including Python. In general mathematics, we have learned that 2 + 3 = 5 (literally say that, 2 plus 3 is equal to 5). However, equal to is the comparison operator in programming languages, and it is ==. Herein below, you will learn various assignment operators.

x = 5 # variable x is assigned with integer data-type
print( x ) # outputs the value assigned to variable, x

name = "Amit" # variable 'name' is assigned with string data-type
print( name ) # outputs the value assigned to variable, name

    Assignment operators work only on variable name on left side, and the literal value or other variable/function/expression value on right operand is given to that variable on left operand. Assignment operators have associativity from right to left. Let's see above examples in context with assignment operators.

a = 3             # Each statement must be written in new line.
a += 4          # addition assignement (a = a + 4)
print( a )     # Outputs 7

b = 3
b -= 4          # subtraction assignement (b = b - 4)
print( b )     # Outputs -1

c = 3
c *= 4          # multiplication assignement (c = c * 4)
print( c )     # Outputs 12

d = 3
d /= 4          # division assignement (d = d / 4)
print( d )     # Outputs 0.75

e = 3
e //= 4        # floor division assignement (e = e // 4)
print( e )     # Outputs 0

f = 3
f %= 4         # modulus assignement (f = f % 4)
print( f )     # Outputs 3

g = 3
g **= 4         # exponential assignement (g = g ** 4)
print( g )     # Outputs 81

h = 3
h **= 0.5     # exponential assignement (h = h ** (1/2))
print( h )     # Outputs 1.7320508075688772

Bitwise Assignment Operators

    & (bitwise and), | (bitwise or), ^ (bitwise xor), ~ (bitwise not), << (bitwise shift left), >> (bitwise shift right) are bitwise operators, (explained in PHP context − see table) and have same functionalities in Python also.

i = 6
i &= 3     # bitwise and assignement (i = i & 3)
print( i )     # Outputs 2

j = 6
j |= 3     # bitwise or assignement (j = j | 3)
print( j )     # Outputs 7

k = 6
k ^= 3     # bitwise xor assignement (k = k ^ 3)
print( k )     # Outputs 5

l = 6
m = ~ l     # bitwise not operator works on single operand
print( m )     # Outputs -7

    In next page, you will learn about comparison operators, logical operators, membership operators, identity operators and control structures.


Leave a Comment: