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

2operators, Operator Precedence, If, If Else, Elif Statements, Loops in Python - 1625071036620

Uploaded by

keed wild
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
27 views12 pages

2operators, Operator Precedence, If, If Else, Elif Statements, Loops in Python - 1625071036620

Uploaded by

keed wild
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

Bitwise Operators:

The bitwise operators perform bit by bit operation on the values of the two
operands. Consider the following example.

For Example:

if a = 7

b=3

then, binary (a) = 0111

binary (b) = 0011

hence, a & b = 0011

a | b = 0111

a ^ b = 0100

~ a = 1000

Logical Operators:
The logical operators are used primarily in the expression evaluation to make a
decision. Python supports the following logical operators.

Membership Operators:

Python membership operators are used to check the membership of value inside
a Python data structure. If the value is present in the data structure, then the
resulting value is true otherwise it returns false.

Identity Operators:

The identity operators are used to decide whether an element belongs to certain
class or type.

Two identity operators are available in Python:

 is – returns True if the type of the value in the right operand points to the
same type in the left operand. For example, type(3) is int evaluates to True
because 3 is indeed an integer number.

 is not – returns True if the type of the value in the right operand points to a
different type than the value in the left operand. For example, type(3) is not
float evaluates to True because 3 is not a floating-point value.
Operator precedence:

The precedence of the operators is essential to find out since it enables us to


know which operator should be evaluated first. The precedence table of the
operators in Python is given below.

If-else statements:

Decision making is the most important aspect of almost all the programming
languages. As the name implies, decision making allows us to run a particular
block of code for a particular decision. Here, the decisions are made on the
validity of the particular conditions. Condition checking is the backbone of
decision making.
In python, decision making is performed by the following statements.

if statement:

The if statement is used to test a particular condition and if the condition is true,
it executes a block of code known as if-block. The condition of if statement can be
any valid logical expression which can be either evaluated to true or false.
Syntax:

For Example:

Output:

The if-else statement:

The if-else statement provides an else block combined with the if statement
which is executed in the false case of the condition.

If the condition is true, then the if-block is executed. Otherwise, the else-block is
executed.
Syntax:

For Example:
Output:

The elif statement:

The elif statement enables us to check multiple conditions and execute the
specific block of statements depending upon the true condition among them. We
can have any number of elif statements in our program depending upon our
need.

The elif statement works like an if-else-if ladder statement in C.

Syntax:
For Example:
Output:

Python Loops:

The flow of the programs written in any programming language is sequential by


default. Sometimes we may need to alter the flow of the program. The execution
of a specific code may need to be repeated several numbers of times.

For this purpose, The programming languages provide various types of loops
which are capable of repeating some specific code several numbers of times.
Consider the following diagram to understand the working of a loop statement.
Why we use loops in python?

The looping simplifies the complex problems into the easy ones. It enables us to
alter the flow of the program so that instead of writing the same code again and
again, we can repeat the same code for a finite number of times. For example, if
we need to print the first 10 natural numbers then, instead of using the print
statement 10 times, we can print inside a loop which runs up to 10 iterations.

Advantages of loops

There are the following advantages of loops in Python.

1. It provides code re-usability.

2. Using loops, we do not need to write the same code again and again.

3. Using loops, we can traverse over the elements of data structures (array or
linked lists).

There are the following loop statements in Python.

You might also like