IKH

Logical Operators

  • Logical operators – and, or, not.
  • We can apply logical operators for all types.

For boolean types behavior’s

  • and ⇒ If both arguments are True then only result is True.
  • or ⇒ If at-least one argument is True then result is True.
  • not ⇒ complement.

Example

Python
print(True and False)
print(True or False)
print(not False)

Output

PowerShell
False
True
True

For non boolean types behavior’s

  • 0 means False.
  • Non-zero means True.
  • Empty string is always treated as False.

Example

Python
print(1 and 0)
print(1 or 0)
print(not "")

Output

PowerShell
0
1
True

x and y

  • if x is evaluates to false return x otherwise return y.
  • If first argument is zero then result is zero otherwise result is y.

Example

Python
print(10 and 20)
print(0 and 20)

print("ajay" and "ajay shukla")
print("" and "ajay")
print("ajay" and "")

Output

PowerShell
20
0
ajay shukla
""
""

x or y

  • If x evaluates to True then result is x otherwise result is y.

Example

Python
print(10 or 20)
print(0 or 20)

print("" or "ajay")
print("ajay" or "")

Output

PowerShell
10
20
ajay
ajay

not x

  • If x is evaluates to False then result is True otherwise False.

Example

Python
print(not 10)
print(not 0)

print(not "")
print(not "ajay")

Output

PowerShell
False
True
True
False

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!

Report an error