0% found this document useful (0 votes)
3 views

unit3-GE3151 NOTES

The document provides an overview of various types of operators in Python, including unary, binary, and ternary operators, as well as arithmetic, relational, logical, bitwise, assignment, membership, identity, and lambda operators. It explains the function and precedence of each operator type, along with examples of their usage in Python programming. Additionally, it covers control structures such as conditional statements and loops.

Uploaded by

rmshaira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

unit3-GE3151 NOTES

The document provides an overview of various types of operators in Python, including unary, binary, and ternary operators, as well as arithmetic, relational, logical, bitwise, assignment, membership, identity, and lambda operators. It explains the function and precedence of each operator type, along with examples of their usage in Python programming. Additionally, it covers control structures such as conditional statements and loops.

Uploaded by

rmshaira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Input: OPERATORS

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.

Solution : Operand: An operand is an entity on which an operation is to be

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.

2. using format  The only Ternary operator is Conditional operator.


 Using .format the data can be displayed on the Types of operators:
console.
1. arithmetic operators
 For that purpose { } and .format is used.
2. relational / Comparison operators
add.py
3. logical operators
4. Bitwise operator
5. Assignment operator
6. Conditional Operator or Ternary operator
7. Membership operator
8. Identity operator
9. Lambda operator

1.ARITHMETIC OPERATORS

Operator Name Precedence Associativity


** Exponent

+ Unary Plus
Highest RL
- Unary Minus

~ Bitwise NOT

* Multiplication

/ Division
Intermediate LR
// Floor Division

% Modulus

+ Addition
Lowest LR
- 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

This is an exponentiation rel.py


** 2** 3 = 8
operator(power)

This is floor division operator.


In this operation the result is
// the quotient in which the 10//3 = 3
digits after the decimal point
are removed
arith.py

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

Assign.py 7.MEMBERSHIP OPERATOR


Used to test whether value or variable found in a
x=12 sequence(String,List,Tuple,Set and Dictionary)
y=15
In Dictionary we can test key not value

#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)

#Simple Assignment Statement Example


x=12
y=15
z=x+y
print("x:{}+y:{}={}".format(x,y,z))

#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:

variablename = lambda variable: (operation/expression)

ex: Lam = lambda(a) : (a+20) CONDITIONAL STATEMENTS


IF :
print(lam(30)) The if statement is the simplest form of the conditional statement.

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:

TO find Vote Eligible or not

EXAMPLE: 2

Program to find a number is positive or not


Write a python program to display the result such as
3. IF- ELIF-IF ( Chained Conditional) Distinction, A,B,C,D,F grade based on the marks entered by
Sometimes there are more than two possibilities. the user.
These possibilities can be expressed using chained
conditions
The chained conditional execution will be such that each
condition is checked in order.
The elif is basically abbreviation of else if.
There is no limit on the number of elif statements.
If there is else clause then it should be at the end.
In chained execution, each condition is checked in order
and if one of the condition is true then corresponding
branch runs and then the statement ends.
In this case if there are any remaining conditions then that
condition won’t be tested.
Syntax :

Lab Ex 1A: Eb bill Calculation:

Example: 1
Write a python to identify if a number is positive, negative
or zero.

Example 2: Nested Conditionals


 When one condition is specified inside another
condition then it is called as nested conditionals.
 Nested If : A nested if is an if statement that is the
target of another if statement.
 Nested if statements means an if statement inside
another if statement.

Sample Output

Iteration
Definition: Repeated execution of a set of statements is called
iteration.

 The programming constructs used for iteration are while,


for, break, continue and so on..

1.while

The flow of execution is specified as follows -


1. Using the condition determine if the given expression is true
or false.
2. If the expression is false then exit the while statement
3. If the expression is true then execute the body of the while
and go back to step 1 in which again the condition is checked.
For example
i=1
while i<=10:
print(i)
i=i+1
• The body of a while contains the statement which will
change the value of the variable used in the test condition.
Hence finally after performing definite number of iterations, the
test condition gets false and the control exits the while loop.
• If the condition never gets false, then the while body Ex: To Find Sum of N Natural Numbers upto n:
executes for infinite times. Then in this case, such while loop is
called infinite loop.

Type 2: Using Else in while

Ex: To Print N Natural Numbers upto n:

Ex: To Print nth Multiplication Table:


for loop
Type 1:

Type 2:

for variable in range(st_index, end_index, step_value ):


Body of for loop

The variable takes the value of the item inside the


sequence on each iteration.
Loop continues until we reach the last item in the
sequence.
The body of for loop is separated from the rest of the
code using indentation.

Write a python program for displaying even or odd numbers


between 1 to n.

Write a python program to display Fibonacci numbers for the


sequence of length n
UNCONDITIONAL STATEMENT Flowchart
[or]
LOOP CONTROLLED STATEMENTS
break
 The break statement is used to transfer the control to the
end of the loop.
 When break statement is applied then loop gets
terminates and the control goes to the next line
pointing after loop body.
Syntax:

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:

Functions Function Call


• For calling a function we use name of the
function. By giving the call to the function, it executes
and returns with some result.

Library or For example


PRE- User - Defined type is a function used for returning the data type
DEFINED Function of the argument passed to it.
Functions
return Values
The value can be returned from a function using the
keyword return.

Syntax
return [expression_list]

function
definition

function call

 The first line of function definition is called function


header and rest is called body. Global And Local scope Variables:
 Note that - The code within every function starts with a  The global variables are those variables that are
colon (:) and should be indented (space). declared and defined outside the function and can
 Python functions don't have any explicit begin or end be used inside the function.
like curly braces to indicate the start and stop for the  The local variables are those variables that are
function, they have to rely on this indentation. declared and defined inside a function.

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)

You might also like