PYTHON tutorials #5 :Operators


SUBMITTED BY: Guest

DATE: Dec. 16, 2013, 8:35 a.m.

FORMAT: Python

SIZE: 1.3 kB

HITS: 878

  1. # for looping structure
  2. # for <var> in <iterable> :
  3. # <suite>
  4. # keyword parameter : end is a parameter
  5. # does not create a variable
  6. for i in [0, 1, 2, 3, 4] :
  7. print(i, end ="\t")
  8. #print(end) # error
  9. print()
  10. for i in range(5) :
  11. print(i, end ="\t")
  12. print()
  13. # what is the relationship bet loop variable and the iterable object?
  14. # loop variable is always a copy of the element of the iterable object
  15. # it could be a copy of value type or copy of reference type
  16. # any change to a copy of value type will not affect the iterable object
  17. # any assignment to a copy of ref type will not affect the iterable object
  18. # any change to a copy of ref type will affect the iterable object
  19. a = [11, 22, 33, 44]
  20. for i in a :
  21. print(i, end = "\t")
  22. i += 100
  23. print()
  24. print(a) # no change
  25. a = [[11], [22], [33], [44]]
  26. for i in a :
  27. print(i, end = "\t")
  28. i = i + [100]
  29. print()
  30. print(a) # no change
  31. a = [[11], [22], [33], [44]]
  32. for i in a :
  33. print(i, end = "\t")
  34. i.append(100)
  35. print()
  36. print(a) # change
  37. a=[12]
  38. c=[9]
  39. a+[3]
  40. c.append(6)
  41. print(a,c)
  42. print([12,23]+[23])

comments powered by Disqus