Python Operators
या ट्यूटोरियलमध्ये आपण पायथनमधील विविध प्रकारच्या ऑपरेटर, त्यांचे वाक्यरचना आणि उदाहरणासह त्यांचे कसे वापरावे याबद्दल सर्व काही शिकू शकाल.
What are operators in python?
ऑपरेटर हे पायथनमधील विशिष्ट चिन्हे आहेत जी अंकगणित किंवा तार्किक गणना करतात. ऑपरेटर ऑपरेट केलेल्या मूल्याला ऑपरेंड म्हणतात.
उदाहरणार्थ:
>>> 2+3
5
येथे, + ऑपरेटर आहे जो वर्धापन करतो. 2 आणि 3 ऑपरेशन्स आहेत आणि 5 ऑपरेशनचे आउटपुट आहे.
Arithmetic operators
अंकगणित ऑपरेटर गणिती ऑपरेशन जसे की जोड, वजाबाकी, गुणाकार इ. करण्यासाठी करतात.
Operator | Meaning | Example |
---|---|---|
+ | Add two operands or unary plus | x + y+ 2 |
- | Subtract right operand from the left or unary minus | x - y- 2 |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus - remainder of the division of left operand by the right | x % y (remainder of x/y) |
// | Floor division - division that results into whole number adjusted to the left in the number line | x // y |
** | Exponent - left operand raised to the power of right | x**y (x to the power y) |
Example 1: Arithmetic operators in Python
x = 15
y = 4
# Output: x + y = 19
print('x + y =',x+y)
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x/y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: x ** y = 50625
print('x ** y =',x**y)
Output
x + y = 19 x - y = 11 x * y = 60 x / y = 3.75 x // y = 3 x ** y = 50625
Comparison operators
तुलना ऑपरेटर मूल्ये तुलना करण्यासाठी वापरले जातात. कंडिशननुसार ते एकतर ट्रू किंवा फॉल्स परत करते.
Operator | Meaning | Example |
---|---|---|
> | Greater than - True if left operand is greater than the right | x > y |
< | Less than - True if left operand is less than the right | x < y |
== | Equal to - True if both operands are equal | x == y |
!= | Not equal to - True if operands are not equal | x != y |
>= | Greater than or equal to - True if left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to - True if left operand is less than or equal to the right | x <= y |
Example 2: Comparison operators in Python
x = 10
y = 12
# Output: x > y is False
print('x > y is',x>y)
# Output: x < y is True
print('x < y is',x<y)
# Output: x == y is False
print('x == y is',x==y)
# Output: x != y is True
print('x != y is',x!=y)
# Output: x >= y is False
print('x >= y is',x>=y)
# Output: x <= y is True
print('x <= y is',x<=y)
Output
x > y is False x < y is True x == y is False x != y is True x >= y is False x <= y is True
Logical operators
Operator | Meaning | Example |
---|---|---|
and | True if both the operands are true | x and y |
or | True if either of the operands is true | x or y |
not | True if operand is false (complements the operand) | not x |
Example 3: Logical Operators in Python
x = True
y = False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
Output
x and y is False x or y is True not x is False
या ऑपरेटरसाठी सत्य टेबल आहे.
Bitwise operators
बिटवाईस ऑपरेटर ऑपेन्डर्सवर कार्य करतात जणू ते बायनरी अंकांचे तार असतात. ते थोड्या वेळाने ऑपरेट करतात, म्हणूनच ते नाव.
उदाहरणार्थ, बायनरीमध्ये 2 10 आहे आणि 7 111 आहे.
खालील सारणीमध्ये x = 10 (बायनरीमध्ये 0000 1010) आणि y = 4 (बायनरीमध्ये 0000 0100) द्या
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 ) |
Assignment operators
व्हेरिएबल्सला व्हॅल्यूज नियुक्त करण्यासाठी पायथनमध्ये असाइनमेंट ऑपरेटर वापरले जातात.
a = 5 एक सोपा असाईनमेंट ऑपरेटर आहे जो डावीकडील व्हेरिएबलला उजवीकडील 5 व्हॅल्यू देतो.
पायथनमध्ये अ + + 5 सारख्या अनेक कंपाऊंड ऑपरेटर आहेत जे व्हेरिएबलमधे जोडले जातात आणि नंतर तेच नेमून देतात. हे a = a + 5 च्या समतुल्य आहे.
Operator | Example | Equivalent to |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
//= | x //= 5 | x = x // 5 |
**= | x **= 5 | x = x ** 5 |
&= | x &= 5 | x = x & 5 |
|= | x |= 5 | x = x | 5 |
^= | x ^= 5 | x = x ^ 5 |
>>= | x >>= 5 | x = x >> 5 |
<<= | x <<= 5 | x = x << 5 |
Special operators
पायथन भाषा ओळख ऑपरेटर किंवा सदस्यता ऑपरेटर सारख्या काही खास प्रकारच्या ऑपरेटरची ऑफर देते. त्यांची उदाहरणे खाली वर्णन केली आहेत.
Identity operators
पायथनमधील ओळख ऑपरेटर आहेत आणि नाही. दोन व्हॅल्यूज (किंवा व्हेरिएबल्स) मेमरीच्या समान भागावर आहेत की नाही हे तपासण्यासाठी त्यांचा उपयोग केला जातो. दोन भिन्न जे समान आहेत याचा अर्थ असा नाही की ते एकसारखे आहेत.
Operator | Meaning | Example |
---|---|---|
is | True if the operands are identical (refer to the same object) | x is True |
is not | True if the operands are not identical (do not refer to the same object) | x is not True |
Example 4: Identity operators in Python
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
# Output: False
print(x3 is y3)
Output
False True False
येथे आपण पाहतो की x1 आणि y1 समान मूल्यांचे पूर्णांक आहेत, म्हणून ते समान तसेच एकसारखे आहेत. एक्स 2 आणि वाई 2 (स्ट्रिंग्स) च्या बाबतीतही असेच आहे.
पण x3 आणि y3 याद्या आहेत. ते समान आहेत परंतु एकसारखे नाहीत. कारण दुभाषिक ते समान असले तरीही त्यांना मेमरीमध्ये वेगळे शोधतात.
Membership operators
पायथनमधील सदस्यता ऑपरेटर आहेत आणि नाही. ते अनुक्रम (स्ट्रिंग, यादी, टपल, सेट आणि डिक्शनरी) मध्ये मूल्य किंवा चल आढळले की नाही हे तपासण्यासाठी त्यांचा उपयोग केला जातो.
शब्दकोशामध्ये आपण केवळ कीची उपस्थिती तपासू शकतो, मूल्य नाही.
Operator | Meaning | Example |
---|---|---|
in | True if value/variable is found in the sequence | 5 in x |
not in | True if value/variable is not found in the sequence | 5 not in x\ |
Example #5: Membership operators in Python
x = 'Hello world'
y = {1:'a',2:'b'}
# Output: True
print('H' in x)
# Output: True
print('hello' not in x)
# Output: True
print(1 in y)
# Output: False
print('a' in y)
Output
True True True False
येथे 'एच' एक्स मध्ये आहे परंतु 'हॅलो' एक्समध्ये नाही (लक्षात ठेवा पायथन केस सेन्सेटिव्ह आहे). त्याचप्रकारे, 1 ही एक की आहे आणि y अ शब्दकोषातील मूल्य अ आहे. म्हणून y मध्ये 'a' फालस रिटर्न करेल.
0 टिप्पण्या