Python - revision-(3)+IF
Good Morning Boys.
Lets take a look at the topic of the day "Python Operators"
The Google meet ID for today is
Lets take a look at the topic of the day "Python Operators"
The Google meet ID for today is
-----------------------------------------------------------------
By the end of this session, you will be able to take a recap of the concepts taught earlier and would be able to:
Operators are used to perform operations on variables and values.
By the end of this session, you will be able to take a recap of the concepts taught earlier and would be able to:
- Identify the various operators available in Python
- Understand and apply the various
- arithmetic operators in Python.
- Assignment operators.
- relational operators
- Logical operators
Operators are used to perform operations on variables and values.
Python divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators*
- Membership operators*
- Bitwise operators*
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations. Assume variable a holds 10 and variable b holds 20, then −
| Operator | Description | Example |
|---|---|---|
| + Addition | Adds values on either side of the operator. | a + b = 30 |
| - Subtraction | Subtracts right hand operand from left hand operand. | a – b = -10 |
| * Multiplication | Multiplies values on either side of the operator | a * b = 200 |
| / Division | Divides left hand operand by right hand operand | b / a = 2 |
| % Modulus | Divides left hand operand by right hand operand and returns remainder | b % a = 0 |
| ** Exponent | Performs exponential (power) calculation on operators | a**b =10 to the power 20 |
| // | Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity) − |
Let us focus on the following example ( Note the output. If you dont understand, share your doubts in the comment section.)
Example #1: Arithmetic operators in Python
x = 15
y = 4
# Output: x + y = 19
print('x + y =',x+y)
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x/y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: x ** y = 50625
print('x ** y =',x**y)
Please read the description and example carefully
Assume variable a holds 10 and variable b holds 20, then −
a+=2 is using the shorthand operator. Normal assignment would be a=a+2. So, "+=" is the shorthand operator.
Operator
|
Description
|
Example
|
=
|
Assigns values from right side operands to left side operand
|
c = a + b assigns value of a + b into c
|
+= Add AND
|
It adds right operand to the left operand and assign the result to left operand
|
c += a is equivalent to c = c + a
|
-= Subtract AND
|
It subtracts right operand from the left operand and assign the result to left operand
|
c -= a is equivalent to c = c - a
|
*= Multiply AND
|
It multiplies right operand with the left operand and assign the result to left operand
|
c *= a is equivalent to c = c * a
|
/= Divide AND
|
It divides left operand with the right operand and assign the result to left operand
|
c /= a is equivalent to c = c / a
|
%= Modulus AND
|
It takes modulus using two operands and assign the result to left operand
|
c %= a is equivalent to c = c % a
|
**= Exponent AND
|
Performs exponential (power) calculation on operators and assign value to the left operand
|
c **= a is equivalent to c = c ** a
|
//= Floor Division
|
It performs floor division on operators and assign value to the left operand
|
c //= a is equivalent to c = c // a
|
- The assignment operators are used frequently when we are doing programming.
- New values are created using assignment operators, taking the inputs from the user.
- Assignment statements will use variables and constants as "OPERANDS" and arithmetic operators(+,-,*,/,// or %) as "OPERATORS"
Python Comparison Operators
- These operators compare the values on either sides of them and decide the relation among them.
- They are also called Relational operators.
Assume variable a holds 10 and variable b holds 20, then −
Operator
|
Description
|
Example
|
==
|
If the values of two operands are equal, then the condition becomes true.
|
(a == b) is not true.
|
!=
|
If values of two operands are not equal, then condition becomes true.
|
(a != b) is true.
|
<>
|
If values of two operands are not equal, then condition becomes true.
|
(a <> b) is true. This is similar to != operator.
|
>
|
If the value of left operand is greater than the value of right operand, then condition becomes true.
|
(a > b) is not true.
|
<
|
If the value of left operand is less than the value of right operand, then condition becomes true.
|
(a < b) is true.
|
>=
|
If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.
|
(a >= b) is not true.
|
<=
|
If the value of left operand is less than or equal to the value of right operand, then condition becomes true.
|
(a <= b) is true.
|
Example #2: Comparison operators in Python
x = 10
y = 12
# Output: x > y is False
print('x > y is',x>y)
# Output: x < y is True
print('x < y is',x<y)
# Output: x == y is False
print('x == y is',x==y)
# Output: x != y is True
print('x != y is',x!=y)
# Output: x >= y is False
print('x >= y is',x>=y)
# Output: x <= y is True
print('x <= y is',x<=y)
Python Logical Operators
There are following logical operators supported by Python language. Assume variable a holds 10 and variable b holds 20 then
| Operator | Description | Example |
|---|---|---|
| and Logical AND | If both the operands are true then condition becomes true. | (a and b) is true. |
| or Logical OR | If any of the two operands are non-zero then condition becomes true. | (a or b) is true. |
| not Logical NOT | Used to reverse the logical state of its operand. | Not(a and b) is false. |
Logical operators are used to combine conditional statements:
(Try and predict the result in the following examples. To check whether you are right or not , click on "Try it" and see the result)
Logical operators are used to combine conditional statements:
(Try and predict the result in the following examples. To check whether you are right or not , click on "Try it" and see the result)
| Operator | Description | Example | Try it |
|---|---|---|---|
| and | Returns True if both statements are true | x < 5 and x < 10 | |
| or | Returns True if one of the statements is true | x < 5 or x < 4 | |
| not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
Example #1: Logical Operators in Python
x = True
y = False
# Output: x and y is False
print('x and y is',x and y)
# Output: x or y is True
print('x or y is',x or y)
# Output: not x is False
print('not x is',not x)
x = True
y = False
# Output: x and y is False
print('x and y is',x and y)
# Output: x or y is True
print('x or y is',x or y)
# Output: not x is False
print('not x is',not x)Python Operators Precedence
The following table lists all operators from highest precedence to lowest.
| Sr.No. | Operator & Description |
|---|---|
| 1 |
**
Exponentiation (raise to the power)
|
| 2 |
~ + -
Complement, unary plus and minus (method names for the last two are +@ and -@)
|
| 3 |
* / % //
Multiply, divide, modulo and floor division
|
| 4 |
+ -
Addition and subtraction
|
| 5 |
>> <<
Right and left bitwise shift
|
| 6 |
&
Bitwise 'AND'
|
| 7 |
^ |
Bitwise exclusive `OR' and regular `OR'
|
| 8 |
<= < > >=
Comparison operators
|
| 9 |
<> == !=
Equality operators
|
| 10 |
= %= /= //= -= += *= **=
Assignment operators
|
| 11 |
is is not
Identity operators
|
| 12 |
in not in
Membership operators
|
| 13 |
not or and
Logical operators
|
EXERCISE
Write a program in Python to accept percentage marks obtained by a student and print his grade depending on the following conditions:
if percentage>=90, Grade--> A
if percentage<90 but >=75, Grade--> B
if percentage<75 but >=60, Grade--> C
if percentage<60 but >=45, Grade--> D
if percentage<45 but >33, Grade--> E
else Grade-->F
Good morning mam
ReplyDeleteGood mornig mam
ReplyDeleteAshish
Good Morning Maam
ReplyDeleteGood morning mam
ReplyDeletegood morning ma'am
ReplyDelete-Artham
Good morning ma'am
ReplyDeleteRahul
Good morning ma'am
ReplyDelete-Geet