0% found this document useful (0 votes)
19 views48 pages

Unit 1.1

Useful for students who have just started studying python it explains many concepts of python

Uploaded by

zafarshaikhn336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views48 pages

Unit 1.1

Useful for students who have just started studying python it explains many concepts of python

Uploaded by

zafarshaikhn336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Computer Programming Paradigm (Python)

(2601103)

Unit 1.1 Programs to Implement Operators:

Reference:

• Rao, R. N. (2009). Core Python programming (2nd ed.). Dreamtech Press


• Chun, W. (2007). Core Python programming (1st ed.). Pearson.
• Lutz, M. (2013). Learning Python (5th ed.). O'Reilly Media.
Comments in python
 The hash symbol ‘#’ represents the comment line in Python.
 A comment is useful to describe the elements of a program. Comments are not executed
by the Python compiler or PVM.
 Hence, it is not compulsory to write comments; however, comments improve
[Link]
understanding of a program.
Comments in python
 Multi line comments:
 Instead of starting every line with # symbol, we can write the block of code inside
""" (triple double quotes) or ''' (triple single quotes) in the beginning and ending of the
block as:

 The triple double quotes (""") or triple single quotes (''') are called ‘multi line comments’
or ‘block comments’. They are used to enclose a block of lines as comments.
Comments in python
 Multiline comments:

 In fact, Python supports only single line comments. Multiline comments are not
available in Python.

 The triple double quotes or triple single quotes are actually not multi line comments
but they are regular strings with the exception that they can span multiple lines.

 That means memory will be allocated to these strings internally. If these strings are
not assigned to any variable, then they are removed from memory by the garbage
collector and hence these can be used as comments.

 So, using """ are ''' or not recommended for comments by Python people since they
internally occupy memory and would waste time of the interpreter since the
interpreter has to check them.
OPERATORS IN PYTHON
 Operator
 An operator is a symbol that performs an operation. An operator acts on some variables
called operands.
 For example, if we write a + b, the operator ‘ + ‘ is acting on two operands ‘a’ and ‘b’.
 If an operator acts on a single variable, it is called unary operator.
 If an operator acts on two variables, it is called binary operator.
 If an operator acts on three variables, then it is called ternary operator.
 We can classify the operators depending upon their nature, as shown below:
a)  Arithmetic operators
b)  Assignment operators
c)  Unary minus operator
d)  Relational operators
e)  Logical operators
f)  Boolean operators
g)  Bitwise operators
h) Membership operators
i) Identity operators
OPERATORS IN PYTHON
 Operator
a) Arithmetic Operators
 These operators are used to perform basic arithmetic operations like addition,
subtraction, division, etc.
 There are seven arithmetic operators available in Python. Since these operators act
on two operands, they are called ‘binary operators’ also.
 Let’s assume a = 13 and b = 5
OPERATORS IN PYTHON
 Operator
a) Arithmetic Operators
 When there is an expression that contains several arithmetic operators, we should
know which operation is done first and which operation is done next.
 In such cases, the following order of evaluation is used:
1. First parentheses are evaluated.
2. Exponentiation (power of) is done next.
3. Multiplication(*), division(/), modulus(%) and floor divisions(//) are at equal
priority.
4. Addition and subtraction are done afterwards.
5. Finally, assignment operation is performed.
 Let’s take a sample expression: (Example 1) d = (x+y)*z**a//b+c.
 Assume the values of variables as: x=1; y=2; z=3; a=2; b=2; c=3.
OPERATORS IN PYTHON
 Operator
a) Arithmetic Operators
 Then, the given expression d = (1+2)*3**2//2+3 will evaluate as:
 1. First parentheses are evaluated. d = 3*3**2//2+3.
 2. Exponentiation is done next. D = 3*9//2+3.
 3. Multiplication, division, modulus and floor divisions are at equal priority. d =
 27//2+3 and then d = 13+3.
 4. Addition and subtraction are done afterwards. d = 16.
 5. Finally, assignment is performed. The value 16 is now stored into ‘d’.
 Hence, the total value of the expression becomes 16 which is stored in the
variable ‘d’.
OPERATORS IN PYTHON
 Operator
a) Arithmetic Operators
 Example 2: (m - n) * p**q // r + s
 M = 10,n = 3 ,p = 2 ,q = 3 ,r = 4 ,s = 5
 Result = 19
 Example 3: ((a + b)**c - d) * e // f + g % h
 A = 2,b = 3 ,c = 3 ,d = 10 ,e = 4 ,f = 5 ,g = 17 ,h = 6
 Result = 97
OPERATORS IN PYTHON
b) Assignment Operators
 These operators are useful to store the right side value into a left side variable.
 They can also be used to perform simple arithmetic operations like addition, subtraction, etc.,
and then store the result into a variable. Let’s assume the values x = 20, y = 10 and z = 5:
OPERATORS IN PYTHON
b) Assignment Operators
 Python does not have increment operator ( ++ ) and decrement operator ( -- ) that
are available in C and Java.
 It is possible to assign the same value to two variables in the same statement as:
 a=b=1
 print(a, b) # will display 1 1

 Another example is where we can assign different values to two variables as:
 a=1; b=2
 print(a, b) # will display 1 2

 The same can be done using the following statement:


 a, b = 1, 2
 print(a, b) # will display 1 2
OPERATORS IN PYTHON
c) Unary Minus Operator
 The unary minus operator is denoted by the symbol minus ( - ). When this
operator is used before a variable, its value is negated.
 That means if the variable value is positive, it will be converted into negative and
vice versa.
 For example, consider the following statements:
 n = 10
 print(-n) # displays -10

 num = -10
 num = - num
 print(num) # displays 10
OPERATORS IN PYTHON
d) Relational Operators
 Relational operators are used to compare two quantities. We can understand whether two
values are same or which one is bigger or which one is lesser, etc. using these operators.
 These operators will result in True or False depending on the values compared, we are
assuming a =1 and b = 2.
OPERATORS IN PYTHON
d) Relational Operators
 Relational operators can be chained. It means, a single expression can hold more
than one relational operator.
 Example1:
 x=15
 10<x<20 # displays True
 Here, 10 is less than 15 is True, and then 15 is less than 20 is True. Since both
the conditions are evaluated to True, the result will be True.

 Example 2:
 10>=x<20 # displays False
 Here, 10 is greater than or equal to 15 is False. But 15 is less than 20 is True.
Since we get False and True, the result will be False.
OPERATORS IN PYTHON
d) Relational Operators
 Example 3
 10<x>20 # displays False
 Here, 10 is less than 15 is True. But 15 is greater than 20 is False. Since we are
getting
 True and False, the total result will be False.
 So, the point is this: in the chain of relational operators, if we get all True, then
only the final result will be True. If any comparison yields False, then we get False
as the final result.
 Example 4
 1<2<3<4 # will give True
 1<2>3<4 # will give False
 4>2>=2>1 # will give True
OPERATORS IN PYTHON
e) Logical Operators
 Logical operators are useful to construct compound conditions.
 A compound condition is a combination of more than one simple condition.
 Each of the simple condition is evaluated to True or False and then the decision is
taken to know whether the total condition is True or False.
 False indicates 0 and True indicates any other number.
 There are 3 logical operators, Let’s take x = 1 and y=2 in this table.
OPERATORS IN PYTHON
e) Logical Operators
 Example 1
 x = 100
 y = 200
 print(x and y)# will display 200
 print(x or y)# will display 100
 print (not x) # will display False

 Example 2
 x=1; y=2; z=3
 if(x<y and y<z): print('Yes')
 else: print('No') Output: Yes

 Example 3
 x=1; y=2; z=3
 if(x>y or y<z): print('Yes')
 else: print('No') Output: Yes
OPERATORS IN PYTHON
f) Boolean Operators
 There are two ‘bool’ type literals. They are True and False. Boolean operators act
upon ‘bool’ type literals and they provide ‘bool’ type output.
 It means the result provided by Boolean operators will be again either True or
False. There are three Boolean operators, Let’s take x = True and y = False.
OPERATORS IN PYTHON
f) Boolean Operators
 Example: BOOLEAN AND Operator
 A = 10 , b = 5
 print(a > 5 and b < 10) # True and True → True

 Example 2 BOOLEAN AND Operator


 A = 0, b = 5
 print(a > 5 and b < 10) # False and True → False

 Example 3: BOOLEAN OR Operator


 A = 10, b = 15
 print(a > 5 or b < 10) # True or False → True

 Example 4: BOOLEAN NOT Operator


 x = True
 print(not x) # Output: False
OPERATORS IN PYTHON
g) Bitwise Operators
 These operators act on individual bits (0 and 1) of the operands. We can use
bitwise operators directly on binary numbers or on integers also.
 When we use these operators on integers, these numbers are converted into bits
(binary number system) and then bitwise operators act upon those bits.
 The results given by these operators are always in the form of integers.
 We use decimal number system in our daily life. This number system consists of
10 digits from 0 to 9.
 We count all numbers using these 10 digits only. But in case of binary number
system that is used by computers internally, there are only 2 digits, i.e. 0 and 1
which are called bits (binary digits).
 All values are represented only using these two bits. It is possible to convert a
decimal number into binary number and vice versa.
OPERATORS IN PYTHON
g) Bitwise Operators
 Example 1: Converting 45 into binary number
system.
 Rule: Divide the number successively by 2 and
take the remainders from bottom to top,
 The decimal number 45 is represented as 101101
in binary. If we use 8 bit representation, we can
write it as: 0010 1101.
#Decimal to binary
 num = 45
 binary = bin(num)
 print("Binary of", num, "is", binary)
OPERATORS IN PYTHON
g) Bitwise Operators
 Example 2: Converting binary number 0010 1101 into decimal number.
 Rule: Multiply the individual bits by the powers of 2 and take the sum of the
products,
 Here, the sum is coming to 45. So 0010 1101 in binary is equal to 45 in decimal
number system.
 #Convert Binary to decimal
 decimal_number = int('00101101', 2)
 print(decimal_number) # Output: 45
OPERATORS IN PYTHON
h) Bitwise Operators
 There are 6 types of bitwise operators as shown below:
1. Bitwise Complement operator ( ~ )
2. Bitwise AND operator (&)
3. Bitwise OR operator (|)
4. Bitwise XOR operator (^)
5. Bitwise Left shift operator (<<)
6. Bitwise Right shift operator (>>)
OPERATORS IN PYTHON
h) Bitwise Operators
1. Bitwise Complement operator ( ~ ):
 This operator gives the complement form of a given number. This operator symbol
is ~, which is pronounced as tilde.
 Complement form of a positive number can be obtained by changing 0’s as 1’s and
vice versa.
 The complement operation is performed by NOT gate circuit in electronics. Truth
table is a table that gives relationship between the inputs and the output. The truth
table is also given for NOT gate,
OPERATORS IN PYTHON
h) Bitwise Operators
1. Bitwise Complement operator ( ~ ):
 This operator gives the complement form of a given number. This operator symbol
is ~, which is pronounced as tilde.
 Complement form of a positive number can be obtained by changing 0’s as 1’s and
vice versa.
 The complement operation is performed by NOT gate circuit in electronics. Truth
table is a table that gives relationship between the inputs and the output. The truth
table is also given for NOT gate,
 Example :
 a=10 (in binary 0000 1010)
 print(~a) #display -11 (2’s complement 0000 1010 +1 = 0000 1011) ( a=−(a+1))
OPERATORS IN PYTHON
h) Bitwise Operators
[Link] AND Operator (&):
 This operator performs AND operation on the individual bits of numbers. The
symbol for this operator is &, which is called ampersand.
 To understand the bitwise AND operation see the truth table.

 From the truth table, we can conclude that by multiplying the input bits, we can
get the output bit. The AND gate circuit present in the computer chip will perform
the AND operation.
OPERATORS IN PYTHON
h) Bitwise Operators
[Link] AND Operator (&):
 Example:
 If x = 10, y = 11. Find the value of x&y.
 x = 10 = 0000 1010.
 Y = 11 = 0000 1011.
 From the truth table, by multiplying the bits, we can get x&y = 0000 1010. This is
nothing but 10 (in decimal).
OPERATORS IN PYTHON
h) Bitwise Operators
[Link] XOR Operator ( ^ ):
 This operator performs exclusive or (XOR) operation on the bits of numbers. The
symbol is ^, which is called cap, carat, or circumflex symbol.
 To understand the XOR operation, see the truth table.
 From the table, we can conclude that when we have odd number of 1’s in the input
bits, we can get the output bit as 1.
 The XOR gate circuit of the computer chip will perform this operation.
OPERATORS IN PYTHON
h) Bitwise Operators
[Link] XOR Operator ( ^ ):
 Example
 If x = 10, y = 11, find the value of x^y.
 x = 10 = 0000 1010.
 y = 11 = 0000 1011.
 From the truth table, when odd number of 1’s are there, we can get a 1 in the
output.
 Thus, x^y = 0000 0001 is nothing but 1 (in decimal).
OPERATORS IN PYTHON
h) Bitwise Operators
[Link] Left Shift Operator (<<):
 This operator shifts the bits of the number towards left a specified number of
positions.
 The symbol for this operator is <<, read as double less than. If we write x<<n, the
 meaning is to shift the bits of x towards left n positions.
 If x = 10, calculate x value if we write x<<2.
 Shifting the value of x towards left 2 positions will make the leftmost 2 bits to be
lost. The value of x is 10 = 0000 1010. Now, x<<2 will be 0010 1000 = 40 (in
decimal).
OPERATORS IN PYTHON
h) Bitwise Operators
[Link] Right Shift Operator (>>):
 This operator shifts the bits of the number towards right a specified number of
positions.
 The symbol for this operator is >>, read as double greater than. If we write x>>n,
the meaning is to shift the bits of x towards right n positions.
 >> shifts the bits towards right and also preserves the sign bit, which is the
leftmost bit.
 Sign bit represents the sign of the number. Sign bit 0 represents a positive number
and 1 represents a negative number.
 So, after performing >> operation on a positive number,
 we get a positive value in the result also. If right shifting is done on a negative
number, +again we get a negative value only.
OPERATORS IN PYTHON
h) Bitwise Operators
[Link] Right Shift Operator (>>):
 If x = 10, then calculate x>>2 value.
 Shifting the value of x towards right 2 positions will make the rightmost 2 bits to be
lost. x value is 10 = 0000 1010. Now x>>2 will be: 0000 0010 = 2 (in decimal)
OPERATORS IN PYTHON
i) Membership Operators
 The membership operators are useful to test for membership in a sequence such as
strings, lists, tuples or dictionaries.
 For example, if an element is found in the sequence or not can be asserted(declared)
using these operators.
 There are two membership operators as shown here:
1. in
2. not in

1. The in Operator:
 This operator returns True if an element is found in the specified sequence.
 If the element is not found in the sequence, then it returns False.
OPERATORS IN PYTHON
i) Membership Operators
1. The in Operator:
2. Example1
 names = ["Rani", "Yamini", "Sushmita", "Veena"]
 for name in names:

 print (name)

 Example2
 names = ["Rani", "Yamini", "Sushmita", "Veena"]
 print("Yamini" in names) #display True
 print("Alia" in names) #display False
OPERATORS IN PYTHON
i) Membership Operators
2. The not in Operator:
 This works in reverse manner for ‘in’ operator. This operator returns True if an
element is not found in the sequence. If the element is found, then it returns False.
 Example1
 colors = ["red", "green", "blue"]
 print("yellow" not in colors) # True
 print("red" not in colors) # False

 Example2
 sentence = "Python is fun"
 print("Java" not in sentence) # True
 print("Python" not in sentence) # False
OPERATORS IN PYTHON
f) Identity Operators
 These operators compare the memory locations of two objects. Hence, it is possible
to know whether the two objects are same or not.
 The memory location of an object can be seen using the id() function.
 This function returns an integer number, called the identity number that internally
represents the memory location of the object.
 a = 25
 b = 25
 id(a) #1670954952
 id(b) #1670954952
 a’ to the object 25, assigning another name ‘b’ to the same object 25.
 Here, 25 is the object for which two names are given. If we display an identity
number of these two variables, we will get same numbers as they refer to the same
object
OPERATORS IN PYTHON
f) Identity Operators
 id(one) #51792432
 id(two) #51607192
 both the lists are created at different memory locations, we have their identity
numbers different.
 So, ‘is’ operator will take the lists ‘one’ and ‘two’ as two different lists even though
their values are same.
OPERATORS IN PYTHON
f) Identity Operators
 There are two identity operators:
1. is
2. is not

1. The is Operator:
 The ‘is’ operator is useful to compare whether two objects are same or not. It will
internally compare the identity number of the objects. If the identity numbers of the
objects are same, it will return True; otherwise, it returns False.
 a = 25
 b = 25
 if(a is b):
 print("a and b have same identity")
 else:
 print("a and b do not have same identity") #o/p: a and b have same identity
OPERATORS IN PYTHON
f) Identity Operators
1. The is Operator:
 Example 2
 one = [1,2,3,4]
 two = [1,2,3,4]
 if(one is two):
 print("one and two are same")
 else:
 print("one and two are not same")
OPERATORS IN PYTHON
f) Identity Operators
2. The is not Operator:
 This is not operator returns True, if the identity numbers of two objects being
compared are not same. If they are same, then it will return False.
 The ‘is’ and ‘is not’ operators do not compare the values of the objects. They
compare the identity numbers or memory locations of the objects.
 If we want to compare the value of the objects, we should use equality operator
( == ).
 Example
 if(one == two):
 print("one and two are same")
 else:
 print("one and two are not same")
OPERATORS IN PYTHON
i) Operator Precedence and Associativity

The
sequence of
execution of the
operators is called
operator precedence.
OPERATORS IN PYTHON
i) Operator Precedence and Associativity
OPERATORS IN PYTHON
Mathematical Functions:
 In Python, a module is a file that contains a group of useful objects like functions,
classes or variables. ‘math’ is a module that contains several functions to perform
mathematical operations.
 If we want to use any module in our Python program, first we should import that
module into our program by writing ‘import’ statement.
 For example, to import math’ module (import math), Once this is done, we can use
any of the functions available in math module.
 E x a m p l e 1
x = [Link](16) #x’ value will become 4.0

 Example2
 import math as m
 x = [Link](16)
OPERATORS IN PYTHON
Mathematical Functions:

OPERATORS IN PYTHON
Mathematical
Functions:
OPERATORS IN PYTHON
Mathematical
Functions:
OPERATORS IN PYTHON
Mathematical
Functions:
OPERATORS IN PYTHON
Mathematical
Functions:

You might also like