Python-Operators Compressed
Python-Operators Compressed
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Arithmetic operators
Arithmetic operators are used to perform mathematical operations like
addition, subtraction, multiplication and division.
Operator Description Syntax
+ Addition: adds two operands x+y
** Power : Proprietary
Returns content.
first© Great
raisedLearning. All Rights Reserved.
to power second Unauthorized use or distribution prohibited. x ** y
Arithmetic operators Examples
Input: a = 9, b = 4 Output:
• # Addition of numbers • 13
add = a + b
• # Subtraction of numbers • 5
sub = a - b
• # Multiplication of number • 36
mul = a * b
• # Division(float) of number • 2.25
div1 = a / b
• # Division(floor) of number • 2
div2 = a // b
• # Modulo of both number • 1
mod = a % b
• # Power • 6561
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
p = a ** b
Logical operators
Logical operators perform Logical AND, Logical OR and Logical
NOT operations.
| Bitwise OR x|y
~ Bitwise NOT ~x
• 40
• # print bitwise left shift operation
print(a << 2)
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Keywords
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
yield
• yield is used inside a function like a return statement. But yield returns a generator.
• Generator is an iterator that generates one item at a time. A large list of values will
take up a lot of memory. Generators are useful in this situation as it generates only
one value at a time instead of storing all the values in memory. For example,
g = (2**x for x in range(100))
will create a generator g which generates powers of 2 up to the number two raised to
the power 99. We can generate the numbers using the next() function as shown below.
next(g) Output:1
next(g) Output:2
next(g) Output:4
next(g) Output:8
next(g) Output:16
And so on… This type of generator is returned by the yield statement from a function.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Example of Yield
• def generator():
for i in range(6):
yield i*i
g = generator()
for i in g:
print(i)
Output
0
1
4
9
16
25
Here, the function generator() returns a generator that generates square of numbers from 0 to 5. This is printed in
the for loop.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
lambda
• lambda is used to create an anonymous function (function with no name). It is an
inline function that does not contain a return statement. It consists of an
expression that is evaluated and returned. For example:
a = lambda x: x*2
for i in range(1,6):
print(a(i))
Output
2
4
6
8
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
10
async, await
• The async and await keywords are provided by the asyncio library in
Python. They are used to write concurrent code in Python.
• For example:
import asyncio
async def main():
print('Hello')
await asyncio.sleep(1)
print('world')
• Here if there is an exception in the Try-block, it is handled in the except or else block. But no
matter in what order the execution flows, we can rest assured that the Finally-block is executed
even if there is an error. This is useful in cleaning up the resources.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
assert
• assert is used for debugging purposes.
• While programming, sometimes we wish to know the internal state or check if our
assumptions are true. assert helps us do this and find bugs more
conveniently. assert is followed by a condition.
• If the condition is true, nothing happens. But if the condition is
false, AssertionError is raised. For example:
a=4
assert a < 5
assert a > 5
Traceback (most recent call last): File "<string>", line 301, in runcode File "<interactive
input>", line 1, in <module> AssertionError
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Break and continue
• break and continue are used inside for and while loops to alter their
normal behavior.
• break will end the smallest loop it is in and control flows to the
statement immediately below the loop. continue causes to end the
current iteration of the loop, but not the whole loop.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Continue Break