Operator and Decision Making - Jupyter Notebook
Operator and Decision Making - Jupyter Notebook
Python Operators
Operators are used to perform operations on variables and values.
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operator
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Out[1]: 3.3333333333333335
Out[3]: 3
Out[4]: 8
In [ ]:
== Equal x == y
!= Not equal x != y
Out[5]: True
In [6]: 20>10
Out[6]: True
In [7]: 12.3<12
Out[7]: False
and Returns True if both statements are true x < 5 and x < 10
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)
Out[8]: True
Out[9]: False
Out[10]: True
not 10
Out[11]: False
In [12]: not 0
Out[12]: True
Out[13]: False
in Returns True if a sequence with the specified value is present in the object x in y
not in Returns True if a sequence with the specified value is not present in the object x not in y
Out[16]: True
In [17]: st = "red green yellow blue white"
'Green' in st
Out[17]: False
Out[19]: True
identity operator
Identity Operator compare the memory location of two objects
Operator
is
is not
In [20]: a=10
b=10
print(id(a),id(b))
140718834033328 140718834033328
In [21]: a is b
Out[21]: True
In [22]: a is not b
Out[22]: False
In [23]: a=10
b=10.0
a is b
Out[23]: False
Bitwise operators
Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.
In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
Bitwise OR
In [24]: 10&4
Out[24]: 0
In [28]: 10&6
Out[28]: 2
In [29]: ~10
Out[29]: -11
In [30]: 10<<1
Out[30]: 20
In [31]: 10>>1
Out[31]: 5
Decision Making
What is an if statement?
In [3]: if 10<5:
print("10 is Greater")
print("hi")
print("hi")
print("hi")
print("Outside")
Outside
In [ ]:
True
In [1]: not 0
Out[1]: True
In [2]: if 0:
print("This evaluates to True.")
else:
print("This evaluates to False.")
In [7]: if -51:
print("This evaluates to True.")
else:
print("This evaluates to False.")
In [16]: a=None
In [18]: print(a)
None
In [19]: type(a)
Out[19]: NoneType
WAP that accepts marks of five subjects and finds percentage and prints grades according to th
e
following criteria:
Between 90-100%--------------Print „A‟
80-90%----------------------------Print „B‟
60-80%---------------------------Print „C‟
Below 60%----------------------Print „D‟
if 10>2:
pass
print("Hi")
Hi
In [ ]: