Found 10784 Articles for Python

What is different in OR and AND operators in Python?

Malhar Lathkar
Updated on 26-Feb-2020 10:21:41

5K+ Views

In Python, and and or (along with not) are defined as logical operators. Both require two operands which may evaluate to true or false.The and operator returns True only if both operands are True.>>> a=50 >>> b=25 >>> a>40 and b>40 False >>> a>100 and b>> a==0 and b==0 False >>> a>0 and b>0 TrueThe or operator returns True if either operand is true.>>> a=50 >>> b=25 >>> a>40 or b>40 True >>> a>100 or b>> a==0 or b==0 False >>> a>0 or b>0 True

What is different in | and OR operators in Python?

Malhar Lathkar
Updated on 26-Feb-2020 10:22:24

1K+ Views

In Python or is a logical operator and | is a bitwise operator. The or operator requires two opeans of any type and may be true or false. It returns true if any one operand evaluates to true.>>> a=50 >>> b=25 >>> a>40 or b>40 True >>> a>100 or b>> a==0 or b==0 False >>> a>0 or b>0 TrueThe | operator takes bits as operands and returns 1 if any one operand is 1>>> a=10   #0000 1010 >>> bin(a) '0b1010' >>> b=20   #0001 0100 >>> bin(b) '0b10100' >>> c=a|b >>> c 30         #0001 1110 >>> bin(c) '0b11110'

What is different in & and AND operators in Python?

babita aryaa
Updated on 30-Jul-2019 22:30:21

364 Views

Yes, they both are different as AND is a logical operator which is used two compare between two values whereas  & is used as a bitwise operator.

What are boolean operators in Python?

Malhar Lathkar
Updated on 26-Feb-2020 10:23:13

7K+ Views

The logical operators and, or and not are also referred to as boolean operators. While and as well as or operator needs two operands, which may evaluate to true or false, not operator needs one operand evaluating to true or false.Boolean and operator returns true if both operands return true.>>> a=50 >>> b=25 >>> a>40 and b>40 False >>> a>100 and b>> a==0 and b==0 False >>> a>0 and b>0 TrueBoolean or operator returns true if any one operand is true>>> a=50 >>> b=25 >>> a>40 or b>40 True >>> a>100 or b>> a==0 or b==0 False >>> a>0 or ... Read More

What is modulo % operator in Python?

Malhar Lathkar
Updated on 26-Feb-2020 10:23:56

365 Views

The % symbol is defined in Python as modulo operator. It can also be called remainder operator. It returns remainder of division of two numeric operands (except complex numbers).>>> a=10 >>> b=3 >>> a%b 1 >>> a=12.25 >>> b=4 >>> a%b 0.25 >>> a=-10 >>> b=6 >>> a%b 2 >>> a=1.55 >>> b=0.05 >>> a%b 0.04999999999999996

What is tilde (~) operator in Python?

Malhar Lathkar
Updated on 18-Jun-2020 08:35:21

5K+ Views

The bitwise operator ~ (pronounced as tilde) is a complement operator. It takes one bit operand and returns its complement. If the operand is 1, it returns 0, and if it is 0, it returns 1For example if a=60 (0011 1100 in binary) its complement is -61 (-0011 1101) stored in 2's complement>>> a=60 >>> bin(a) '0b111100' >>> b=~a >>> a 60 >>> >>> b -61 >>> bin(b) '-0b111101

What is behavior of ++ and -- operators in Python?

Pythonista
Updated on 26-Feb-2020 07:36:49

159 Views

In C/C++ and Java etc, ++ and -- operators are defined as increment and decrement operators. In Python they are not defined as operators.In Python objects are stored in memory. Variables are just the labels. Numeric objects are immutable. Hence they can't be incremented or decremented.However, prefix ++ or -- doesn't give error but doesn't perform either.>>> a=5 >>> b=6 >>> ++a 5 >>> --b 6Postfix ++ or -- produce errors>>> a=5 >>> b=6 >>> a++ SyntaxError: invalid syntax >>> b-- SyntaxError: invalid syntax

Explain function of % operator in Python.

Pythonista
Updated on 26-Feb-2020 07:37:20

349 Views

In Python % is an arithmetic operator that returns remainder of division operation. It is called modulo or remainder operator and operates upon numeric operands except complex numbers>>> a=10 >>> a%3 1 >>> a%5 0 >>> b=12.5 >>> b%2.5 0.0 >>> b%2 0.5

What is function of ^ operator in Python

Pythonista
Updated on 26-Feb-2020 07:37:55

358 Views

In Python, ^ is called EXOR operator. It is a bitwise operator which takes bits as operands. It returns 1 if one operand is 1 and other is 0.Assuming a=60 (00111100 in binary) and b=13 (00001101 in binary) bitwise XOR of a and b returns 49 (00110001 in binary)>>> a=60 >>> bin(a) '0b111100' >>> b=a^2 >>> bin(b) '0b111110' >>> a=60 >>> bin(a) '0b111100' >>> b=13 >>> bin(b) '0b1101' >>> c=a^b >>> bin(c) '0b110001'

What is "not in" operator in Python?

Pythonic
Updated on 09-Sep-2023 15:12:50

5K+ Views

In Python, 'not in' membership operator evaluates to true if it does not finds a variable in the specified sequence and false otherwise. For example >>> a = 10 >>> b = 4 >>> l1 = [1,2,3,4,5] >>> a not in l1 True >>> b not in l1 False Since 'a' doesn't belong to l1, a not in b returns True. However, b can be found in l1, hence b not in l1 returns False

Advertisements