Python Fundamentals

Python And Operator

Python And Operator are used for operations between Python Variable – values, python operators divided into special groups, let’s see and learn

Python And Operator Types

as we said, python categorize operators into groups which is related to each other, so we can find :

  • Arithmetic {Math} operators
  • Assignment operators
  • Relational {Comparison} operators
  • Boolean {Logical} operators
  • Identity operators
  • Membership operators
  • Bitwise operators

so, let’s start knocking them out


Python And Operator ( Arithmetic )

  • +    : A + B
  • –     : A – B
  • *    : A x B
  • /     : A/B
  • **   : A Power(B)
  • //     : Floor division
  • %    : Reminder of A//B
# Numbers
# input() .. is a method to get inputs from user
num1 = float(input('input first number : '))
num2 = float(input('input second number : '))



# Operations
Output1 = num1 + num2
Output2 = num1 - num2
Output3 = num1 / num2
Output4 = num1 * num2
Output5 = num1 ** num2


# Showing things up
print('{0} + {1} = {2}'.format(num1, num2, Output1))
print('{0} - {1} = {2}'.format(num1, num2, Output2))
print('{0} / {1} = {2}'.format(num1, num2, Output3))
print('{0} * {1} = {2}'.format(num1, num2, Output4))
print('{0} ** {1} = {2}'.format(num1, num2, Output5))

Python And Operator ( Assignment )

it’s all about assigning values to variables,  ( = ) is not the only way to assign value to variable, so..! okay, let’s see

  • =    : Normal Assign
  • +=  : B += A .. B = B + A
  • -=   : B -= A .. B = B – A
  • *=   : B *= A .. B = B * A
  • /=    : B /= A .. B = B * A
  • //=   : B //= A .. B = B // A
  • %=  : B %= A .. B = B % A
  • **= : B **= A .. B = B ** A
John = 6.5
Marry = 2


print(Marry += John)
print(Marry -= John)
print(Marry *= John)
print(Marry /= John)
print(Marry //= John)
print(Marry %= John)
print(Marry **= John)

Python And Operator ( Comparison )

those operators checks and compare between values saved in variables, let’s see

  • ==   : A == B .. True/False .. Equal
  • !=   : A != B .. True/False .. Not Equal
  • <>  : A <> B .. True/False .. Not Equal
  • >    : A > B .. True/False .. Bigger
  • <    : A < B .. True/False .. Smaller
  • >=  : A >= B .. True/False .. Bigger or Equal
  • <=   : A <= B .. True/False .. Smaller or Equal
B = 5.3
John = 3.5

print(Marry == John)
print(Marry != John)
print(Marry <> John)
print(Marry > John)
print(Marry < John)
print(Marry >= John)
print(Marry <= John)

Python And Operator ( Logical )

okay, how to start, okay fine let’s do this, let’s say sarah has two girls marin and lora, they’re kids and she wants to ask them, follow

marin_money = 10
lora_money = 5

# Check if they still have the money
if marin_money == 10 and lora_money == 5:
  print ('True')

# Check if any girl still has her money
if marin_money == 10 or lora_money == 5:
  print ('True')

# Check if the money changed
if not(marin_money == 10 and lora_money == 5):
  print ('True')
  • and
  • or
  • not

Python And Operator ( Identity )

in hear ( FBI ) we could use this methods to check identity

  • is
  • is not

Python And Operator ( Membership )

we explained membership check with almost every data type, for example :  Python List and Python String

  • in
  • not in

Python And Operator ( Bitwise )

it works in binary, bit by bit operations so it’s not the right time or place to talk about

Write a Comment

Your email address will not be published. Required fields are marked *