operators and expressions in python | types of operators | python expressions
bySanee Kumar •
0
OPERATORS AND EXPRESSIONS
LEARNING OBJECTIVES
इस अध्याय के अंत में छात्र समझ सकेंगे:
Types of Operators:
Arithmetic Operators
Relational Operators
Assignment Operators
Logical Operators
Identity Operators
Membership Operators
Precedence of Operators
Python Expressions
Indentation
OPERATORS
ऑपरेटर्स का उपयोग डेटा पर कुछ action करने के लिए किया जाता है। ऑपरेटरों के 6 विभिन्न प्रकार हैं:
Arithmetic
Unary: इन्हें एक ही ऑपरेंड की आवश्यकता होती है। वे अपने ऑपरेंड का पूर्ववर्ती (precede) या अनुसरण (follow) कर सकते हैं। उदाहरण के लिए: unary +, unary -, increment / decrement ऑपरेटर।
DESCRIPTION
EXAMPLE
UNARY -
To make the value of the number negative
-5
UNARY +
To make the value of the number positive
+5
Binary: इन्हें एक operation करने के लिए दो ऑपरेंड की आवश्यकता होती है। उदाहरण के लिए: +, -, *, /, % ।
DESCRIPTION
EXAMPLE
+
Add two operands
10+5=15
-
Subtracts two operands
10-5=5
*
Multiplies two operands
10*5=50
/
दो ऑपरेंड को विभाजित करता है और भागफल देता है (Divides two operands and gives quotient)
10/5=5
10/3.0 = 3.3
10.0/3=3.3
10/3=3
%
दो ऑपरेंड को विभाजित करता है और शेष देता है (Divides two operands and gives remainder)
10%5=0
**
Exponentiation
2**4= 16
//
Integer division
5//2=2
5.0//2=2.0
Relational/Comparison Operator : ये ऑपरेटर दो ऑपरेंड के बीच संबंध का वर्णन करते हैं(These operators describe the relationship between two operands)। उदाहरण के लिए: <,>,<=,>=,==,!=
DESCRIPTION
EXAMPLES
<
जाँच करता है कि क्या left operand की value right operand की value से less है, यदि हाँ तो condition true हो जाती है।
10<15, true
10>15, false
"rain"<"sun"
true
>
जाँच करता है कि क्या left operand की value right operand की value से greater है, यदि हाँ तो condition true हो जाती है।
15>10, true
15<10, false
"rain">"sun"
false
<=
जाँच करता है कि क्या left operand की value right operand की value से less या equal है, यदि हाँ तो condition true हो जाती है।
15<=15, true
13<=15, true
16<=15, false
"rain"<="sun"
true
>=
>जाँच करता है कि क्या left operand की value right operand की value से greater या equal है, यदि हाँ तो condition true हो जाती है।
15>=15, true
16>=15, true
14>=15, false
"rain">="sun"
false
= =
जाँच करता है कि क्या left operand की value right operand की value के equal है, यदि हाँ तो condition true हो जाती है।
15= = 15, true
16 = = 15, false
"rain"=="sun"
false
!=
जाँच करता है कि क्या left operand की value right operand की value के equal नहीं है, यदि हाँ तो condition true हो जाती है।
15 != 15, false
14 != 15, true
"rain"!= "sun"
true
Assignment Operator
Following assignment operators are supported by Python:
DESCRIPTION
EXAMPLE
=
राइट साइड ऑपरेंड की वैल्यू लेफ्ट साइड ऑपरेंड को असाइन करता है।
A = 10
+=
Right operand को left operand में जोड़ता(add) है और रिजल्ट को left operand में assign करता है।
A+=10 is equivalent to
A =A +10
- =
Right operand को left operand में से घटाता (subtract) है और रिजल्ट को left operand में assign करता है।
A-=10 is equivalent to
A =A -10
*=
Right operand को left operand से गुणा (multiply) करता है और रिजल्ट को left operand में assign करता है।
A*=10 is equivalent to
A =A *10
/=
Left operand को right operand से divide करता है और quotient को left operand में assign करता है।
A/=10 is equivalent to
A =A /10
%=
Left operand को right operand से divide करता है और reminder को left operand में assign करता है।
A%=10 is equivalent to
A =A %10
**=
ऑपरेंड पर exponential operations करता है और परिणाम को बाएं ऑपरेंड को सौंपता है
A **=2 is equivalent to A=A**2
//=
ऑपरेंड पर floor division operation करता है और परिणाम को बाएं ऑपरेंड को सौंपता है
A//=2 is equivalent to A=A//2
Logical Operators
Logical operators का उपयोग रिलेशनल एक्सप्रेशंस को join करने के लिए किया जाता है (Logical operators are used to join relational expressions)
DESCRIPTION
EXAMPLE
not
NOT operator। यह एक एकीकृत ऑपरेटर (unary operator) है और यह अपने ऑपरेंड की logical state को उलट (reverse) देता है। यदि condition true है, तो logical NOT operator उसे false करेगा। यदि condition false है, तो logical NOT operator उसे true करेगा।
>>> not(5>10) = not(false)
true
or
OR Operator। यह true return करता है अगर इस ऑपरेटर द्वारा join की गई कोई भी condition true है। दोनों ही conditions false होने पर यह false return करता है।
>>> (10<5) or (15>20)
false
>>> (10>5) or (15>20)
true
>>> (10<5) or (15<20)
true
>>> (10<5) or (15< 20)
true
and
AND Operator। यह true return करता है अगर इस ऑपरेटर द्वारा join की गई दोनों conditions true है। किसी भी एक condition false होने पर यह false return करता है।
>>> (20>5 ) and (15>10)
true
>>> (20<5 ) and (15>10)
false
>>>(20>5 ) and (15<10)
false
>>>(20<5 ) and (15<10)
false
NOTE: Please remember
Expression
Result
Expression
Result
not(false)
true
true and true
true
not(true)
false
false or true
true
false and false
false
false or false
false
false and true
false
true or false
true
true and false
false
true or true
true
Identity Operators:
Identity operators का उपयोग दो variables की तुलना करने के लिए किया जाता है, वे वास्तव में एक ही object हैं, एक ही memory location के साथ।
DESCRIPTION
EXAMPLE
is
True return करता है अगर इस ऑपरेटर के दोनों तरफ के variable एक ही memory location को point करते हैं अन्यथा false return करता है।
>>>x =5
>>>type(x) isint
True
is not
True return करता है अगर इस ऑपरेटर के दोनों तरफ के variable different memory locations को point करते हैं अन्यथा false return करता है।
>>>x =5
>>>type(x) isnot int
False
Membership Operators:
Membership operators का उपयोग किसी value की membership को मान्य (validate) करने के लिए किया जाता है। यह एक sequence में membership के लिए परीक्षण करता है, जैसे कि strings, lists या tuples।
DESCRIPTION
EXAMPLE
in
‘in’ ऑपरेटर का उपयोग यह जांचने के लिए किया जाता है कि कोई value किसी sequence में मौजूद है या नहीं। True का मूल्यांकन करता है अगर specified sequence में value मिलती है अन्यथा false होता है।
>>>x = [1,2,3,4,5]
>>>3 in x
True
not in
True का मूल्यांकन करता है अगर specified sequence में value नहीं मिलती है अन्यथा false होता है।
>>>x =[1,2,3,4,5]
>>>7 not in x
True
PRECEDENCE OF OPERATORS
Python अपने mathematical operators के लिए same precedence rules का पालन करता है जो की गणित करता है। संक्षिप्त नाम (acronym) PEMDAS (P arentheses, E xponentiation, M ultiplication/ D ivision, A ddition/ S ubtraction) order of operations को याद रखने का एक उपयोगी तरीका है:
<(less than),<=(less than or equal),>(greater than), >=(greater than or equal to)
==(equal),!=(not equal)
=(simple assignment) and other assignment operators (arithmetic assignment operator)
is, is not
in, not in
not (logical NOT), and (logical AND), or (logical OR)
NOTE: गुणन (Multiplication ) और विभाजन (division) एक ही precedence पर हैं, और बाएं से दाएं नियम लागू होता है। इसी तरह, घटाव(subtraction) और जोड़(addition) एक ही precedence पर हैं, और बाएं से दाएं नियम लागू होता है।
EXPRESSIONS
एक expression values, variables, operators and call to functions के लिए एक संयोजन(combination) है। जब Python prompt पर एक expression type की जाती है, तो interpreter इसका मूल्यांकन करता है और result display करता है, जो हमेशा एक मान (value) होता है:
EXAMPLE
v=u*t + 1/2*a*t*t
f=(c * 9/5) + 32
math MODULE
Math module mathematical function के लिए access प्रदान करता है। इस module में मौजूद सबसे व्यापक रूप से उपयोग किए जाने वाले कुछ functions हैं:
math.ceil(x) Return the ceiling of x, the smallest integer greater than or equal to x.
math.floor(x) Return the floor of x, the largest integer less than or equal to x.
math.exp(x) Return e**x.
math.log10(x) Return the base-10 logarithm of x.
math.pow(x, y) Return x raised to the power y.
math.sqrt(x) Return the square root of x.
Q Write the Python expression for the following (निम्नलिखित के लिए पायथन expression लिखें):
(i) -x/y + (1+2a)4 - xy
(ii) a+ (2x-y)/3x
Answers
(i) -x/y + math.pow((1+2*a), 4) - x*y
(ii) a+ math.sqrt((2*x - y)/3*x)
Q Evaluate the following expressions(निम्नलिखित expressions का मूल्यांकन(evaluation) करें):
(i) 85%2+70-3**3+15
= 85%2 + 70 - 27 +15
= 1 + 70 - 27 +15
= 59
(ii) 5//2*5+8-3**2
= 2*5+8-9
= 10+8-9
= 9
(iii) 5/2*5+8-3**2
= 2.5*5+8-9
= 12.5+8-9
= 11.5
(iv) not(5>10) or (10 == 10) and (not(9<15))
=not(false) or(true) and(not(true))
= true or true and false
=true and false
= true
Q Write a Python script to input values of a, b, c and calculate the roots of the quadratic equation.(a, b, c के इनपुट values के लिए पायथन script लिखें और quadratic equation के roots की गणना(calculate) करें।)
# Program to calculate roots of the quadratic equation
import math
a=eval(input('enter value of a'))
b=eval(input('enter value of b'))
c=eval(input('enter value of c'))
D=b*b -4*a*c
root1= (-b + math.sqrt(D))/2*a
root2= (-b - math.sqrt(D))/2*a
print("Root1=", root1)
print("Root2=", root2)
OUTPUT
enter value of a1
enter value of b2
enter value of c1
Root1= -1.0
Root2= -1.0
INDENTATION
पायथन में इंडेंटेशन का उपयोग statements के ब्लॉक बनाने के लिए किया जाता है जिसे suite के रूप में भी जाना जाता है । अधिकांश प्रोग्रामिंग भाषाओं में, इंडेंटेशन का उपयोग केवल कोड को सुंदर दिखने के लिए किया जाता है। लेकिन पायथन में, यह संकेत(indicate) देने के लिए आवश्यक है कि statement किस कोड ब्लॉक को belong करती है। इंडेंटेशन का उपयोग आमतौर पर flow of कंट्रोल स्टेटमेंट्स (जैसे if, for आदि के लिए), फंक्शन, क्लास आदि में किया जाता है। हालांकि हम इंडेंटेशन के लिए कितनी भी spaces दे सकते हैं, लेकिन एक ब्लॉक में सभी स्टेटमेंट में एक ही इंडेंट होना चाहिए।
NOTE :
Python में इंडेंटेशन की मात्रा मायने रखती है। पायथन ब्लॉक में एक missing या अतिरिक्त स्थान के त्रुटि या अप्रत्याशित परिणाम(error or unpredictable results) हो सकते हैं।
It is important to indent the statements within the same block of code at the same level.
Python IDLE is also designed to automatically indent code. For instance, pressing Return key after typing the ‘:’ in the flow of control statements like if, for etc. automatically indents the cursor on the next line.
Unnecessary indentation results in syntax error and incorrect indentation results in logical error.
Built-in functions for working with numbers
Some of the common built-in functions for working with numbers are:
EXPLANATION
EXAMPLE
abs()
returns absolute value of a number
abs(12) will give 12
abs(-12) will give 12
max()
returns largest element
print(max(10, 20, 30)) will give 30
min()
returns smallest element
print(min(10, 20, 30)) will give 10
pow()
returns x to the power of y
pow(2,3) will give 8
NOTE : The help() function invokes the built-in Help System.
EXAMPLE
>>> help(max)
OUTPUT
Help on built-in function max in module built-in's:
max(...)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the largest argument.