Python Operators

Python Operators

In Python, if you want to apply ‘+’ operator for the string, compulsory both arguments should be string type only, otherwise PVM throws an error message. In this case ‘+’ act as concatenation operator.

पॉयथन में, यदि आप स्ट्रिंग के लिए ‘+’ ऑपरेटर एप्लाई करना चाहते हैं, तो इसके लिए दोनों आगुर्मेन्ट को केवल स्ट्रिंग टाइप का होना चाहिए नहीं तो पीवीएम् एक एरर मेसेज देगा इस केस में + concatenation operator के रूप में कार्य करता है।

For example:

>>> strl = "Python" 
>>> str2 = "Programming" 
>>> strl + str2 
'Python Programming' 
>>>print(strl + str2) 
PythonProgramming

Note: Python cannot concatenate a string with integer.
पॉयथन इंटीजर के साथ एक स्ट्रिंग को कॉनकैटनेट नहीं कर सकता है।
For example:

>>> str1 = "Hello" 
>>>val = 100 
>>> str1 + val 
TypeError: can only concatenate str (not "int") to str

To make this possible, we can convert the number into a string using the appropriate function.
इसे संभव बनाने के लिए, हम उचित फंक्शन का उपयोग करके संख्या को एक स्ट्रिंग में बदल सकते हैं।
For example:

>>> str1 + str (val) 
'Hello100'
>>>print (str1 + str (val)) 
Hello100

The method we used to do this is the str() function. The str() function converts the specified value into a string

जिस विधि से हम ऐसा करते थे वह stro फंक्शन है। stro फंक्शन निर्दिष्ट वैल्यू को एक स्ट्रिंग में परिवर्तित करता है।

‘*’ operator for “string” type
 “स्ट्रिंग” टाईप के लिए ‘* ऑपरेटर

In Python, if you want to apply ‘*’ operator for the string, compulsory one argument should be string type another one is int type only, otherwise PVM throws an error message. In this case ‘*” act as string repetation operator.

पॉयथन में, यदि आप स्ट्रिंग के लिए ‘* ऑपरेटर एप्लाई करना चाहते हैं, अनिवार्य रूप से एक आर्गेमेन्ट स्ट्रिंग प्रकार होना चाहिए और एक अन्य केवल int टाइप प्रकार का हो, नहीं तो पीवीएम एक एरर मैसेज देगा। इस केस में, ‘* स्ट्रिंग रिपीटेशन ऑपरेटर के रूप में कार्य करता है।

>>>print ("python" * 5) 
pythonpythonpythonpythonpython 
>>>print (5 * "python") 
pythonpythonpythonpythonpython 
>>>print("python" * "python") 
TypeError: can't multiply sequence by non-int of type 'str! 
>>>print ("python" * "3") 
TypeError: can't multiply sequence by non-int of type.'str' 
>>>print ("python" * int ("3") ) 
Pythonpythonpython

‘*’ operator for “bool” type
बूल” टाईप के लिए ‘* ऑपरेटर

In Python, “True’ internally represented as ‘1’ and ‘False’ internally represented as ‘0’. If we are performing arithmetic operations with Boolean values, we will get corresponding values.

पॉयथन में, ‘टू’ को आंतरिक रूप से ‘1’ के रूप में और ‘फॉल्स’ को आंतरिक रूप से ‘0’ के रूप में दर्शाया गया है। यदि हम बूलियन वैल्यू के साथ अरिथमेटिक ऑपरेशन कर रहे हैं, तो हमें संबंधित वैल्यू मिलेंगे।

>>>print('python' * True) 
python 
>>>print('python' * False)

‘/’ operator for arithmetic operation
अरिथमेटिक ऑपरेशन के लिए ‘/’ ऑपरेटर

In Python, x/0 or x//0 or x%0 then PVM throws an error called “zero division error”.

पॉयथन में, x/0 या x//0 या x%0 उसके बाद पीवीएम एक एरर मैसेज देगा जिसे “जीरो डिवीजन एरर’ कहा जाता है।
Example:
>>> 3/0
ZeroDivisionError: division by zero
>>> 3//10
0
>>> 3%0
 ZeroDivisionError: integer division or modulo by zero

Relational Operator or Comparision Operator –
रिलेशनल ऑपरेटर या कम्पेरिजन ऑपरेटर

This Operator compares the values of each side of the operand and determines the relation between them. It is also referred as relational operators. The Comparison operators are typically used in Boolean contexts like conditional and loop statements to direct program flow.

ऑपरेटर ऑपरेंड के प्रत्येक पक्ष के वैल्यू की तुलना करते हैं और उनके बीच संबंध निर्धारित करते हैं। इसे रिलेशनल ऑपरेटर्स के रूप में भी जाना जाता है। कम्पेरिजन ऑपरेटर का उपयोग आमतौर पर बूलियन कंटेक्स्ट में किया जाता है जैसे कि कंडीशनल और लूप स्टेटमेंट को सीधे प्रोग्राम फ्लो में।

Comparison operator for arithmetic operation:
एरिथमेटिक ऑपरेशन के लिए कम्पेरिजन ऑपरेटर:

Comparison operator in python compares the values of two operands and returns true or False, these Comparison operators are also called rational operator. You can in the example given below:

पॉयथन में कम्पेरिजन ऑपरेटर दो ऑपरेंड के मूल्यों की तुलना करता है और सही या गलत रिटर्न देता है, इन तुलना ऑपरेटर को तर्कसंगत ऑपरेटर भी कहा जाता है। आप नीचे दिए गए उदाहरण में देख सकते हैं:
For example:

a = 10 
b = 20 
print('a < b : ', a < b) 
print ( 'a > b : ', a > b) 
print('a <= b : ', a <= b) 
print('a >= b : ', a >= b) 
print('a == b : ', a == b) 
print ('a != b: ', a != b)

Output:
a < b :  True
a > b :  False
a <= b :  True
a >= b :  False
a == b :  False
a != b:  True

Comparison operator for string operation
स्टिंग ऑपरेशन के लिए कम्पेरिजन ऑपरेटर

In Python we can apply comparison operator in string type operations. The comparison will be happen for string operations – using First character of the string Unicode value or ASCII value only.

पॉयथन में, स्ट्रिंग टाइप ऑपरेशन में हम कम्पेरिजन ऑपरेटर को लागू कर सकते हैं। कम्पेरिजन स्टिंग ऑपरेशन के लिए होगी- स्ट्रिंग के पहले कैरेक्टर यूनिकोड वैल्यू या ASCII वैल्यू मात्र।

Note: ASCII value for ‘a = 97’ and ‘A=65’. In order to know ASCII value of ‘a’ character there is a builtin function ord() returns ASCII value of a character and similarly if you know ASCII value, then if you find corresponding character in Python we have another buit-in function called chr() function. Let us see an example to understand.

नोट: ‘a = 97’ और ‘A=65’ के लिए ASCII वैल्यू। ‘a’ कैरेक्टर के लिए ascii वैल्यू को जानने के लिए एक बिल्ट-इन-फंक्शन ord() एक कैरेक्टर के ascii वैल्यू देता है और इसी तरह यदि आप ascii वैल्यू जानते हैं, फिर यदि आप पॉयथन में करस्पॉन्डिंग कैरेक्टर पाते हैं, तो हमारे पास chr0 फंक्शन नामक एक और बिल्ट-इन-फंक्शन है। समझने के लिए एक उदाहरण लेते हैं।
For example:

>>>ord ('a') 
97 
>>>ord ( 'A') 
65 
>>>chr (97) 
'a' 
>>>chr (65) 
'A'

Comparison operator for ‘bool’ operation:
ूल’ ऑपरेशन के लिए कम्पेरिजन ऑपरेटर:

In Python, For Boolean type operation also we can apply comparison operator. In Python, ‘True’ internally represented as ‘l’ and ‘False’ internally represented as ‘0’. If we are performing arithmetic operations with Boolean values, we will get corresponding values.

पॉयथन में, बूलियन टाइप ऑपरेशन के लिए भी हम कम्पेरिजन ऑपरेटर को अप्लाई कर सकते हैं। पॉयथन में, ‘टू’ को आंतरिक रूप से ‘1’ के रूप में और ‘फॉल्स’ को आंतरिक रूप से ‘0’ के रूप में दर्शाया गया है। यदि हम बूलियन वैल्यू के . साथ अरिथमेटिक ऑपरेशन कर रहे हैं, तो हमें करस्पॉन्डिंग वैल्यू मिलेंगे।
For example:

>>> a = 10 
>>> a > True 
True 
>>> a < True 
False 
>>> 10 > True 
True 
>>> 3.0 < False 
False 
>>> True < False 
False
>>> True <= False 
False 
>>> True > False 
True 
>>> True >= False 
True 
>>> True == False 
False 
>>>True != False 
True

Relational operators chaining
रिलेशनल ऑपरेटर्स चेनिंग

We can also use relational operators together. This concept is called relational operators chaining. These operators can be arranged arbitrarily. They will be used as a chain. So for an example, if the expression is x <y <z, then it is similar to the x<y and y < z. हम रिलेशनल ऑपरेटर्स को भी एक साथ इस्तेमाल कर सकते हैं। इस अवधारणा को रिलेशनल ऑपरेटर्स चेनिंग कहा जाता है। इन ऑपरेटरों को मनमाने तरीके से व्यवस्थित किया जा सकता है। उन्हें एक चेन के रूप में इस्तेमाल किया जाएगा। तो एक उदाहरण के लिए, यदि एक्सप्रेशन x < y <z है, तो यह x < y और y <z के समान हैं।
Example 1:

a = 10 
b = 20 
c = 5 # c < a < b'is same as c <a and a < b 
print (c <a) 
print (a < b) 
print (c <a < b) # b is not in between 40 and 60
print (40 <= b <= 60) 
# ais 10, which is greater than c print (a == 10 > c)

Output:
True
True
True
False

In chaining operations, if all comparisons return ‘True’, then the result is ‘True’. If at least one comparison is ‘False’, then the result is ‘False’.

चेनिंग ऑपरेशन में, यदि सभी कम्परिजन रिटर्न ‘टू’ है, तो परिणाम ‘टू’ होगा। यदि कम से कम एक कम्पेरिजन ‘फाल्स’ होगा, तो रिजल्ट ‘फाल्स’ होगा।
For example:

>>> 10<20
True
>>> 10<20<30
True
>>> 10<20<30<40 
True 
>>> 10<20<30<40>50 
False

Note: In Python,’==’ and ! =’operators are also called as “equality operators”.
पॉयथन में , ‘==’ और ‘!= ऑपरेटर  को एक्वेंलटी ऑपरेटर के नाम से भी जाना जाता है

>>> 10 == 20 
False 
>>>10 != 20 
True 
>>> 1 == True 
True 
>>> 1 == 1.0 
True 
>>> 'hello' == 'hello' 
True 
>>> 10 == 'hello' 
False 
>>> 10 == '10' 
False 
>>> 1 == 2 == 3 == 4 
False 
>>> 1 == "True" 
False 
>>> "r" == "r" == 'r'
True 
>>> 3 == 3.0 
True

Difference between ‘==’ and ‘is’ operator
‘==और ‘is’ ऑपरेटर के बीच अंतर

‘==’ operator is for ‘content comparison only’ but ‘is’ operator for ‘address comparison’ or ‘reference comparison only’.

‘==’ ऑपरेटर ‘केवल कंटेंट कम्पेरिजन’ के लिए है जबकि is’ ऑपरेटर ‘एड्रेस कम्पेरिजन’ या ‘केवल रिफरेन्स कम्पेरिजन’ के लिए ।
For example:

a = [1,2,3] 
b = [1,2,3] 
print (id(a)) 
print(id (b)) 
print (a is b) 
print (a == b) 
c = a
print (a is c) 
print (a == c)

Output:
51900200
10670216
False
True
True
True

Chapter wise Model Paper Link

About Me