UNIT 1
LAB EXPERIMENTS
Aim)Write a program to add and multiply given complex numbers
Source Program
Ans)
print("Addition of two complex numbers : ",(4+3j)+(3-7j))
print("Subtraction of two complex numbers : ",(4+3j)-(3-7j))
print("Multiplication of two complex numbers : ",(4+3j)*(3-7j))
print("Division of two complex numbers : ",(4+3j)/(3-7j))
output:
Aim)write a program to print multiplication table to given number
num = int(input("Display multiplication table of? "))
for i in range(1, 11):
print(num, 'x', i, '=', num*i)
output:
[Link] the following operators in python with suitable example?
Source Program:For example, to add two numbers that are stored in operands x and y is written as,
x+y. Where + is the Arithmetic addition operator. The operator in Python are classified as follow:
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Bitwise Operators
4. Logical Operator
5. Assignment Operators
6. Membership Operators
7. Identity Operators
1. Arithmetic Operators
These operators are used to perform operations such as addition, subtraction, multiplication,
division and modulo division. For example, x=7, and y=3.
Operator Meaning Example
+ Addition-Used to perform arithmetic addition x+y, results in 10
- Subtraction-Used to perform arithmetic subtraction x-y, results in 4
* Multiplication-Used to perform multiplication x*y, results in 21
/ Division-Used to perform division x/y , results in 2
% Modulus-Used to perform modulus operation (remainder) x%y, results in 1
// Used to perform floor division (floor value) x//y, results in 2
** Exponent- Used to raise operand on left to the power x**y, 343
of operand on right
Example program:
[Link]
Source Code Output
x=input("Enter value of x :") Enter value of x
y=input("Enter value of y :") :7 Enter value of y
print " " :3
print " Addition is:",(x+y)
print " Subtraction is:",(x-y) Addition is: 10
print " Multiplication is:", Subtraction is: 4
(x*y) print " Division Multiplication is: 21
is:",(x/y) Division is: 2
print " Modulus is:",(x%y) Modulus is: 1
print " Floor Division Floor Division is: 2
is:",(x//y) Exponent is: 343
print " Exponent is:",
(x**y) print " "
2. Comparison (Relational) Operators
These operators are used to compare value. The operators can either return True or False
according to the condition. The table with following values for x=7 and y=3
Operator Meaning Example
> Greater Than-Returns True if the left operand is greater x>y, results in True
than the right, otherwise returns False
< Less Than-Returns True if the left operand is less than X<y, results in False
the right, otherwise returns False
== Equal to-Returns True if both are equal, otherwise False x==y, returns False
!= Not Equal to- Returns True if both are not equal, x!=y, return True
otherwise False
>= Greater than or Equal- Returns True if the left operand x>y, returns True
is greater than or equal to the right, otherwise returns
False
<= Less than or Equal- Returns True if the left operand is X<y, returns False
Less than or equal to the right, otherwise returns False
Example Program: [Link]
Source Code Output
x=input("Enter value of x ") Enter value of x 7
y=input("Enter value of y ") Enter value of y 3
print " "
print " Greater than is:", Greater than is:
(x>y) print " Less Than is:", True Less Than is:
(x<y) print " Equality is :", False Equality is :
(x==y) print " Not equal is:", False Not equal is:
(x!=y) True
print " Greater than or equal is:",(x>=y) Greater than or equal is: True
print " Less than or equal is:", Less than or equal is: False
(x<=y) print " "
3. Bitwise Operators
Bitwise operators act on operands as if they were string of binary digits. The operators operate
bit by bit. For example, x=2 (binary value is 10) and y=7 (Binary value is 111). The binary
equivalent of the decimal values of x and y will be 10 and 111 respectively.
Operator Meaning Example
& Bitwise AND X&y=0
x=010
y=111
x&y= 010 ( 2)
| Bitwise OR x|y=7
x=010
y=111
x|y=111 (7)
~ Bitwise Not ~x is ,-3
^ Exclusive OR (XOR) X^y=5
x=010
y=111
x|y=101 (5)
>> Shift Right (operand >>no. of bit positions) x>>1, results 1
<< Shift Left (operand <<no. of bit positions) X<<2, 1000 (8)
Example Program: [Link]
Source Code Out put
x=input("Enter value of x :") Enter value of x :2
y=input("Enter value of y :") Enter value of y :7
print "-----Bitwise Operations--------" -----Bitwise Operations--------
print " AND (&) is:",(x&y) AND (&) is: 2
print " OR (|) is:",(x|y) OR (|) is: 7
print " XOR (^) is:",(x^y) XOR (^) is: 5
print " Not (~) is:",(~x) Not (~) is: -3
print " Shift Right(>>) is:", Shift Right(>>) is: 1
(x>>1) print " Shift Left (<<)is:", Shift Left (<<)is: 8
(x<<2)
print " "
4. Logical Operator:
There are three logical operators: and, or, and not. The semantics (meaning) of these
operators is similar to their meaning in English. For example, x > 0 and x < 10 is true only if x is
greater than 0 and less than 10. n % 2 == 0 or n % 3 == 0 is true if either (or both) of the
conditions is true, that is, if the number is divisible by 2 or 3. Finally, the not operator negates a
boolean expression, so not(x > y) is true if (x > y) is false, that is, if x is less than or equal to y.
Operator Meaning Example
And True if both the operands are x and y
True
Or True, if either of the operands x or y
is True
Not True if operand false not x
Example Program: [Link]
Source Code Out Put
x=True x and y is : False
y=False x or y is : True
print " x and y is :",x and y not x is: False
print " x or y is :",x or y
print " not x is:",not x
5. Assignment Operator
Assignment operator is used to assign values to the variable. For example, x=5 is simple
assignment operator, that assigns value 5 to the to the variable x. There are various compound
operators in python like a+=5, which adds value 5 to the variable and later assigns it to variable
a. This expressions is equivalent to a=a+5. The same assignment operator can be applied to all
expressions that contain arithmetic operators such as, *=, /=, -=, **=,%= etc.
x=4
x+=5
>>> x
print “The value of x is:”, x
>>> The value of x 9
6. Membership Operators
These operators are used to test whether a value or operand is there in the sequence such as list,
string, set, or dictionary. There are two membership operators in python: in and not in. In the
dictionary we can use to find the presence of the key, not the value.
Operator Meaning Example
In True if value or operand is 5 in x
present in the sequence
not in True if value or operand is not 5 not in x
present in the sequence
Example Program:
Source Code Output
#membership operator: in and not in ('H in x', True)
x="Hello Python" # string ('hello in x',
False)
y={1:'a',2:'n',3:'t'} #dictionary ('1 in y', True)
print ("H in x",'H' in x) ('a in y', False)
print ("hello in x","hello" in x)
print ("1 in y",1 in y) # 1 key key in dictionary
print ("a in y",'a' in y) #a is value in dictionary
7. Identity Operators
These are used to check if two values (variable) are located on the same part of the memory.
If the x is a variable contain some value, it is assigned to variable y. Now both variables are
pointing (referring) to the same location on the memory as shown in the example program.
Operator Meaning Example
Is True if the operands are X=5
identical ( refer to the same Y=X
memory) X is Y , returns True
is not True if the operands are not X=5 #int
identical ( refer to the same Y=5.0 # float
memory) X is not Y, returns True
Example Program:
Source Code Output
#indetity operator program ('x is y', False)
x=[1,2,3] #list ('x is z', True)
y=[1,2,3] #list
z=x
print ("x is y", x is y) # x and y are different
Objects
print ("x is z", x is z) # x and z are refering to
same object
Aim:Write a program to swap two numbers without using a temporary variable?
Source Program:
a=5
b = 10
a, b = b, a
print("a:", a)
print("b:", b)
output:
Aim:Write a program to find the largest element among three elements?
Source Program
# change the values of num1, num2 and num3
# for a different result
num1 = 10
num2 = 14
num3 = 12
# uncomment following lines to take three numbers from user
#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
output