Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1.  
  2. # FOR LOOPS AND WHILE LOOPS
  3.  
  4. # range returns a list of integers
  5. ```python
  6. range(0, 3) # returns [0, 1, 2]: includes first value but excludes second value
  7. range(3) # same thing: starting at zero is the default
  8. range(0, 5, 2) # returns [0, 2, 4]: third argument specifies the 'stride'
  9. ```
  10.  
  11. ## for loop (not recommended)
  12. ```python
  13. fruits = ['apple', 'banana', 'cherry']
  14. for i in range(len(fruits)):
  15. print fruits[i].upper()
  16. ```
  17.  
  18. ## alternative for loop (recommended style)
  19. ```python
  20. for fruit in fruits:
  21. print fruit.upper()
  22. ```
  23.  
  24. ## use xrange when iterating over a large sequence to avoid actually creating the integer list in memory
  25. ```python
  26. for i in xrange(10**6):
  27. pass
  28. ```
  29.  
  30. ## iterate through two things at once (using tuple unpacking)
  31. ```python
  32. family = {'dad':'homer', 'mom':'marge', 'size':6}
  33. for key, value in family.items():
  34. print key, value
  35. ```
  36.  
  37. ## use enumerate if you need to access the index value within the loop
  38. ```python
  39. for index, fruit in enumerate(fruits):
  40. print index, fruit
  41. ```
  42.  
  43. ## for/else loop
  44. ```python
  45. for fruit in fruits:
  46. if fruit == 'banana':
  47. print "Found the banana!"
  48. break # exit the loop and skip the 'else' block
  49. else:
  50. # this block executes ONLY if the for loop completes without hitting 'break'
  51. print "Can't find the banana"
  52. ```
  53.  
  54. ## while loop
  55. ```python
  56. count = 0
  57. while count < 5:
  58. print "This will print 5 times"
  59. count += 1 # equivalent to 'count = count + 1'
  60. ```
  61.  
  62.  
  63. ## COMPREHENSIONS
  64.  
  65. ## for loop to create a list of cubes
  66. ```python
  67. nums = [1, 2, 3, 4, 5]
  68. cubes = []
  69. for num in nums:
  70. cubes.append(num**3)
  71. ```
  72.  
  73. ### equivalent list comprehension
  74. ```python
  75. cubes = [num**3 for num in nums] # [1, 8, 27, 64, 125]
  76. ```
  77.  
  78. ### for loop to create a list of cubes of even numbers
  79. ```python
  80. cubes_of_even = []
  81. for num in nums:
  82. if num % 2 == 0:
  83. cubes_of_even.append(num**3)
  84. ```
  85.  
  86. ### equivalent list comprehension
  87.  
  88. syntax: [expression for variable in iterable if condition]
  89. ```python
  90. cubes_of_even = [num**3 for num in nums if num % 2 == 0] # [8, 64]
  91. ```
  92.  
  93. ### for loop to cube even numbers and square odd numbers
  94. ```python
  95. cubes_and_squares = []
  96. for num in nums:
  97. if num % 2 == 0:
  98. cubes_and_squares.append(num**3)
  99. else:
  100. cubes_and_squares.append(num**2)
  101. ```
  102.  
  103. ### equivalent list comprehension (using a ternary expression)
  104. syntax: [true_condition if condition else false_condition for variable in iterable]
  105. ```python
  106. cubes_and_squares = [num**3 if num % 2 == 0 else num**2 for num in nums] # [1, 8, 9, 64, 25]
  107. ```
  108. # for loop to flatten a 2d-matrix
  109. ```python
  110. matrix = [[1, 2], [3, 4]]
  111. items = []
  112. for row in matrix:
  113. for item in row:
  114. items.append(item)
  115. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement