Skip to content

Latest commit

 

History

History
122 lines (96 loc) · 1.4 KB

arithmatics.md

File metadata and controls

122 lines (96 loc) · 1.4 KB

Addition

a, b = 4, 2
c = a + b
print(c)
# => 6

Substraction

a, b = 4, 2
c = a - b
print(c)
# => 2

Multiplication

a, b = 4, 2
c = a * b
print(c)
# => 8

Division

Get float result

a, b = 4, 2
c = a / b
print(c) 
# => 2.0

Get integer result

a, b = 4, 2
c = a // b
print(c)
# => 2

Power

a, b = 4, 2
c = a ** b # a to the power of b (4^2)
print(c)
# => 16

Reminder

a, b = 4, 2
c = a % b
print(c)
# => 0

Sign change

a, b = 4, 2
c = -a
print(c)
# => -4

Compare values

Check if two values are the same

a, b = 4, 2
a = 1
b = 1
print('a == b', a == b)
# => a == b True

Check if two values are not the same

a = 1
b = 2
print( 'a != b', a != b )
# => a != b True

Compare object ids (memory locations)

If two variables point to the same value

a = 1
b = c = 2
print('a is b', a is b)
# => a is b False
print('b is c', b is c)
# => b is c True

This operation is same as id(a) == id(b) (https://docs.python.org/3/library/functions.html#id)

If two variables point to the different values

a = 1
b = 1
print( 'a is not b', a is not b ) # a is b
# => a is not b False

💡 Unsupported operations in Python

  • x++ and x--
  • ++x and --x

💡 Order of the execution

() => +x => -x => ** => * => / => % => + => -