2operators, Operator Precedence, If, If Else, Elif Statements, Loops in Python - 1625071036620
2operators, Operator Precedence, If, If Else, Elif Statements, Loops in Python - 1625071036620
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
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.
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:
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 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 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.
Syntax:
For Example:
Output:
Python Loops:
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
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).