KEMBAR78
Python Training in Bangalore | Python Operators | Learnbay.in
Operators in Python
Python class 2
Complex Numbers
form a + bJ, where a and b are floats and j represents the square root of -1 (which is
an imaginary number).
x= complex(a,b)
abs(x)
x.real x.imag ,x.conjugate(),x.abs()
Not used much in Python programming
Code: https://repl.it/H5bg/2
Operators
Special symbols that represent computations like addition
and multiplication. The values the operator is applied to are
called operands.
a+b=c { a and b is operand and + is operator}
Types of Operators:
Arithmetic Operators
Comparison (Relational) Operators
Logical Operators
Bitwise Operators
Arithmetic Operators
+ - * / // % **
+ and - can be used as unary and binary operator. {unary means identity ).Examples:
complex(2,3) * complex(2,3),7+True,8-False
4/9, -4/9, -5--5, 7//9, -7//9 , -7.0//2, -7.0/2
“Abc” * 2, “Abc” * -2, “Abc” *0, “Abc”* True, 7+True,8.5 +True
math.floor(-7.5) ,math.ceil(-8.2), math.ceil(-1), 4**2 ,2**1.2
% operator and modular division of -ve number.
Code Exercise: https://repl.it/H5c4/5
https://www.python.org/dev/peps/pep-0238/ ----> Proposal to change division operator
Relational Operators
== != < > <= >= is is
not
in
Difference between is and ==
a = ’ abc ’
b = a
c = input ( ’ Enter String : ’)
Relational Operators
An is expression evaluates to True if two variables point to the same (identical)
object.
An == expression evaluates to True if the objects referred to by the variables are
equal (have the same contents).
< and > for two strings
ord(‘a’) to find ASCII
Logical Operators
Discuss the truth table: for booleans.
And operator: first evaluates its left operand,if false,result is left else right operand
Or : evaluates its left operand,if True,the result returned is the value of the left
operand; otherwise, right operand.
Not operator can only be used as unary operator.
True is not True
Bit–wise Operators
Bitwise operator works on bits and performs bit by bit operation.
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Bit–wise Operators
Left Shift << and right Shift >>
Operator Precedence :
PEMDAS : Parentheses,Exponentiation,Multiplication.Division,Addition,Substraction
* And / has same precedence. + and - has same precedence.
Operator with same precedence are evaluated from left to right.
Control Flows
IF :Used to check a condition: if the condition is true, run a block of statements
(called the if-block), else process another block of statements (called the else-
block). The else clause is optional.
elif:
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Coding Exercises
1. Input the number from keyboard and check if number is positive or negative.
Modify the code and print “Number is Zero” if number is 0.
2. Decide a number and take guesses from the user and check if it is the number
that we have.
3. Python Program to check if number is even or odd.
// And %
7 3 2 1
-7 3 -3 2
7 -3 -3 -2
-7 -3 2 -1

Python Training in Bangalore | Python Operators | Learnbay.in

  • 1.
  • 2.
    Complex Numbers form a+ bJ, where a and b are floats and j represents the square root of -1 (which is an imaginary number). x= complex(a,b) abs(x) x.real x.imag ,x.conjugate(),x.abs() Not used much in Python programming Code: https://repl.it/H5bg/2
  • 3.
    Operators Special symbols thatrepresent computations like addition and multiplication. The values the operator is applied to are called operands. a+b=c { a and b is operand and + is operator} Types of Operators: Arithmetic Operators Comparison (Relational) Operators Logical Operators Bitwise Operators
  • 4.
    Arithmetic Operators + -* / // % ** + and - can be used as unary and binary operator. {unary means identity ).Examples: complex(2,3) * complex(2,3),7+True,8-False 4/9, -4/9, -5--5, 7//9, -7//9 , -7.0//2, -7.0/2 “Abc” * 2, “Abc” * -2, “Abc” *0, “Abc”* True, 7+True,8.5 +True math.floor(-7.5) ,math.ceil(-8.2), math.ceil(-1), 4**2 ,2**1.2 % operator and modular division of -ve number. Code Exercise: https://repl.it/H5c4/5 https://www.python.org/dev/peps/pep-0238/ ----> Proposal to change division operator
  • 5.
    Relational Operators == !=< > <= >= is is not in Difference between is and == a = ’ abc ’ b = a c = input ( ’ Enter String : ’)
  • 6.
    Relational Operators An isexpression evaluates to True if two variables point to the same (identical) object. An == expression evaluates to True if the objects referred to by the variables are equal (have the same contents). < and > for two strings ord(‘a’) to find ASCII
  • 7.
    Logical Operators Discuss thetruth table: for booleans. And operator: first evaluates its left operand,if false,result is left else right operand Or : evaluates its left operand,if True,the result returned is the value of the left operand; otherwise, right operand. Not operator can only be used as unary operator. True is not True
  • 8.
    Bit–wise Operators Bitwise operatorworks on bits and performs bit by bit operation. a = 0011 1100 b = 0000 1101 ----------------- a&b = 0000 1100 a|b = 0011 1101 a^b = 0011 0001 ~a = 1100 0011
  • 9.
    Bit–wise Operators Left Shift<< and right Shift >> Operator Precedence : PEMDAS : Parentheses,Exponentiation,Multiplication.Division,Addition,Substraction * And / has same precedence. + and - has same precedence. Operator with same precedence are evaluated from left to right.
  • 10.
    Control Flows IF :Usedto check a condition: if the condition is true, run a block of statements (called the if-block), else process another block of statements (called the else- block). The else clause is optional. elif: if expression1: statement(s) elif expression2: statement(s) elif expression3: statement(s) else: statement(s)
  • 11.
    Coding Exercises 1. Inputthe number from keyboard and check if number is positive or negative. Modify the code and print “Number is Zero” if number is 0. 2. Decide a number and take guesses from the user and check if it is the number that we have. 3. Python Program to check if number is even or odd.
  • 12.
    // And % 73 2 1 -7 3 -3 2 7 -3 -3 -2 -7 -3 2 -1