Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Python Prog--phase 2 Doc 2

Uploaded by

navneet sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Prog--phase 2 Doc 2

Uploaded by

navneet sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

PHASE 2

CHAPTER 3 ---- INTRODUCTION TO PYTHON


PROGRAMMING

LECTURE – 26

Number of Approaches to develop Python Programs


=>In Python Programming, we have 2 Approaches to develop Python Programs. They are

1. By Using Interactive Approach

2. By using Batch Mode Approach

1. By Using Interactive Approach

=>In This Mode of development, Python Programmer can write a single statement and immediately
Python Environment gives Result.

=>Interactive Approach is useful to test one instruction at a time.

=>In this Mode of Development, we are unable to re-use the code in other part of the Program bcoz
Interactive Approach is unable to provide to save the code. So Interactive Approach is not
recommended to develop Real Time applications.

=>Examples

>>> a=10

>>> b=20

>>> c=a+b

>>> print(a)-----------10

>>> print(b)-----------20

>>> print(c)-----------30

Example Software: Python Command

Python IDLE Shell ( Comes along python Software Installation)


2. By using Batch Mode Approach

=>In this mode development, Python Programmer writes batch OR group of statements, Save those

statements on Some name with an extension .py (called File Name.py).

=>In Real Time applications, we use Batch Mode approach only because It allows us to save the
Batch of statements / Instructions on Some File Name and we can re-use those statements in other
part of the Project.

Example: sum.py <----File Name (Program)

a=10

b=20

c=a+b

print(a)

print(b)

print(c)

Definition of Program: A Program is a Collection Optimized Instructions.

=>Every Python Program must be given some File Name with an extension .py

=>Hence with Batch Mode approach, we develop Programs where as with Interactive approach we
test one instruction at a time.

=>We always develop Programs by using Batch Mode approach for Solving Real Time Problems.

=>To develop Programs in bacth Mode approach, we need to use IDEs ( Interactive Development
Environment).

=>The IDEs for Developing Python Programs are

1. PYTHON IDLE Shell ( Comes along python Software Installation)

2. Edit Plus

3. PyCharm

4. Anakonda Jupiter Note Book

5. Anakonda Spider

6. VS Code

7. Google Clab

8. Atom

9. Sublime Text.....etc

=>From IDES (2) to (9) are not comming along python Software Installation and They must be
installed Explicitly.
Programs on Batch Mode

addopex1.py

#Program for adding Two Numbers

a=10
b=20
c=a+b
print(a)
print(b)
print(c)

addopex2.py

#program for adding two numbers

a=10
b=20
c=a+b
print("--------------------------")
print("Val of a=",a)
print("Val of b=",b)
print("Sum=",c)
print("--------------------------")

addopex3.py

#Program for addition of Two Numebrs

a=float(input("Enter Value of a:"))


b=float(input("Enter Value of b:"))
c=a+b
print("--------------------------")
print("Val of a=",a)
print("Val of b=",b)
print("Sum=",c)
print("--------------------------")

MulOpex1.py

#Program for Mul of Two Numbers

a=float(input("Enter Value of a:"))


b=float(input("Enter Value of b:"))
c=a*b
print("==========================")
print("Val of a=",a)
print("Val of b=",b)
print("Mul=",c)
print("==========================")
LECTURE – 27

Displaying the Result of Python Program on the console


=>To Display the Result of Python Program on the console, we use print()
=>In Otherwords, print() is one of the pre-defined function is used Displaying the Result of Python
Program on the console.
=>we can use print() with the following syntaxes

Syntax-1 : print(Value)
(OR)
print(Value1,Value2,....Value-n)

=>With this syntax we can display either single or Multiple Values


Examples:

>>> a=10
>>> print(a) 10
>>> a=12.34
>>> print(a) 12.34
>>> a=10
>>> b=2
>>> c=a+b
>>> print(a,b,c) 10 2 12

Syntax-2: print(Msg)
(OR)
print(Msg1,Msg2,...Msg-n)
(OR)
print(Msg1+Msg2+....+Msg-n)

=>Here Msg1,Msg2...Msg-n are Str type values


=>This Syntax displays Str Values

Examples

>>> print("Hello Python World") Hello Python World


>>> print("Hello" ) Hello
>>> print("Hello","Python","World") Hello Python World
>>> print("Hello"+"Python"+"World") HelloPythonWorld
>>> print("Hello"+" "+"Python"+" "+"World") Hello Python World
>>> print("Hello"+10) TypeError: can only concatenate str (not "int") to str
>>> print("Hello"+str(10)) Hello10
>>> print("Hello"+"10") Hello10
>>> print("1"+"2"+"3") 123
>>> print(str(1)+str(2)+"3") 123
>>> print(1+2+3) 6
>>> print(1+2+"3") TypeError: unsupported operand type(s) for +: 'int' and 'str'

Syntax-3: print(Messages cum Values)


(OR)
print(Values Cum Message)

=>This Syntax generates / Displays Message Cum Values


Examples:

>>> a=10
>>> print('Value of a=',a) Value of a= 10
>>> print(a," is the value a") 10 is the value a
>>> name="Rossum"
>>> print("Name of Person=",name) Name of Person= Rossum
>>> print(name," is the name of the Person") Rossum is the name of the Person

>>> a=10
>>> b=20
>>> c=a+b
>>> print(c," is the sum") 30 is the sum
>>> print("sum=",c) sum= 30
>>> print("Sum of ",a," and ",b,"=",c) Sum of 10 and 20 = 30

Syntax-4: print(Messages Cum Values with format())

=>This Syntax generates / Displays Message Cum Values by using format()


Examples:

>>> a=10
>>> print("Value of a={}".format(a)) Value of a=10
>>> print("{} is the value of a".format(a)) 10 is the value of a

>>> a=100
>>> b=200
>>> c=a+b
>>> print("Sum of {} and {}={}".format(a,b,c)) Sum of 100 and 200=300

>>> a=10
>>> b=20
>>> c=30
>>> d=a+b+c
>>> print("sum of ",a,",",b," and ",c,"=",d) sum of 10 , 20 and 30 = 60
>>> print("Sum of {},{} and {}={}".format(a,b,c,d)) Sum of 10,20 and 30=60

Syntax-5: print(Messages Cum Values with format specifiers

=>This Syntax generates / Displays Message Cum Values by using format specifiers
=>In Python Programming, For Displaying Integer Data, we use %d format specifier

For Displaying Float Data, we use %f format specifier.


For Displaying str Data, we use %s format specifier.

=>Note That, if any value / data does not contain format specifier then we must convert that values /
data into str type by using str()

Examples:

>>> a=10
>>> print("Value of a=%d" %a) Value of a=10
>>> print("%d is the value a" %a) 10 is the value a

>>> a=10
>>> b=20
>>> c=a+b
>>> print("Sum of %d and %d = %d" %(a,b,c)) Sum of 10 and 20 = 30

>>> a=10
>>> b=2.3
>>> c=a+b
>>> print("sum of %d and %f=%f" %(a,b,c)) sum of 10 and 2.300000=12.300000
>>> print("sum of %d and %0.2f=%0.3f" %(a,b,c)) sum of 10 and 2.30=12.300
>>> print("sum of %0.2f and %0.2f=%0.3f" %(a,b,c)) sum of 10.00 and 2.30=12.300
>>> sno=10
>>> sname="Rossum"
>>> marks=22.22
>>> print("My Number is %d and Name is %s and marks=%0.2f" %(sno,sname,marks))
My Number is 10 and Name is Rossum and marks=22.22

>>> print("My Number is %d and Name is '%s' and marks=%0.2f" %(sno,sname,marks))


My Number is 10 and Name is 'Rossum' and marks=22.22

>>> print("My Number is {} and Name is '{}' and marks={}".format(sno,sname,marks))


My Number is 10 and Name is 'Rossum' and marks=22.22

>>> a=12.23
>>> b=34.567
>>> print("a=%d and b=%d" %(a,b)) a=12 and b=34

>>> lst=[10,"Rossum",33.34,True,2+3j]

>>> print("Content of lst=",lst) Content of lst= [10, 'Rossum', 33.34, True, (2+3j)]
>>> print("Content of lst={}".format(lst)) Content of lst=[10, 'Rossum', 33.34, True, (2+3j)]
>>> print("Content of lst=%d" %lst) TypeError: %d format: a real number is required, not list
>>> print("Content of lst=%s" %str(lst)) Content of lst=[10, 'Rossum', 33.34, True, (2+3j)]

>>> d={10:"Apple",20:"Mango"}

>>> print("Content of d=",d) Content of d= {10: 'Apple', 20: 'Mango'}


>>> print("Content of d={}".format(d)) Content of d={10: 'Apple', 20: 'Mango'}
>>> print("Content of d=%s" %str(d)) Content of d={10: 'Apple', 20: 'Mango'}

Syntax-6: print(Values, end= )

=>This Syntax display the values Horizontally (Same Line) end with specified Symbols

Examples:

>>> r=range(10,21)

>>> print(r,type(r))
range(10, 21) <class 'range'>
>>> for v in r:
... print(v)
...
10
11
12
13
14
15
16
17
18
19
20
>>> for v in r:
... print(v,end="->")-----10->11->12->13->14->15->16->17->18->19->20->
>>> lst=[10,"Rossum",33.34,True,2+3j]

>>> for val in lst:


... print(val)
...
10
Rossum
33.34
True
(2+3j)
>>> for val in lst:
... print(val,end=" ") 10 Rossum 33.34 True (2+3j)

Special Point:

>>> s="python"

>>> print(s,type(s)) python <class 'str'>


>>> s=s+s+s
>>> print(s,type(s) ) pythonpythonpython <class 'str'>

>>> s="python"

>>> print(s,type(s)) python <class 'str'>


>>> s=s*3 # here * operator is called Repetation Operator
>>> print(s,type(s)) pythonpythonpython <class 'str'>

>>> lst=[10,"Rossum"]

>>> print(lst) [10, 'Rossum']


>>> print(3*lst) [10, 'Rossum', 10, 'Rossum', 10, 'Rossum']
>>> print("=") =
>>> print("="*5) =====
>>> print("="*20) ====================
>>> print("*"*50) **************************************************

>>> print(12*3) # Here * is called Mul Operator 36


>>> print("12"*"3") TypeError: can't multiply sequence by non-int of type 'str'
>>> print("12"*3) 121212
>>> print("K"+"V"*3) KVVV

>>> print("*"*5) *****


>>> print("*\n"*5)
*
*
*
*
*

>>> for val in range(1,6):


... print("*")
...
*
*
*
*
>>> for val in range(1,6):
... print("*",end=" ") *****
LECTURE – 28
Reading the Data OR Input Dynamically from Key Board
=>To read the Data OR Input Dynamically from Key Board, we have 2 pre-defined Functions. They
are
1. input()
2. input(message)

1. input()

Syntax:- varname=input()

=>here input() is used for Reading the any type of data in the form of str from Key Board and place
that value in LHS Varname.
=>Programatically, we can convert str type data into any possible data type value by using Typcasting
Techniques.

2. input(message)

Syntax:- varname=input(Message)

=>Here Message represents any string data and It is one of the User-Prompting Message
=>here input(Message) is used for Reading the any type of data in the form of str from Key Board and
place that value in LHS Varname along with It also gives User-Prompting Message.
=>Programatically, we can convert str type data into any possible data type value by using Typcasting
Techniques.

#program for accepting a values and displayt it on console


#InputEx1.py

print("Enter Value of a:")


a=input()
print(a,type(a))

#program for accepting a values and displayt it on console


#InputEx2.py

a=input("Enter Value of a: ")


print(a,type(a))

#program for accepting Two Numerical Values from KBD and Multiply them
#MulEx1.py

s1=input("Enter First Value:")


s2=input("Enter Second Value:")
print(s1,type(s1))
print(s2,type(s2))
#s3=s1*s2---TypeError

#Convert s1 and s2 into float type--float()

a=float(s1)
b=float(s2)
print(a,type(a))
print(b,type(b))
c=a*b
print("="*50)
print("value of a={}".format(a))
print("value of b={}".format(b))
print("Mul={}".format(c))
print("="*50)

#program for accepting Two Numerical Values from KBD and Multiply them
#MulEx2.py

a=float( input("Enter First Value:") )


b=float(input("Enter Second Value:"))
c=a*b
print("="*50)
print("value of a={}".format(a))
print("value of b={}".format(b))
print("Mul={}".format(c))
print("="*50)

#program for accepting Two Numerical Values from KBD and Multiply them
#MulEx3.py

print("Enter Two Values")


a=float( input() )
b=float( input() )
c=a*b
print("="*50)
print("value of a={}".format(a))
print("value of b={}".format(b))
print("Mul={}".format(c))
print("="*50)

#program for accepting Two Numerical Values from KBD and Multiply them
#MulEx4.py

print("Enter Two Values")


c=float(input())*float(input())
print("Mul={}".format(c))

#program for accepting Two Numerical Values from KBD and Multiply them
#MulEx5.py

print("Enter Two Values")


print("Mul={}".format(float(input())*float(input())))

#program for accepting Two Numerical Values from KBD and Multiply them
#MulEx6.py
print("Mul={}".format(float(input("Enter First Value:"))*float(input("Enter Second Value:"))))
#Program for cal area and Perimeter of Rectangle
#RectAreaPeri.py

l=float(input("Enter Length:"))
b=float(input("Enter Breadth:"))
#cal Area of Rect
area=l*b
#cal Perimeter
peri=2*(l+b)
#display result
print("*"*40)
print("\tLength=%0.2f" %l)
print("\tBreadth=%0.2f" %b)
print("\tArea of Rectangle=%0.2f" %area)
print("\tPerimter of Rectangle=%0.2f" %peri)
print("*"*40)

#program for cal Area and Perimter of Circle


#CircleAreaPeri.py

rad=float(input("Enter Radius:"))
ac=3.14*rad*rad
pc=2*3.14*rad
print("*"*50)
print("\t\tRadious=%0.2f" %rad)
print("\t\tArea of Circle=%0.2f"%ac)
print("\t\tPerimeter of Circle=%0.2f" %pc)
print("*"*50)

LECTURE – 29
Operators and Expressions in Python

=>An Operator is a symbol, which is used for Performing Certain Operation on given Object Data /
Values and gives Result.
=>If two or More Objects OR Values Connected with an Operator then it is called Expression.
=>In otherwords, An Expression is a collection of Objects / Values Connected with an Operator.
=>In Python Programming, we have 7 Operators. They are

1. Arithmetic Operators
2. Assigment Operator
3. Relational Operators ( Comparision Operators)
4. Logical Operators( Comparision Operators )
5. Bitwise Operators (Most Imp)
6. Membership Operators
a) in operator
b) not in operator
7. Identity Operators
a) is operator
b) is not operator

NOTE: Python does not Support Unary Operators Like ++ and - -


NOTE: Python does not Support Ternary Operator in C,C++,Java, C#.net ( ? : )
NOTE: Python Supports Short hand Operators
NOTE: Python is having is its own Ternary Operator------- if..else operator
1. Arithmetic Operators

=>The purpose of Arithmetic Operators is that "To Perform Arithmetic Operations such as
Addition,Substraction, Multiplications etc..".
=>If two or More Objects OR Values Connected with Arithmetic Operators then we call it as Arithmetic
Expression.
=>Python Programming Contains 7 Arithmetic Operators. They are given in the following Table

SLNO SYMBOL MEANING ( a=10, b=3) EXAMPLE

1. + Addition print(a+b)---->13

2. - Substraction print(a-b)----->7

3. * Multiplication print(a*b)----->30

4. / Division print(a/b)------>3.3333333333333335
(float Quotient)

5. // Floor Division print(a//b)------>3


(Integer Quotient)

6. % Modulo Division print(a%b)------>1


(Remainder)

7. ** Exponentiation print(a**b)------>1000

NOTE:

>>> print(10.0/3.0) 3.3333333333333335


>>> print(10.0//3.0) 3.0

>>> print(10.0/3) 3.3333333333333335


>>> print(10.0//3) 3.0

>>> print(10/3.0) 3.3333333333333335


>>> print(10//3.0) 3.0

>>> print(10/3) 3.3333333333333335


>>> print(10//3) 3

2. Assigment Operator

=>The purpose of assignment operator is that " To assign or transfer Right Hand Side (RHS) Value /
Expression Value to the Left Hand Side (LHS) Variable "
=>The Symbol for Assigment Operator is single equal to ( = ).
=>In Python Programming,we can use Assigment Operator in two ways.
1. Single Line Assigment
2. Multi Line Assigment
1. Single Line Assigment:

=>Syntax: LHS Varname= RHS Value


LHS Varname= RHS Expression

=>With Single Line Assigment at a time we can assign one RHS Value / Expression to the single
LHS Variable Name.
Examples:

>>> a=10
>>> b=20
>>> c=a+b
>>> print(a,b,c) 10 20 30

2. Multi Line Assigment:

=>Syntax: Var1,Var2.....Var-n= Val1,Val2....Val-n

Var1,Var2.....Var-n= Expr1,Expr2...Expr-n

Here The values of Val1, Val2...Val-n are assigned to Var1,Var2...Var-n Respectively.


Here The values of Expr1, Expr2...Expr-n are assigned to Var1,Var2...Var-n Respectively.

Examples:

>>> a,b=10,20
>>> print(a,b) 10 20

>>> c,d,e=a+b,a-b,a*b
>>> print(c,d,e) 30 -10 200

>>> sno,sname,marks=10,"Rossum",34.56
>>> print(sno,sname,marks) 10 Rossum 34.56

>>> a,b=10,20
>>> print(a,b) 10 20

>>> a,b=b,a # Swapping Logic


>>> print(a,b) 20 10

#Program for cal simple Interest and total amount to pay


#simpleInt.py

p=float(input("Enter Principle Amount:"))


t=float(input("Enter Time:"))
r=float(input("Enter rate of Interest:"))
#cal si and totamt
si=(p*t*r)/100
totamt=p+si
print("="*50)
print("\tSimple Intrest Result")
print("="*50)
print("\tPrinciple Amount:{}".format(p))
print("\tTime:{}".format(t))
print("\tRate of Interest:{}".format(r))
print("-"*50)
print("\tSimple Interest :{}".format(si))
print("\tTOTAL AMOUNT TO PAY:{}".format(totamt))
print("="*50)
#ArithmeticOperatorsEx1.py

a=int(input("Enter Value of a:"))


b=int(input("Enter Value of b:"))
print("="*50)
print("\tArithmetic Operators Results")
print("="*50)
print("\tsum({},{})={}".format(a,b,a+b))
print("\tSub({},{})={}".format(a,b,a-b))
print("\tMul({},{})={}".format(a,b,a*b))
print("\tDivision({},{})={}".format(a,b,a/b))
print("\tFloor Division({},{})={}".format(a,b,a//b))
print("\tModulo Division({},{})={}".format(a,b,a%b))
print("\tPower({},{})={}".format(a,b,a**b))
print("="*50)

#Swapvalues.py

a,b=input("Enter Value of a:"),input("Enter Value of b:")


print("-----------------------------")
print("Original value of a:{}".format(a))
print("Original value of b:{}".format(b))
print("-----------------------------")
#Swapping logic
a,b=b,a # Multi Line assignment
print("Swapped value of a:{}".format(a))
print("Swapped value of b:{}".format(b))
print("-----------------------------")

#SquareRootEx.py

n=int(input("Enter a Number for cal Square Root:"))


res=n**0.5
print("SquareRoot({})={}".format(n,res))
LECTURE – 30

3. Relational Operators ( Comparision Operators)

=>The purpose of Relational Operators is that "To Compare Two Values".


=>If two values / Objects connected with Relational Operators then it is called Relational Expression".
=>The result of Relational Expression is either True or False.
=>The Relational Expression is also called Test Condition.
=>In Python Programming, we have 6 Types of Relational operators. They are given in the following
Table.

SLNO SYMBOL MEANING EXAMPLE

1. > greater than print(10>5)-----------True


print(10>20)----------False
2. < less than print(10<20)----------True
print(10<5)-----------False

3. == Equality Operator print(10==20)--------False


(double equal to) print(10==10)--------True

4. != not equal to print(10!=20)--------True


print(10!=10)--------False

5. >= greater than print(10>=10)------True


or equal to print(10>=20--------False

6. <= less than print(10<=10)------True


Or equal to print(10<=5)------False

NOTE: ord() is pre-defined Function which gives UNICODE Value for any Alphabet ,Symbols,
and digits
---chr() is pre-defined Function which gives Alphabet ,Symbols, and digits for UNICODE Value.

>>> "python">"python"
False
>>> "python">"PYTHON"
True
>>> ord('A')
65
>>> ord('B')
66
>>> ord('Z')
90
>>> ord('p')+ord('y')+ord('t')+ord('h')+ord('o')+ord('n')
674
>>> ord('P')+ord('Y')+ord('T')+ord('H')+ord('O')+ord('N')
482
>>> chr(65)
'A'
>>> chr(97)
'a'
>>> "INDIA">"INDIA"
False
>>> "INDIA">"INDAI"
True
>>> "PYTHON">="PYTHON"[::-1]
True

>>> "MADAM">="MADAM"[::-1]
True
>>> ord("@")
64
>>> ord("&")
38
>>> "@#$%">"&*(#"-------------True
>>> #"1234">"4321"
>>> ord("1")
49
>>> ord("4")
52

>>> "1234">"4321"
False
>>> "1234">"4320"
False
>>> ord('0')
48
>>> chr(90)
'Z'
>>> chr(65)
'A'
>>> chr(48)
'0'
>>> chr(49)
'1'
>>> chr(50)
'2'

4. Logical Operators( Comparision Operators )

=>The purpose of Logical Operators is that "To compare the result of Two or More Relational
Expressions".
=>If Two or More Relational Expressions are connected with Logical Operators then It is called
Loogical Expressions.
=>The result of Loogical Expressions is either True or False.
=>The Loogical Expressions is also called Compound Condition(More than one Test Cond).
=>In Python Programming, we have 3 Logical Operators. They are given in the following Table.

SLNO SYMBOL MEANING

1. and Physical ANDing


2. or Physical ORing
3. not ---------------------------

1. "and" Operator (Physical ANDing)

=>Syntax: RelExpr1 and RelExpr2


=>The Functionality of "and" Operators described by using the following Truth table.

RelExpr1 RelExpr2 RelExpr1 and RelExpr2

True False False


False True False
False False False
True True True

Examples:

>>> True and False


False
>>> False and True
False
>>> False and False
False
>>> True and True
True

>>> 10>20 and 20>10----------False----Short Circuit Evaluation


>>> 10>5 and 20>30 and 40>30----False---Short Circuit Evaluation
>>> 10>5 and 20>10 and 30>4----True ---Full Length Evaluation

Short Circuit Evaluation of "and" operator

=>If Two or More Relational Expressions are connected with "and" operator and If the First relational
Expression is False then PVM Will not evalute Rest of the Relational Expressions and The result of
entire Logical Expressions is False. This Process of Evalulation is called Short Circuit Evaluation.

2. "or" Operator (Physical ORing)

=>Syntax: RelExpr1 or RelExpr2

=>The Functionality of "or" Operators described by using the following Truth table.

RelExpr1 RelExpr2 RelExpr1 or RelExpr2

True False True


False True True
False False False
True True True

Examples:

>>> 10>20 or 20>30 or 30>10--------True---Full Length Evaluation


>>> 10>5 or 20>30 or 40>50----------True---Short Circuit Evaluation
>>> 10>20 or 20>10 or 40>50--------True----Short Circuit Evaluation
>>> 10>20 or 20>100 or 40>50-------False---Full Length Evaluation

Short Circuit Evaluation of "or" operator

=>If Two or More Relational Expressions are connected with "or" operator and If the First relational
Expression is True then PVM Will not evalute Rest of the Relational Expressions and The result of
entire Logical Expressions is True. This Process of Evalulation is called Short Circuit Evaluation.

3. "not" Operator

=>Syntax: not RelExpr1


=>The Functionality of "or" Operators described by using the following Truth table.

RelExpr1 not RelExpr1

True False
False True

Examples:

>>> a=True
>>> print(a)------------True
>>> print(not a)------False
>>> a=False
>>> print(a)-----------False
>>> print(not a)-------True

>>> print(10>20)--------False
>>> print(not 10>20)----True
>>> print(not 10!=20)----False
>>> print(not 10==20)----True

>>> not 1234-----------False


>>> not(not 1234)--------True
>>> not "KVR"------------False
>>> not ""------------------True
>>> not " "---------------False

#RelationalOperatorsEx.py

a=int(input("Enter Value of a:"))


b=int(input("Enter Value of b:"))
print("="*50)
print("\tRelational Operators")
print("="*50)
print("\t {} > {}={}".format(a,b,a>b))
print("\t {} < {}={}".format(a,b,a<b))
print("\t {}=={}={}".format(a,b,a==b))
print("\t {}!={}={}".format(a,b,a!=b))
print("\t {}>={}={}".format(a,b,a>=b))
print("\t {}<={}={}".format(a,b,a<=b))
print("="*50)

LECTURE – 31
Bitwise Operators (Most Imp)

=>The Bitwise Operators are applicable on Integer data but not on Floating Point Values bcoz floating
point values does not contain Certainity.
=>Bitwise Operators are used for Performing Operations on Integer data in the form of Bit by Bit.
=>In Otherwords, Bitwise operators First Converts Integer data into Binary Format and Performs
operations on Binary Data in the form of Bit by Bit. Since these Operators are performing Bit by Bit
on Integer Binary data and hence They named as Bitwise Operators.
=>In Python Programming, we have 6 Bitwise Operators. They are given in the following Table.

SLNO SYMBOL MEANING

1. << Bitwise Left Shift Operator


2. >> Bitwise Right Shift Operator
3. | Bitwise OR Operator
4. & Bitwise AND Operator
5. ~ Bitwise Complement Operator
6. ^ Bitwise XOR Operator
1. Bitwise Left Shift Operator ( << )

Syntax: varname=GivenNumber << No. of Bits

Concept: The Bitwise Left Shift Operator ( << ) shifts No. of Bits Towards Left Side By adding No. of
Zero at Right Side (depends on No. of Bits). So thatr esult value will displayed in the form of decial
number system.

Examples:

>>> a=10
>>> b=3
>>> c=a<<b
>>> print(c)-----------80
>>> print(10<<3)--------80
>>> print(3<<2)---------12
>>> print(100<<2)-----400
>>> print(12<<4)-------192

2. Bitwise Right Shift Operator ( >> )

Syntax: varname=GivenNumber >> No. of Bits

Concept: The Bitwise Right Shift Operator ( >> ) shifts No. of Bits Towards Right Side By Adding
No. of Zero at Left Side (depends on No. of Bits). So that result value will displayed in the form of
decial number system .

Examples:

>>> a=10
>>> b=3
>>> c=a>>b
>>> print(c)----------------1
>>> print(10>>2)----------2
>>> print(25>>4)----------1
>>> print(24>>3)---------3
>>> print(50>>4)--------3
>>> print(45>>2)--------11

Bitwise OR Operator (|)

Syntax: varname=Val1 | Val2


=>The Functionality of Bitwise OR Operator is expressed by using the following Truth Table

Val1 Val2 Val1 | Val2

0 0 0
1 0 1
0 1 1
1 1 1

Examples

>>> 0 | 0----------------0
>>> 1 | 0----------------1
>>> 0 | 1----------------1
>>> 1 | 1----------------1

>>> a=10
>>> b=12
>>> c=a|b
>>> print(c)----------14
>>> print(10|15)-----15
>>> print(3|4)--------7

>>> s1={10,20,30}
>>> s2={15,20,35}
>>> s3=s1.union(s2)
>>> print(s3,type(s3))---------{35, 20, 10, 30, 15} <class 'set'>
>>> s4=s1|s2 # Bitwise OR Operator
>>> print(s4,type(s4))---------{35, 20, 10, 30, 15} <class 'set'>
>>> s2={"Sberry","Apple","Guava"}
>>> s3=s1.union(s2)
>>> print(s3,type(s3))--------{'Mango', 'Guava', 'Sberry', 'Kiwi', 'Apple'} <class 'set'>
>>> s4=s1|s2 # Bitwise OR Operator
>>> print(s4,type(s4))--------{'Mango', 'Guava', 'Sberry', 'Kiwi', 'Apple'} <class 'set'>

Special Ponits

>>> 100 or 200------------100


>>> -123 or 400---------- -123
>>> 123 or 0------------- 123
>>> 0 or 123-------------123
>>> 3|4---------------------7
>>> 3 or 4--------------3
>>> 0 or 234 or 0 or 34-------234

>>> True and True


True
>>> True and False
False
>>> False and False
False
>>> False and True
False

>>> 100 and 200--------------200


>>> 100 and 0------------------0
>>> 100 and -123-------------123
>>> "KVR" or "PYTHON"------------'KVR'
>>> "KVR" or "PYTHON" or False-------'KVR'
>>> False or "KVR" or "PYTHON" or False-------'KVR'
>>> "KVR" and "PYTHON"----------------'PYTHON'
>>> "KVR" and 567---------------------567

>>> 100 and 200 or 456----------------200


>>> 100 and 200 or 567 or "KVR" and 450 and 678-------200
>>> 100&200-----------------64
>>> 100 and 200-----------200
>>> "$" and "%" or 123---------'%'

LECTURE – 32

Bitwise AND Operator ( & )

Syntax: varname=Val1 & Val2

=>The Functionality of Bitwise AND Operator is expressed by using the following Truth Table

Val1 Val2 Val1 & Val2


0 0 0
1 0 0
0 1 0
1 1 1
Examples

>>> 0 & 1
0
>>> 0 & 0
0
>>> 1 & 0
0
>>> 1 & 1
1

>>> a=10
>>> b=15
>>> c=a&b
>>> print(c) 10
>>> 10 and 15 15
>>> print(3&4) 0
>>> "Keshav" and "Regular" and "Forgot" 'Forgot'

Bitwise Complement Operator ( ~ )

=>Syntax:- varname=~value

=>The Functionality of Bitwise Complement Operator ( ~ ) is that "It Inverts the bits".
=>Inverting the Bits are nothing but 0 becomes 1 and 1 becomes 0.
=>The Formula for Bitwise Complement Operator ( ~ ) is that " ~Value= - (Value+1)

Examples:

>>> a=4
>>> b=~a ~(0100)
-(0100+1)
-(0101)---->Whose result is -5
>>> print(b) 5
>>> print(a) 4
Bitwise XOR Operator ( ^ )

Syntax: varname=Val1 ^ Val2

=>The Functionality of BitwiseXOR Operator is expressed by using the following Truth Table

Val1 Val2 Val1 ^ Val2


0 0 0
1 0 1
0 1 1
1 1 0

Examples

>>> 1^0
1
>>> 1^1
0
>>> 0^1
1
>>> 0^0
0

>>>a=10
>>>b=15
>>>c=a^b 1010
1111
--------------
0101-----Result--5
>>>print(c)---5

>>> print(4^5) 1
>>> print(10^5) 15

>>> s1={10,20,30}
>>> s2={15,20,25}
>>> print(s1,type(s1)) {10, 20, 30} <class 'set'>
>>> print(s2,type(s2)) {25, 20, 15} <class 'set'>
>>> s3=s1.symmetric_difference(s2)
>>> print(s3,type(s3)) {10, 15, 25, 30} <class 'set'>
>>> s4=s1^s2 # Bitwise XOR
>>> print(s4,type(s4)) {10, 15, 25, 30} <class 'set'>

Membership Operators
=>The purpose of Membership Operators is that "To Check the Existence of Specified value in an
Iterable Object".
=>An Iterable Object is one, which contains Two or More Number of Values.

=>Examples: Sequence Data Types(str,bytes,byearray,range)


List data Types ( list, tuple)
Set Data Types (set,frozenset)
dict data type(dict)

=>Note that Fundamental and NoneType Data Types are not belongs to Iterable Object.
=>In Python Programming, we have 2 Types of Membership Operators. They are
1. in
2. not in
1. in

=>Syntax: Value in IterableObject

=>If the "Value" Present in IterableObject then "in" operator Returns True
=>If the "Value" not Present in IterableObject then "in" operator Returns False

2. not in

=>Syntax: Value not in IterableObject

=>If the "Value" not Present in IterableObject then "not in" operator Returns True
=>If the "Value" Present in IterableObject then "not in" operator Returns False

Examples:

>>> s="PYTHON"
>>> print(s,type(s))
PYTHON <class 'str'>
>>> "P" in s
True
>>> "p" in s
False
>>> "O" in s
True
>>> "N" not in s
False
>>> "y" not in s
True
>>> "k" not in s
True

>>> s="PYTHON"
>>> print(s,type(s))
PYTHON <class 'str'>
>>> "PYTH" in s
True
>>> "PYTH" not in s
False
>>> "THON" not in s
False
>>> "PTO" in s
False
>>> "PTO" not in s
True
>>> "HON" not in s
False
>>> "PON" not in s
True
>>> "NOH" in s
False
>>> "NOH" not in s[::-1]
False
>>> "PON" in s[::2]
False
>>> "PTO" not in s[::2]
False
>>> "PTO" not in s[::2][::-1]
True

>>> lst=[10,"Rossum",23.45,True,2+3j]
>>> print(lst,type(lst))
[10, 'Rossum', 23.45, True, (2+3j)] <class 'list'>
>>> "Rossum" in lst
True
>>> False not in lst
True
>>> True not in lst
False
>>>
>>> print(lst,type(lst))
[10, 'Rossum', 23.45, True, (2+3j)] <class 'list'>
>>> 'Ross' in lst
False
>>> 'Ross' not in lst[1]
False
>>> 'Ross' in lst[1]
True
>>> 'Ross' not in lst[1][::-1]
True
>>> 'Ross'[::-1] not in lst[1][::]
True

>>> lst=[10,"Rossum",23.45,True,2+3j]

>>> print(lst,type(lst)) [10, 'Rossum', 23.45, True, (2+3j)] <class 'list'>


>>> lst[-1] in lst True
>>> lst[-1] in lst[4] TypeError: argument of type 'complex' is not iterable
>>> lst[-1].real in lst[len(lst)-1] TypeError: argument of type 'complex' is not iterable

>>> lst=[10,"Rossum",23.45,True,"2+3j"]

>>> print(lst,type(lst)) [10, 'Rossum', 23.45, True, '2+3j'] <class 'list'>


>>> lst[-1] in lst[4] True
>>> lst in lst False

>>> s="PYTHON"
>>> s in s True

>>> d1={10:"Apple",20:"Mango",30:"Kiwi"}
>>> print(d1,type(d1)) {10: 'Apple', 20: 'Mango', 30: 'Kiwi'} <class 'dict'>
>>> d1 in d1 TypeError: unhashable type: 'dict'
>>> 10 in d1
True
>>> "Apple" in d1
False
>>> (30,"Kiwi") in d1
False
>>> (30,"Kiwi") in d1.items()
True
>>> for x in d1:
... print(x)
...
10
20
30
>>> "Apple" in d1.values()
True
>>> "Apple"[::-1] not in d1.values()[::-1] TypeError: 'dict_values' object is not subscriptable

LECTURE – 33

Identity Operators----Python Command Prompt

=>The purpose of Identity Operators is that "To Check the memory address of Objects".
=>In Python Programming , We have Two Types of Identity Operators. They are
1. is
2. is not
1. is

=>Syntax: Obj1 is Obj2

=>Here 'is' operator returns True provided Obj1 and Obj2 Memory Addresses are Same
=>Here 'is' operator returns False provided Obj1 and Obj2 Memory Addresses are Different

2. is not

=>Syntax: Obj1 is not Obj2

=>Here 'is not' operator returns True provided Obj1 and Obj2 Memory Addresses are Different
=>Here 'is not ' operator returns False provided Obj1 and Obj2 Memory Addresses are Same

Examples

>>> a=None
>>> b=None

>>> print(a, id(a))


None 140732600948728
>>> print(b, id(b))
None 140732600948728
>>> a is b
True
>>> a is not b
False

>>> d1={10:"Apple",20:"Mango",30:"Kiwi"}
>>> d2={10:"Apple",20:"Mango",30:"Kiwi"}
>>> print(d1,id(d1))
{10: 'Apple', 20: 'Mango', 30: 'Kiwi'} 2681935187584
>>> print(d2,id(d2))
{10: 'Apple', 20: 'Mango', 30: 'Kiwi'} 2681935187712
>>> d1 is d2
False
>>> d1 is not d2
True

>>> s1={10,20,30,40}
>>> s2={10,20,30,40}
>>> print(s1,id(s1))
{40, 10, 20, 30} 2681935942080
>>> print(s2,id(s2))
{40, 10, 20, 30} 2681935941632
>>> s1 is s2
False
>>> s1 is not s2
True
>>> fs1=frozenset({10,20,30,40})
>>> fs2=frozenset({10,20,30,40})

>>> print(fs1,id(fs1))
frozenset({40, 10, 20, 30}) 2681935943200
>>> print(fs2,id(fs2))
frozenset({40, 10, 20, 30}) 2681935942976
>>> fs1 is fs2
False
>>> fs1 is not fs2
True

>>> l1=[10,20,30,40]
>>> l2=[10,20,30,40]

>>> print(l1,id(l1))
[10, 20, 30, 40] 2681935234176
>>> print(l2,id(l2))
[10, 20, 30, 40] 2681935235008
>>> l1 is l2
False
>>> l1 is not l2
True

>>> t1=(10,20,30,40)
>>> t2=(10,20,30,40)

>>> print(t1,id(t1))
(10, 20, 30, 40) 2681935633584
>>> print(t2,id(t2))
(10, 20, 30, 40) 2681935637584
>>> t1 is t2
False
>>> t1 is not t2
True

>>> s1="PYTHON"
>>> s2="PYTHON"

>>> print(s1,id(s1))
PYTHON 2681939497776
>>> print(s2,id(s2))
PYTHON 2681939497776
>>> s1 is s2
True
>>> s1 is not s2
False
>>>
>>> s1="PYTHON"
>>> s2="PYTHOn"
>>> s1 is not s2
True
>>> s1 is s2
False

>>> b1=bytes([10,20,30,40])
>>> b2=bytes([10,20,30,40])

>>> print(b1,id(b1))
b'\n\x14\x1e(' 2681935345920
>>> print(b2,id(b2))
b'\n\x14\x1e(' 2681935343760
>>> b1 is b2
False
>>> b1 is not b2
True

>>> b1=bytearray([10,20,30,40])
>>> b2=bytearray([10,20,30,40])

>>> print(b1,id(b1))
bytearray(b'\n\x14\x1e(') 2681939498032
>>> print(b2,id(b2))
bytearray(b'\n\x14\x1e(') 2681939497648
>>> b1 is b2
False
>>> b1 is not b2
True

>>> r1=range(10,20)
>>> r2=range(10,20)

>>> print(r1,id(r1))
range(10, 20) 2681935341408
>>> print(r2,id(r2))
range(10, 20) 2681935346016
>>> r1 is r2
False
>>> r1 is not r2
True

>>> c1=2+3j
>>> c2=2+3j
>>> print(c1,id(c1))
(2+3j) 2681934897040
>>> print(c2,id(c2))
(2+3j) 2681934900656
>>> c1 is c2
False
>>> c1 is not c2
True

>>> a=True
>>> b=True

>>> print(a,id(a))
True 140732600896360
>>> print(b,id(b))
True 140732600896360
>>> a is b
True
>>> a is not b
False

>>> a=1.2
>>> b=1.2

>>> print(a,id(a))
1.2 2681934897424
>>> print(b,id(b))
1.2 2681934897456
>>> a is b
False
>>> a is not b
True

Here 0---256 values contains Same Address when we store same value in Different Objects
Other than those Values They Contins Different Address

>>> a=300
>>> b=300
>>> print(a,id(a))
300 2681934896976
>>> print(b,id(b))
300 2681934900624
>>> a is b
False
>>> a is not b
True
>>>
>>>
>>> a=10
>>> b=10
>>> print(a,id(a))
10 2681933857296
>>> print(b,id(b))
10 2681933857296
>>> a is b
True
>>> a is not b
False
>>> a=0
>>> b=0
>>> print(a,id(a))
0 2681933856976
>>> print(b,id(b))
0 2681933856976
>>> a is b
True
>>> a is not b
False
>>> a=256
>>> b=256
>>> print(a,id(a))
256 2681933865168
>>> print(b,id(b))
256 2681933865168
>>> a is b
True
>>> a is not b
False

>>> a=257
>>> b=257
>>> print(a,id(a))
257 2681934900752
>>> print(b,id(b))
257 2681934900720
>>> a is b
False
>>> a is not b
True

Here -1 to -5 values contains Same Address when we store same value in Different Objects
Other than those Values They Contins Different Address

>>> a=-1
>>> b=-1
>>> print(a,id(a))
-1 2681933856944
>>> print(b,id(b))
-1 2681933856944
>>> a is b
True
>>> a is not b
False
>>> a=-5
>>> b=-5
>>> print(a,id(a))
-5 2681933856816
>>> print(b,id(b))
-5 2681933856816
>>> a is b
True
>>> a is not b
False
>>> a=-10
>>> b=-10
>>> print(a,id(a))
-10 2681934900720
>>> print(b,id(b))
-10 2681934900752
>>> a is b
False
>>> a is not b
True

>>> a,b=300,300 # Multi Line Assigment

>>> print(a,id(a))
300 2681934900560
>>> print(b,id(b))
300 2681934900560
>>> a is b
True
>>> a is not b
False

>>> a,b=-10,-10 # Multi Line Assigment

>>> print(a,id(a))
-10 2681934900688
>>> print(b,id(b))
-10 2681934900688
>>> a is b
True
>>> a is not b
False

>>> a,b=1.2,1.2
>>> print(a,id(a))
1.2 2681934897424
>>> print(b,id(b))
1.2 2681934897424
>>> a is b
True
>>> a is not b
False

>>> l1,l2=[10,"RS",23.45],[10,"RS",23.45]
>>> print(l1,id(l1))
[10, 'RS', 23.45] 2681939493376
>>> print(l2,id(l2))
[10, 'RS', 23.45] 2681939493312
>>> l1 is l2
False
>>> l1 is not l2
True

>>> a=1000
>>> b=a # Deep Copy
>>> print(a,id(a))
1000 2681934900592
>>> print(b,id(b))
1000 2681934900592
>>> a is b
True
>>> a is not b
False

>>> a=1.2
>>> b=a # Deep Copy
>>> print(a,id(a))
1.2 2681934897552
>>> print(b,id(b))
1.2 2681934897552
>>> a is b
True

>>> l1=[10,"RS",23.45]
>>> l2=l1 # Deep Copy

>>> print(l1,id(l1))
[10, 'RS', 23.45] 2681935235008
>>> print(l2,id(l2))
[10, 'RS', 23.45] 2681935235008

>>> l1 is l2
True
>>> l1 is not l2
False

>>> l1=[10,"RS",23.45]
>>> l2=l1.copy() # Shallow Copy

>>> print(l1,id(l1))
[10, 'RS', 23.45] 2681939493312
>>> print(l2,id(l2))
[10, 'RS', 23.45] 2681939493376
>>> l1 is l2
False
>>> l1 is not l2
True

Python Ternary Operator

=>The Ternary Operator in Python Programming Language is " if .. else Operator ".
=>Syntax:
varname = Expr1 if Test Cond else Expr2
Explanation:
=>Here "if" and "else" are the keywords
=>Here Test Cond is to be evaluated either to be True or False
=>If the Test Condition is True then PVM Executes Expr1 and whose Result assigned to Varname
=>If the Test Condition is False then PVM Executes Expr2 and whose Result assigned to Varname
=>Hence varname contains result of either Expr1 or Expr2
#program for biggest of two numbers

a=float(input("Enter First Value:")) # 10


b=float(input("Enter Second Value:")) # 20
bv=a if a>b else b
print("Big({},{})={}".format(a,b,bv))

#BiTwoEqual.py

a=float(input("Enter First Value:")) # 100


b=float(input("Enter Second Value:")) # 100
bv=a if a>b else b if b>a else "Both Values Equal"
print("big({},{})={}".format(a,b,bv))

#PalindromeEx1.py

word=input("Enter any Value / Word:")


res = "PALINDROME" if word.upper()==word[::-1].upper() else "NOT PALINDROME"
print("{} is {}".format(word,res))

#VowelWord.py

word=input("Enter any Word:") # Apple


res="Vowel Word" if 'a' in word.lower() or 'e' in word.lower() or 'i' in word.lower() or 'o' in word.lower()
or 'u' in word.lower() else " Not Vowel Word"
print("{} is {}".format(word,res))

You might also like