0% found this document useful (0 votes)
4 views12 pages

DAY2

The document provides an overview of Python's bitwise operators, including AND, XOR, and right shift, along with examples of their usage. It also covers compound assignment operators, string indexing, slicing, and various conditional statements such as if, if-else, nested if, and multi-way if-elif-else statements. Additionally, it includes several assignments for practical application of these concepts.
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)
4 views12 pages

DAY2

The document provides an overview of Python's bitwise operators, including AND, XOR, and right shift, along with examples of their usage. It also covers compound assignment operators, string indexing, slicing, and various conditional statements such as if, if-else, nested if, and multi-way if-elif-else statements. Additionally, it includes several assignments for practical application of these concepts.
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/ 12

Institute of Engineering & Management

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.

The Bitwise AND (&) Operator


This operator performs AND operation on input bits of numbers. The Bitwise AND operator is
represented as ‘&’. The ‘&’ operator operates on two operands bit-by-bit. Table explains the AND
(&) operator.
Example: Write a program to read two numbers from the user. Display the result using bitwise |
operator on the numbers.

Output:

The Bitwise XOR (^) Operator:


This operator performs bitwise exclusive or XOR operation on the numbers. It is represented as ‘^’.
The ‘^’ operator also operates on two operands and these two operands are compared bit-by-bit.
Table 3.15. explains the ‘^’ (XOR) operator.
Example:Write a program to read two numbers from the user. Operate bitwise ^ operator on them
and display the result

Name: Roll No:


Department: Section:
Institute of Engineering & Management
Output:

The Right Shift (>>) Operator


The right shift operator is represented as >>. It also needs two operands. It is used to shift bits to the
right by n position. Working of the right shift operator (>>) is explained as follows
Example:

Explanation:

Example:Write a program to shift input data by four bits towards the left.

Output:

THE COMPOUND ASSIGNMENT OPERATOR:


The operators +, *, //, /, % and ** are used with the assignment operator (=) to form the compound
or augmented assignment operator.

Name: Roll No:


Department: Section:
Institute of Engineering & Management

Compound assignment operators:

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.

Accessing characters in a string using the index operator

Name: Roll No:


Department: Section:
Institute of Engineering & Management

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:

Conditional Statement in Python:Python supports various conditional statements. These are:


1. if statements
2. if-else statements
3. Nested if statements
4. Multi-way if-elif-else statements
1. if statements: The if statement executes a statement if a condition is true.The keyword if
begins the if statement. The condition is a Boolean expression which determines whether or not the
body of if block will be executed. A colon (:) must always be followed by the condition. The block

Name: Roll No:


Department: Section:
Institute of Engineering & Management
may contain one or more statements. The statement or statements are executed if and only if the
condition within the if statement is true. The flow chart for if statement is given below.

Flowchart for if statement


Example:

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.

Syntax for if-else statement

Name: Roll No:


Department: Section:
Institute of Engineering & Management

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:

Multi-way if-elif-else Statements:


The syntax for if-elif-else statements is given as follows:
if Boolean-expression1:
statement1
elif Boolean-expression2 :
statement2
elif Boolean-expression3 :
statement3
--------------
- - - - - - - - - - - -- -
elif Boolean-expression n :
statement N
else :
Statement(s)

Name: Roll No:


Department: Section:
Institute of Engineering & Management
In this kind of statements, the number of conditions, i.e. Boolean expressions are checked from top
to bottom. When a true condition is found, the statement associated with it is executed and the rest
of the conditional statements are skipped. If none of the conditions are found true then the last else
statement is executed. If all other conditions are false and if the final else statement is not present
then no action takes place.

Name: Roll No:


Department: Section:
Institute of Engineering & Management
Assignment1: Write a program to read two numbers from the user. Display the result using bitwise
| operator on the numbers.

Output:

Assignment2: Write a program to read two numbers from the user. Display the result using bitwise
‘&’,’~’ and ‘^’ operation on the numbers.

Output:

Name: Roll No:


Department: Section:
Institute of Engineering & Management
Assignment3: Write a program to traverse all the elements of a string.

Output:

Assignment4: Write a program using compound assignment operators to calculate the area of a
rectangle.

Output:

Name: Roll No:


Department: Section:
Institute of Engineering & Management
Assignment5: Write a program which prompts a user to enter the radius of a circle. If the radius is
greater than zero then calculate and print the area and circumference of the circle.

Output:

Name: Roll No:


Department: Section:
Institute of Engineering & Management
Assinment6: Write a program to find out the grade as per the percentage given in following the
table.

Output:

Name: Roll No:


Department: Section:
Institute of Engineering & Management
Assignment7: Write a program to calculate the salary of a medical representative considering the
sales bonus and incentives offered to him are based on the total sales. If the sales exceed or equal
to`1,00,000 follow the particulars of Column 1, else follow Column 2.

Output:

Name: Roll No:


Department: Section:

You might also like