Python Operators & Conditional Statements
Python Operators
The operator can be defined as a symbol which is responsible for a particular operation between two operands. Operators are the pillars of a program on which the logic is built in a particular programming language. Python provides a variety of operators described as follows.
- Arithmetic operators
- Comparison operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Arithmetic operators
Arithmetic operators are used to perform arithmetic operations between two operands. It includes +(addition), - (subtraction), *(multiplication), /(divide), %(reminder), //(floor division), and exponent (**).
Consider the following table for a detailed explanation of arithmetic operators.
Operator | Description |
---|---|
+ (Addition) | It is used to add two operands. For example, if a = 20, b = 10 => a+b = 30 |
- (Subtraction) | It is used to subtract the second operand from the first operand. If the first operand is less than the second operand, the value result negative. For example, if a = 20, b = 10 => a ? b = 10 |
/ (divide) | It returns the quotient after dividing the first operand by the second operand. For example, if a = 20, b = 10 => a/b = 2 |
* (Multiplication) | It is used to multiply one operand with the other. For example, if a = 20, b = 10 => a * b = 200 |
% (reminder) | It returns the reminder after dividing the first operand by the second operand. For example, if a = 20, b = 10 => a%b = 0 |
** (Exponent) | It is an exponent operator represented as it calculates the first operand power to second operand. |
// (Floor division) | It gives the floor value of the quotient produced by dividing the two operands. |
Comparison operator
Comparison operators are used to comparing the value of the two operands and returns boolean true or false accordingly. The comparison operators are described in the following table.
Operator | Description |
---|---|
== | If the value of two operands is equal, then the condition becomes true. |
!= | If the value of two operands is not equal then the condition becomes true. |
<= | If the first operand is less than or equal to the second operand, then the condition becomes true. |
>= | If the first operand is greater than or equal to the second operand, then the condition becomes true. |
<> | If the value of two operands is not equal, then the condition becomes true. |
> | If the first operand is greater than the second operand, then the condition becomes true. |
< | If the first operand is less than the second operand, then the condition becomes true. |
Python assignment operators
The assignment operators are used to assign the value of the right expression to the left operand. The assignment operators are described in the following table.
Operator | Description |
---|---|
= | It assigns the the value of the right expression to the left operand. |
+= | It increases the value of the left operand by the value of the right operand and assign the modified value back to left operand. For example, if a = 10, b = 20 => a+ = b will be equal to a = a+ b and therefore, a = 30. |
-= | It decreases the value of the left operand by the value of the right operand and assign the modified value back to left operand. For example, if a = 20, b = 10 => a- = b will be equal to a = a- b and therefore, a = 10. |
*= | It multiplies the value of the left operand by the value of the right operand and assign the modified value back to left operand. For example, if a = 10, b = 20 => a* = b will be equal to a = a* b and therefore, a = 200. |
%= | It divides the value of the left operand by the value of the right operand and assign the reminder back to left operand. For example, if a = 20, b = 10 => a % = b will be equal to a = a % b and therefore, a = 0. |
**= | a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16 to a. |
//= | A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3 = 1 to a. |
Bitwise operator
The bitwise operators perform bit by bit operation on the values of the two operands.
For example,
Operator | Description |
---|---|
& (binary and) | If both the bits at the same place in two operands are 1, then 1 is copied to the result. Otherwise, 0 is copied. |
| (binary or) | The resulting bit will be 0 if both the bits are zero otherwise the resulting bit will be 1. |
^ (binary xor) | The resulting bit will be 1 if both the bits are different otherwise the resulting bit will be 0. |
~ (negation) | It calculates the negation of each bit of the operand, i.e., if the bit is 0, the resulting bit will be 1 and vice versa. |
<< (left shift) | The left operand value is moved left by the number of bits present in the right operand. |
>> (right shift) | The left operand is moved right by the number of bits present in the right operand. |
Logical Operators
The logical operators are used primarily in the expression evaluation to make a decision. Python supports the following logical operators.
Operator | Description |
---|---|
and | If both the expression are true, then the condition will be true. If a and b are the two expressions, a → true, b → true => a and b → true. |
or | If one of the expressions is true, then the condition will be true. If a and b are the two expressions, a → true, b → false => a or b → true. |
not | If an expression a is true then not (a) will be false and vice versa. |
Membership Operators
Python membership operators are used to check the membership of value inside a data structure. If the value is present in the data structure, then the resulting value is true otherwise it returns false.
Operator | Description |
---|---|
in | It is evaluated to be true if the first operand is found in the second operand (list, tuple, or dictionary). |
not in | It is evaluated to be true if the first operand is not found in the second operand (list, tuple, or dictionary). |
Identity Operators
Operator | Description |
---|---|
is | It is evaluated to be true if the reference present at both sides point to the same object. |
is not | It is evaluated to be true if the reference present at both side do not point to the same object. |
Python 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.
Statement | Description |
---|---|
If Statement | The if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed. |
If - else Statement | The if-else statement is similar to if statement except the fact that, it also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed. |
Nested if Statement | Nested if statements enable us to use if ? else statement inside an outer if statement. |
Indentation in Python
For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for the block level code. In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block.
Generally, four spaces are given to indent the statements which are a typical amount of indentation in python.
Indentation is the most used part of the python language since it declares the block of code. All the statements of one block are intended at the same level indentation. We will see how the actual indentation takes place in decision making and other stuff in python.
The 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.
The syntax of the if-statement is given below.
Example 1
Output:
enter the number?10 Number is even
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.
The syntax of the if-else statement is given below.
Example 1 : Program to check whether a person is eligible to vote or not.
Output:
Enter your age? 90 You are eligible to vote !!
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. However, using elif is optional.
The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.
The syntax of the elif statement is given below.
Example 1
Output:
Enter the number?15 number is not equal to 10, 50 or 100
Comments
Post a Comment