0% found this document useful (0 votes)
44 views

L3:Python

This document discusses various operators in Python programming including mathematical, relational, logical, bitwise, membership, and identity operators. It provides examples of using each type of operator and notes that Python does not use ++ and -- increment/decrement operators like other languages, but instead uses augmented assignment operators.

Uploaded by

Mayur Kinra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

L3:Python

This document discusses various operators in Python programming including mathematical, relational, logical, bitwise, membership, and identity operators. It provides examples of using each type of operator and notes that Python does not use ++ and -- increment/decrement operators like other languages, but instead uses augmented assignment operators.

Uploaded by

Mayur Kinra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer Programming with Python

01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 1


Built-in Library Functions

01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 2


Mathematical Functions
We need to import math module to use these functions
sqrt(x)
ceil(x)
factorial(x)
floor(x)
gcd(a, b)
log(x,b)
log10(x)
sin(x)
pi
e
There are many more
https://docs.python.org/3/library/math.html
02/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 3
Relational/Comparison Operators

01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 4


Logical/Boolean Operators

01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 5


Compound/Augmented Assignment/Shorthand operators

01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 6


++ and --​ operators in Python?

There are no ++ and – operators(increment and decrement operators) in


Python.

Such operators creates compiler dependency issue and associativity of


operators issue in C/Java. Python is clever by avoiding use of ++ and -- in this
sense. For example, x++ can be written as x += 1 and x-- can be written as x -
= 1.

7
01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University
Bitwise Operators

Operator Meaning Example

& Bitwise AND x& y = 0 (0000 0000)

| Bitwise OR x | y = 14 (0000 1110)

~ Bitwise NOT ~x = -11 (1111 0101)

^ Bitwise XOR x ^ y = 14 (0000 1110)

>> Bitwise right shift x>> 2 = 2 (0000 0010)

<< Bitwise left shift x<< 2 = 40 (0010 1000)

8
01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University
Bitwise Operators

1] Write a Python program to test whether your input integer is power


of 2 or not using bitwise operator(s) and without using arithmetic
operators.

2] Given two positive integers n and m. Write a Python program to check


whether n is divisible by 2m or not using bitwise operator(s) and without
using arithmetic operators. For example,
Input : n = 40, m = 3
Output : Yes
Input : n = 40, m = 4
Output : No
9
01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University
Membership Operators
Used to test whether a value is found within a sequence. For example, you can use the membership
operators to test for the presence of a substring in a string, lists, or tuples. Two membership operators
exist in Python:
in – evaluates to True if the value in the left operand appears in the sequence found in the right operand.
not in – evaluates to True if the value in the left operand doesn’t appear in the sequence found in the
right operand.
>>> 'on' in 'Hello Python'
True
>>> 'ton' in 'Hello Python'
False
>>> 'On' in 'Hello Python'
False
>>> 'on' not in 'Hello Python'
False
>>> 'ton' not in 'Hello Python'
10
01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University
True
Membership Operators
>>> 30 in [10,20,30,40,50]
True
>>> 35 in [10,20,30,40,50]
False
>>> 30 not in [10,20,30,40,50]
False
>>> 35 not in [10,20,30,40,50]
True
>>> 'toe' in ('tic', 'tac', 'toe', 50, 20.5)
True
>>> 'toe' not in ('tic', 'tac', 'toe', 50, 20.5)
False
>>> 'too' in ('tic', 'tac', 'toe', 50, 20.5)
False
>>> 'too' not in ('tic', 'tac', 'toe', 50, 20.5)
11
01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University
True
Identity Operators
The identity operators in Python are used to determine whether a value is of a certain type. They are usually used
to determine the type of data a certain variable contains. 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.
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.
>>> x=5
>>> type(x) is int
True
>>> type(x) is float
False
>>> type(x) is not int
False
>>> type(x) is not float
True
>>> y = 3.25
>>> type(y) is not float
False
>>> type(y) is int
False 01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University
12
Ternary/conditional Operator

Like C,C++/Java there is no such ?: operator in Python

>>> a,b=10,20
>>> min=a if a<b else b
>>> print(min)
10

01/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 13

You might also like