Operators in Python


Operators and Operands :
......................:
Python supports the following operators:
Arithmetic Operators.
Relational Operators.
Assignment Operators.
Logical Operators.
Membership Operators.
Identity Operators.
Bitwise Operators.
1) Arithmetic Operators:
Operators    Description
//          Perform Floor division(gives integer value after division)
+          To perform addition
-           To perform subtraction
*          To perform multiplication
/           To perform division
%          To return remainder after division(Modulus)
**       Perform exponent(raise to power)

>>> 10+20
30
>>> 20-10
10
>>> 10*2
20
>>> 10/2
5
>>> 10%3
1
>>> 2**3
8
>>> 10//3
3
>>> 

Highest President ()               Parentheses
Powers                     **                  Exponentiation
Asterisk                     /                      Division
Slash             *                      Multiplication
Plus                       +                            Addition
Lowest          Minus                                Subtraction

User Input:
Example:
mass_kg = int(input("What is your mass in kilograms?" ))
mass_stone = mass_kg * 2.2 / 14
print("You weigh", mass_stone, "stone.")


Python's  "print"  statement prints numbers to 10 decimal places. But what if you only want one or two? We can use the round() function, which rounds a number to the number of decimal points you choose. round() takes two arguments: the number you want to round, and the number of decimal places to round it to. For example:
>>> print (round(3.14159265, 2))
3.14

Now,
twoSigFigs = round(mass_stone, 2)
numToString = str(twoSigFigs)
print ("You weigh " + numToString + " stone.")

Examples: Level(2):
1.      Use Python to calculate {\displaystyle 2^{2^{2^{2}}}=65536}2^{{2^{{2^{2}}}}}=65536.

   2. What is the Output of the following

  >>> (3+2)**4/7

  >>> 4+(2**4)/7

   3. Write a python program

Take two inputs from the user and try to add, subtract,multiply,divide ,modulus and power of the two numbers

Sample:
num1 = 10
num2 = 5
res = 0

res = num1 + num2
print ("Line 1 - Result of + is ", res)

res = num1 - num2
print ("Line 2 - Result of - is ", res)

res = num1 * num2
print ("Line 3 - Result of * is ", res)

res = num1 / num2
print ("Line 4 - Result of / is ", res)

res = num1 // num2
print ("Line 5 - Result of // is ", res)

res = num1 ** num2
print ("Line 6 - Result of ** is ", res)

res = num1 % num2
print ("Line 7 - Result of % is ", res)

2) Relational Operators
<          Less than
>          Greater than
<=       Less than or equal to
>=       Greater than or equal to
==       Equal to
!=        Not equal to
<>       Not equal to(similar to !=)
>>> 10<20
True
>>> 10>20
False
>>> 10<=10
True
>>> 20>=15
True
>>> 5==6
False
>>> 5!=6
True
>>> 10<>2
True
>>> 
            Examples:

x = 10
y = 12

# Output: x > y is False
print('x > y  is',x>y)

# Output: x < y is True
print('x < y  is',x<y)

# Output: x == y is False
print('x == y is',x==y)

# Output: x != y is True
print('x != y is',x!=y)

# Output: x >= y is False
print('x >= y is',x>=y)

# Output: x <= y is True
print('x <= y is',x<=y)

3) Assignment Operators:
Operators    Description
=          Assignment
/=        Divide and Assign
+=       Add and assign
-=        Subtract and Assign
*=       Multiply and assign
%=       Modulus and assign
**=     Exponent and assign
//=       Floor division and assign

>>> c=10
>>> c
10
>>> c+=5
>>> c
15
>>> c-=5
>>> c
10
>>> c*=2
>>> c
20
>>> c/=2
>>> c
10
>>> c%=3
>>> c
1
>>> c=5
>>> c**=2
>>> c
25
>>> c//=2
>>> c
12
>>> 

4) Logical Operators:

Operators    Description
and     Logical AND(When both conditions are true output will be true)
or        Logical OR (If any one condition is true output will be true)
not     Logical NOT(Compliment the condition i.e., reverse)

eg:

a=5>4 and 3>2
print a
b=5>4 or 3<2
print b
c=not(5>4)
print c

Output:

>>> 
True
True
False
>>> 

5) Membership Operators:

Operators    Description
in         Returns true if a variable is in sequence of another variable, else false.
not in Returns true if a variable is not in sequence of another variable, else false.

eg:

a=10
b=20
list=[10,20,30,40,50];
if (a in list):
print "a is in given list"
else:
print "a is not in given list"
if(b not in list):
print "b is not given in list"
else:
print "b is given in list"

Output:

>>> 
a is in given list
b is given in list
>>> 
6)
Identity Operators:

Operators    Description
is         Returns true if identity of two operands are same, else false
is not Returns true if identity of two operands are not same, else false.

Example:

a=20
b=20
if( a is b):
print  ?a,b have same identity?
else:
print ?a, b are different?
b=10
if( a is not b):
print  ?a,b have different identity?
else:
print ?a,b have same identity?
Output:
>>> 
a,b have same identity
a,b have different identity
>>> 
Examples:
num1 = 4
num2 = 5
res = num1 + num2
res += num1
print ("Line 1 - Result of + is ", res)
3.
a = 9
b = 12
c = 3
x = a - b / 3 + c * 2 - 1
y = a - b / (3 + c) * (2 - 1)
z = a - (b / (3 + c) * 2) - 1
print("X = ", x)
print("Y = ", y)
print("Z = ", z)

The output

$ ./evaluationexp.py
X =  10
Y =  7
Z =  4

4.
if 1 < 2 and 4 > 2:
print("condition met")

if 1 > 2 and 4 < 10:
print("condition not met")

if 4 < 10 or 1 < 2:
print("condition met")

5.
>>> x = 2
>>> 1 < x < 3
True
>>> 10 < x < 20
False
>>> 3 > x <= 2
True
>>> 2 == x < 4
True

6.
>>> p = 'hello'
>>> ps = p
>>> ps is p
True
7
five = 5
two = 2
zero = 0

print five and two and zero
o/p:0

>>> five = 5
>>> two = 2
>>> print five + two


Strings Intro Just for Reference:
# Python string examples - all assignments are identical.
String_var = 'Python'
String_var = "Python"
String_var = """Python"""

# with Triple quotes Strings can extend to multiple lines
String_var = """ This document will help you to
explore all the concepts
of Python Strings!!! """

# Replace "document" with "tutorial" and store in another variable
substr_var = String_var.replace("document", "tutorial")
print (substr_var)

sample_str = 'Python String'

print (sample_str[0])       # return 1st character
# output: P

print (sample_str[-1])      # return last character
# output: g

print (sample_str[-2])      # return last second character
# output: n

sample_str = 'Python String'
print (sample_str[3:5])        #return a range of character
# ho
print (sample_str[7:])      # return all characters from index 7
# String
print (sample_str[:6])      # return all characters before index 6
# Python
print (sample_str[7:-4])
# St

print ("Employee Name: %s,\nEmployee Age:%d" % ('Ashish',25))

# Employee Name: Ashish,
# Employee Age: 25

Comments

Popular posts from this blog

Linear Regression Numpy code

week 11(21-25 october)

How java is different from c/c++?