DAY2
DAY2
BITWISE OPERATOR
Python has six bitwise operators for bitwise manipulation. The bitwise operator permits a
programmer to access and manipulate individual bits within a piece of data. Table shows various
bitwise operators supported by Python.
Output:
Explanation:
Example:Write a program to shift input data by four bits towards the left.
Output:
Example:Write a program using compound assignment operators to calculate the area of a circle.
Output:
THE index[] OPERATOR: Through Index[] operator the character of a string can be access one
at a time.The characters in a string are zero based, i.e. the first character of the string is stored at the
0th position and the last character of the string is stored at a position one less than that of the length
of the string.Following figure illustrate it.
Example:
S="INDIA"
S[0] #Access the first element of the string.
Output: I
S="INDIA"
S[4] #Access the last element of the string.
Output: A
Example:Write a program to traverse all the elements of a string
Output: India
Slicing: Slicing is the extraction of a part of a string, list, or tuple. It enables user to access the
specific range of elements by mentioning their indices.
Output:
Output:
In the above program, the two numbers are provided by a user. The statement within
the if block is executed if and only if the Boolean expression num1 – num2 evaluates to True.
If-Else Statement: The if-else statement is used to execute both the true part and the false part of a
given condition. If the condition is true, the if block code is executed and if the condition is false,
the else block code is executed.
Nested if Statements
When a programmer writes one if statement inside another if statement then it is called a nested if
statement. The syntax is given below.
if Boolean-expression1:
if Boolean-expression2:
statement1
else:
statement2
else:
statement3
Example:Write a program to read three numbers from a user and check if the first number is
greater or less than the other two numbers
Output:
Output:
Assignment2: Write a program to read two numbers from the user. Display the result using bitwise
‘&’,’~’ and ‘^’ operation on the numbers.
Output:
Output:
Assignment4: Write a program using compound assignment operators to calculate the area of a
rectangle.
Output:
Output:
Output:
Output: