Professional Documents
Culture Documents
Python 3
Python 3
Section A
== 10 == 20 False
!= or <> 10 != 20 True
> 10 > 20 False
< 10 < 20 True
>= 10 >= 20 False
<= 10 <= 20 True
Relational Operators
‘Santosh’ < ‘Aditya’ alphabetical order is checked
‘god’ < ‘God’ lowercase > uppercase
a = b is assignment, a == b is comparison
Ranges or multiple equalities can be checked
a < b < c b falls between a and c
a == b == c all three are equal
Any non zero number (positive, negative, integer, float)
is treated as True and 0 as False
Logical Operators
and Logical AND True when both operands are true
or Logical OR True when any operand is true
not Logical NOT Used to reverse the operands state
Conditions can be combined using and or or
c1 and c2 – returns True if both are True, else False
c1 or c2 – returns True if one of them is True, else False
and or or need not be combined with conditions only. Any
valid expression can be used in place of conditions
Logical Operators
and operator evaluates all expressions. It returns the last
expression, if all evaluate to True. It returns the first value that
evaluates to False
or operator evaluates all expressions. It returns the first
expression that evaluates to True. It returns the last value that
evaluates to False
not operator negates the conditions result.
a = not a sets a to 0 if its is 1, and 1 if its is 0
a = not b does not change the value of b
all() and any()are built in functions with same effect
Types of Operators
Unary – Operator needs only one operand e.g. not
Binary – Operator needs two operands e.g. +,<,and
Python supports one additional decision making entity
called conditional expression (Ternary operator in C)
<expr1> if <conditional expression> else <expr2>
conditional expression is evaluated first.
If it is True, expression evaluated to <expr1>.
If it is False, expression evaluated to <expr2>
Bitwise Operators
& Bitwise AND – Operator copies bit if its exists in both operands
| Bitwise OR – Operator copies bit if its exists in either operand
^ Bitwise XOR – Operator copies bit if its exists only in operand
~ Bitwise Inverse or NOT – Unary Operator used to reverse the
bits of an operand
<< Left shift – Operator is used to shift the bits towards left
>> Right shift – Operator is used to shift the bits towards right
Bitwise Operators
& Check whether a bit is on or off. Put off a particular bit
| Put on a particular bit
^ Toggle a Bit
~ Convert 0 to 1 and 1 to 0 (unary operator)
<< Shift desired number of bits from left
>> Shift desired number of bits from right
Anything ANDed with 0 is 1
Anything ORed with 0 is 1
Anything XORed with 0 is 1
Membership and Identity Operators
Membership operations are used to check an item or an
element that is part of a string, a list or a tuple
in returns True, if item is in list or sequence
not in returns True, if item is not in list or sequence
Identity operations are used to check whether both the
operands are same or not
is returns True, if the operands are equal
not is returns True, if the operands are not the same
Operator Precedence (Higher to Lower)
( ) Parentheses left-to-right
** Exponent right-to-left
* / % // Multiplication / Division / Modulus left-to-right
+ – Addition / Subtraction left-to-right
<<, >> Bitwise Left Shift and Right Shift left-to-right
<=, <, >, >= Comparison Operators left-to-right
<>, ==, != Equality Comparison Operators left-to-right
Operator Precedence (Higher to Lower)
is, not is Identity Operator left-to-right
in, not in Membership Operator left-to-right
& Bitwise AND left-to-right
^, | Bitwise XOR and OR left-to-right
not Logical NOT right-to-left
and, or Logical AND / OR left-to-right
= += -= *=
Assignment and Shorthand right-to-left
/= %= //= **=
Console Output Functions
print( ) function is used to output to the screen
print( ) function has this form
print(objects, sep =‘ ’, end = ‘\n’,
file = sys.stdout, flush = False)
Objects will be printed on the screen (stdout),
separated by space (‘ ‘) and last object will printed
by new line(‘\n’).
flush = False indicates output stream will not be
flushed
Console Output Functions
Python has facility to call functions and pass keyword
based values as arguments.
While calling print( ), we can pass specific values for
sep and end. In this case, default values will not be used
print(a, b, c, sep =‘,’, end = ‘!’)
print(x, y, sep =‘…’, end = ‘#’)
Console Output Functions
Four ways to control the formatting of output
Using formatted string (fstrings) literals – easiest
Using the format( ) method – older
C printf( ) style – legacy
Using slicing and concatenation operation – difficult
Console Input Functions
input ( ) function can be used to receive input values
form keyboard
input( ) function has this form
s = input(‘prompt’)
prompt is a string that will be displayed on the screen,
soliciting a value.
input ( ) function returns a string
The string must be converted into an int or float
Console Input Functions
input( ) can be used to receive multiple values
name = input (‘Enter full name’)
fname, mname, lname = input (‘Enter full
name’).split( )
split( ) function will split the entered full name with
space as a delimiter. The split values will be assigned to
the variables.
n1,n2,n3 = input(‘Enter three values:’).split( )
n1, n2, n3 = int (n1), int (n2), int (n3)
Console Input Functions
input( ) can be used to receive arbitrary number of values
input( ) can be used to receive different types of values at
a time
data= input('Enter name,age,salary:').split()
name = data[0]
age = int (data[1])
salary = float(data[2])
print(name, age, salary)
String returned by input( ) must be converted to int or
float before performing arithmetic operations