learn how operators work in python


SUBMITTED BY: Guest

DATE: Dec. 15, 2013, 8:30 a.m.

FORMAT: Python

SIZE: 1.2 kB

HITS: 864

  1. # operators
  2. # + - * / // ** %
  3. # * : polymorphic:
  4. # numbers : multiplicative
  5. # str and int : replicative
  6. # list and int : replicative
  7. # tuple and int : replicative
  8. print(3 * 4, 2*"pes"'q'*2, 3*[1, "2"]*1 , 2*(1, 2) * 2)
  9. print(4 * "pes")
  10. #print({1:2} * 4)
  11. print(3 + 4 , "pes" + "it", [1, 2] + [3, 4])
  12. # ** : exponentian op; right associative
  13. print(2 ** 3, 2 ** 3 ** 2) # 8 512
  14. print(26 /5, 26.0// 5, 25 % 4, 25.8 % 4.2)
  15. # relop : == <= >= != < > in
  16. # bool : True False
  17. print (5 == 5) # True
  18. print (5 == 5 == 5) # True
  19. # a == b == c => (a == b) and (b == c)
  20. # True : 5 -5 "pes" [11] non empty data structure
  21. # False: 0 "" [] empty data structure
  22. # logical op :
  23. # 'C' 1 && 2 : 1
  24. # 1 & 2 : 0
  25. # Python: logical : not and or
  26. # bitwise & | ^ ~ << >>
  27. # short ckt eval
  28. # eval left to right logical expr
  29. # stop as soon as the truth or falsehood is found
  30. # a = 0; b = 10
  31. # c = b / a > 2 # error
  32. # c = (a == 0) or (b / a > 2)
  33. a = b = 10
  34. print(a, b)
  35. a = (b != 10)
  36. print(a, b)

comments powered by Disqus