unit3-GE3151 NOTES
unit3-GE3151 NOTES
1. input( ) is used.
2. raw_input( ) is used in older version Q1. Describe the various types of operators in python
language.
Ans:
Example 1 :Write a python program to perform addition
of two numbers. Accept the two numbers using keyboard Expression: An expression is a sequence of operators and operands that
specifies computation of a value. Ex: x = a + b.
add.py performed. The values the operator uses are called operands.
Ex: x=a + b. Here a, b, x are operands.
Operator: An operator is a symbol that specifies an operation to be performed
on operands. Ex: x = a + b. Here + is an operator.
1. Unary Operator:
A Unary operator operates on only one operand.
Unary operator precedes their single operand.
Output: OPERATOR MEANING
- Unary Minus
+ Unary Plus
~ Complement
2.Binary Operator :
A Binary operator operates on two operands.
Ex.8+3.+ is the binary operator and it operates on 8,3.
3.Ternary Operator :
Output:
1. print – print( ) function used A Ternary operator operates on three operands.
1.ARITHMETIC OPERATORS
+ Unary Plus
Highest RL
- Unary Minus
~ Bitwise NOT
* Multiplication
/ Division
Intermediate LR
// Floor Division
% Modulus
+ Addition
Lowest LR
- Subtraction
Unary Plus Operator: Operator can appear only towards the left side of its
Operand. Ex: +5
Unary Minus Operator: Operator can appear only towards the left side of
its Operand. Ex: -9
1.ARITHMETIC OPERATORS 2.Comparison Operator or Relational Operator
It is used to compare two quantities.
Operator Meaning Example There should be no white space character between 2 symbols of a
relational operator.
Addition operator is used for The result of relational operator is a Boolean constant.
+ performing addition of two 10 + 20 = 30 It forms a condition.
numbers. Sl
N OPERATOR NAME EXAMPLE RESULT
Subtraction operator is used o.
– 20 – 10 = 10 1 > Greater A>B
for performing subtraction True if A > B
than
Multiplication operator is 2. < Less A>B
True if A>B
than
* used for performing 10 * 10 = 100
3. >= Greater A>=B
multiplication than True if A>=B
equal to
Division operator is used for
4. <= Less A<=B
/ performing division of two 10 / 2 = 5 than True if A<=B
numbers equal to
5. == Equal to A= =B True if A= = B
Mod operator returns the 6. != Not A!=B
% 10 % 2 = 0 True if A!=B
remainder value equal to
OUTPUT:
3. LOGICAL OPERATORS Ex 1: 2&3 evaluates to 2
It is used to logically relate the Expressions. Ex 2: 2| 3 evaluates to 3
It operates according to the truth Tables.
Sl.No OPERATOR NAME OF RESULT S
OPERATOR i
1 not Logical NOT False if condition is True g
and Logical AND True if both operands are true n
2 Magnitude
3 or Logical OR True if either operands is true
Value B
Operator i
TRUTH TABLE: and t
Result B B B B B B B B B B B B B B B B
AND Operation OR Opeartion i i i i i i i i i i i i i i i i
Operand Operand Result Operand Operand Result t t t t t t t t t t t t t t t t
1 2 1 2
False False False False False False 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
False True False False True True 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1
True False False True False True 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
True True True True True True 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
2&3=2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
NOT Operation 2 | 3 =3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
Operand Result
1
False True Left Shift by 1 bit is equivalent to multiplication by 2 .
n
True False Left shift by n bits is equivalent to multiplication by 2
provided the magnitude does not overflow.
1
Ex 1: 4<<1 evaluates to 8(4*2 ).
2
Ex 2: 5<<2 evaluates to 20(5*2 ).
S
i
g
n
Magnitude
Value B
Operator i
and t
Result B B B B B B B B B B B B B B B B
i i i i i i i i i i i i i i i i
t t t t t t t t t t t t t t t t
1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1
5 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
5<<2= 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0
20
5>>2= 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1
4. BITWISE OPERATORS
It operates on the individual bits of the operand and are used for bit bit.py
manipulation.
It does not consider the operand as one entity.
x=5
Sl.No OPERATOR NAME OF RESULT
y=6
OPERATOR
#Bitwise operator
1 ~ Bitwise NOT Complement z=x&y
bits print("x and y::",z)
2 << Left Shift Operands are z=x|y
shifted left by print("x or y::",z)
the number of z=x^y
times print("x xor y::",z)
specified z=~x
>> Right Shift Operands are print("Not(~) x::",z)
shifted right z=~y
by the print("Not(~) y::",z)
number of z=x<<2
times print("x Left shift by 2 is ::",z)
specified z=x>>2
3 & Bitwise AND True if Both print("x Right shift by 2 is ::",z)
operands are
true #Output
4 ^ Bitwise X- True if Both x and y:: 4
OR operands are x or y:: 7
false x xor y:: 3
5 | Bitwise OR True if either Not(~) x:: -6
of one Not(~) y:: -7
operands is x Left shift by 2 is :: 20
true x Right shift by 2 is :: 1
5. ASSIGNMENT OPERATOR
A Variable can be assigned a value by using assignment operator.
The operator that appears towards the left side of an assignment
operator is called short-hand operator. Syntax: 2
op1+=op2 means op1=op1+op2.
ex: a/=9 means a=a/9 (Y,X) if condition
OPE
Sl. It First evaluates the condition if its true, X is
RAT EXPRESSION RESULT
No evaluated and its value is returned; Otherwise Y
OR
1 = A=B Assigns Result to left operand is evaluated and its value is returned;
2 *= Perform Multiply on 2
A*=B ternary.py
operands and assign value to
i.e ( A=A*B)
left operand
3 /= Perform Division on 2
A/=B
operands and assign value to
i.e ( A=A/B)
left operand
4 %= Compute Modulus of 2
A%=B
operands and assign value to
i.e ( A=A%B)
left operand
5 += Perform Addition on 2
A+=B
operands and assign value to
i.e ( A=A+B)
left operand
6 -= Perform Subtraction on 2
A-=B
operands and assign value to
i.e ( A=A-B)
left operand
7 **= Perform Floor Division on 2
A**=B
operands and assign value to
i.e ( A=A**B)
left operand
#Short hand operator i.e x+=y means x=x+y OPERATOR MEANING EXAMPLE
True if value /
x+=y in Variable is found in 5 in x
the sequence
print("x+=y::",x) True if value /
x-=y not in Variable is not found 5 not in x
print("x-=y::",x) in the sequence
x*=y
print("x*=y::",x)
x/=y
print("x/=y::",x)
x%=y
print("x%=y::",x)
x**=y
print("x**=y::",x)
x//=y
print("x//=y::",x)
#Output
x+=y:: 27
x-=y:: 12
x*=y:: 180
x/=y:: 12.0 8. IDENTITY OPERATOR
Used to check if two values (or variables) are located in same
x%=y:: 12.0 part of memory.
x**=y:: 1.5407021574586368e+16 two values that are equal does not imply that are identical.
6. CONDITIONAL / TERNARY OPERATOR
x//=y:: 1027134771639091.0
It is the only Ternary operator available in Python Language.
x:12+y:15=27 OPERATOR MEANING EXAMPLE
It has the Lowest Priority of all operations. True if operands are
is identical
A is B
Syntax: 1 True if operands are
is not not identical
A is not B
X if True condition else Y
identity .py
CONTROL
STRUCTUR
ES
UNCONDITION
AL STATEMNT
CONDITIONAL
STATEMENT
LOOPING / BREAK
ITERATIVE
STATEMENT
FOR CONTINUE
WHILE
SELECTION/
BRANCHING/
DECISION
MAKING PASS
IF IF ELSE
IF
9. LAMBDA OPERATOR ELIF NESTED
ELSE
Its just like a macro which means that we can any operation at
lambda function by calling the function with variable name and
value would return the final value of operated value.
It return output in form of expression only
Syntax:
CONTROL STRUCTURES
IF – ELSE:
The alternative statements are the type of if statements in which if…else EXAMPLE: 3
statements are used. Program to find a number is Odd or Even
EXAMPLE: 1
Example 4:
EXAMPLE: 2
Example: 1
Write a python to identify if a number is positive, negative
or zero.
Sample Output
Iteration
Definition: Repeated execution of a set of statements is called
iteration.
1.while
Type 2:
Flowchart:
Example 1:
Example:
continue
The continue statement is used to skip some
statements inside the loop. The continue statement is
used with decision making statement such as if...else.
The continue statement forces to execute the next
iteration of the loop to execute.
Syntax:
continue
Example 2 Example:
pass
The pass statement is used when we do not want to
execute any statement. Thus the desired statements can be
bypassed.
Syntax:
Library Function:
#To Find Given number is prime or not These are the functions design and developed
by software provider
print("Enter the number");
Examples:
num = int(input())
prime=0 sqrt(x)
# prime numbers are greater than 1 exp(x)
if num > 1: sin(x)
# check for factors type(100)
for i in range(2,num):
User Defined Function:
if (num % i) == 0:
It declares with keyword def and followed
prime=1
print(num,"is not a prime number") by function name.
Function Definition:
break
if prime==0: Syntax:
print(num,"is a prime number")
def function_name (parameters) :
# if input number is less than
statements This colon is a must
# or equal to 1, it is not prime
else:
print(num,"is not a prime number") Function may or may not take arguments as
input with opening and closing parentheses,
Enter the number just after function name followed by colon.
6 After defining function name and arguments
6 is not a prime number block of program statements at next line and
these statements must be indented.
Enter the number
Function must be defined before it is called.
7
7 is a prime number Here user must define the function and invoke
the function by calling its name.
3. FUNCTION
It is a Sub-Program that contains one or more
statements and it perform some task when called.
Every function is specified by its name.
Types:
Syntax
return [expression_list]
function
definition
function call
Fruitful Functions
There are two types of functions.
1) The functions that return some value
2) The functions that does not return the value.
The fruitful functions are the functions that return
values.
def add(x,y):
z=x+y
return z
a,b=10,20
c=add(a,b)
print(“OUTPUT c=”,c)