Satyam Python 5-Copy
Satyam Python 5-Copy
EXPERIMENT NO: 5
NAME OF EXPERIMENT: Programs to study Operators,
Byte and byte arrays and Sets
DATE OF PERFORMANCE: 10/02/2025
DATE OF SUBMISSION: 24/02/2025
Theory:
a) Python Operators: Arithmetic, Logical, Comparison, Assignment,
Bitwise & Precedence
• Arithmetic Operators
Arithmetic Operators perform various arithmetic calculations like addition, subtraction,
multiplication, division, %modulus, exponent, etc. There are various methods for arithmetic
calculation in Python like you can use the eval function, declare variable & calculate, or call
functions.
Example: For arithmetic operators we will take simple example of addition where we will
add two-digit 4+5=9
x= 4
y= 5
print(x + y)
Similarly, you can use other arithmetic operators like for multiplication(*), division (/),
substraction (-), etc. %
• Comparison Operators
Comparison Operators In Python compares the values on either side of the operand and
determines the relation between them. It is also referred to as relational operators. Various
comparison operators in python are ( ==, != , <>, >,<=, etc.)
Example: For comparison operators we will compare the value of x to the value of y and
print the result in true or false. Here in example, our value of x = 4 which is smaller than y =
5, so when we print the value as x>y, it actually compares the value of x to y and since it is
not correct, it returns false.
Assignment Operators in Python are used for assigning the value of the right operand to
the left operand. Various assignment operators used in Python are (+=, - = , *=, /= , etc.).
Example: Python assignment operators is simply to assign the value, for example
num1 = 4
num2 = 5
print(("Line 1 - Value of num1 : ", num1))
print(("Line 2 - Value of num2 : ", num2))
We can also use a compound assignment operator, where you can add, subtract, multiply
right operand to left and assign addition (or any other arithmetic function) to the left
operand.
• Logical Operators
Logical operators in Python are used for conditional statements are true or false. Logical operators
in Python are AND, OR and NOT. For logical operators following condition are applied.
• For AND operator – It returns TRUE if both the operands (right side and left side) are
true
• For OR operator- It returns TRUE if either of the operand (right side or left side) is
true
• For NOT operator- returns TRUE if operand is false
Example: Here in example we get true or false based on the value of a and b
a = True b = False print(('a
and b is',a and b)) print(('a
or b is',a or b)) print(('not
a is',not a))
Example: For example here we check whether the value of x=4 and value of y=8 is available
in list or not, by using in and not in operators.
x = 4 y = 8 list = [1,
2, 3, 4, 5 ]; if ( x in
list ):
print("Line 1 - x is available in the given list")
else: print("Line 1 - x is not available in the given
list") if ( y not in list ):
print("Line 2 - y is not available in the given list")
else: print("Line 2 - y is available in the given
list")
Identity Operators
Identity Operators in Python are used to compare the memory location of two objects. The
two identity operators used in Python are (is, is not).
• Operator is: It returns true if two variables point the same object and false otherwise
• Operator is not: It returns false if two variables point the same object and true
otherwise
** Exponent
+, - Addition, Subtraction
Example:
x = 20 y =
20 if ( x is
y ):
print("x & y SAME identity")
y=30 if ( x is not y ):
print("x & y have DIFFERENT identity")
Operator precedence
The operator precedence determines which operators need to be evaluated first. To avoid
ambiguity in values, precedence operators are necessary. Just like in normal multiplication
method, multiplication has a higher precedence than addition. For example in 3+ 4*5, the
answer is 23, to change the order of precedence we use a parentheses (3+4)*5, now the
answer is 35. Precedence operator used in Python are (unary + - ~, **, * / %, + - , &) etc.
Type Description
Type Description
No source
Creates an array of size 0
(arguments)
INPUT:
OUTPUT:
c) Python bytearray()
The bytearray() method returns a bytearray object which is an array of the given bytes.
syntax :
bytearray([source[, encoding[, errors]]])
bytearray() method returns a bytearray object (i.e. array of bytes) which is mutable (can be
modified) sequence of integers in the range 0 <= x < 256.
bytearray() Parameters
No source
(arguments) Creates an array of size 0.
Return value from bytearray() bytearray() method returns an array of bytes of the
given size and initialization values.
OUTPUT:
We can add a single element using the add() method, and multiple elements using the update()
method. The update() method can take tuples, lists, strings or other sets as its argument. In all
cases, duplicates are avoided.
• Removing elements from a set
A particular item can be removed from a set using the methods discard() and remove().
The only difference between the two is that the discard() function leaves a set unchanged if
the element is not present in the set. On the other hand, the remove() function will raise an
error in such a condition (if element is not present in the set).
Similarly, we can remove and return an item using the pop() method.
Since set is an unordered data type, there is no way of determining which item will be
popped. It is completely arbitrary.
We can also remove all the items from a set using the clear() method.
Sets can be used to carry out mathematical set operations like union, intersection, difference
and symmetric difference. We can do this with operators or methods.
Program code
INPUT:
OUTPUT:
OUTPUT:
OUTPUT: