Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Demonstrate various methods to create a list of numbers in Python.
- # Copyright 2023 Roland Richter [Mu: Python 3]
- # If you have to create a list which holds numbers 0, 40, 80, ..., 400,
- # there are several methods to do it:.
- # 1. You could simple write a list with all items in it:
- nums_a = [0, 40, 80, 120, 160, 200, 240, 280, 320, 360, 400]
- # ... really tedious typing, isn't it? There are better ways.
- # 2. The second way is still a bit tedious:
- nums_b = [] # Create an empty list, then ...
- num = 0
- while num <= 400: # ... (until 400 is reached)
- nums_b.append(num) # ... start appending numbers: first 0, ...
- num += 40 # ... then 40, 80, and so on.
- # Now, open a handbook such as
- # https://www.w3schools.com/python/python_strings.asp
- # and start to FIX my code; but ONLY code inside ↓ / ↑ lines!
- # 3. Python provides another way to loop through numbers:
- # the "for" loop with the range(start, stop, step) function, where
- # start is the integer to start the iteration with,
- # stop is the integer to stop with (not included),
- # step is the integer specifying the increment.
- # A common source of errors is the fact that stop is NOT included.
- # FIX the numbers in "range" to create all numbers 0, 40, ... 400:
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- nums_c = []
- for num in range(0, 400, 40):
- nums_c.append(num)
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- # 4. Two of the arguments of range(start, stop, step) are optional:
- # start is the integer to start with -- optional (default: 0),
- # stop is the integer to stop with (not included) -- required,
- # step is specifying the increment -- optional (default: 1).
- # So, writing "for num in range(10)" means "for num in range(0, 10, 1)",
- # which is another way of saying "loop over 10 numbers, from 0 to 9".
- # FIX the numbers in "range" to create all numbers 0, 40, ... 400:
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- nums_d = []
- for idx in range(10):
- nums_d.append(40 * idx)
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- # 5. Python provides an even shorter syntax, called list comprehension.
- # FIX the (hopefully obvious) computation error here.
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- nums_e = [42 * idx for idx in range(11)]
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- # 6. Your turn!
- # Create [0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165]
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- nums_f = []
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- # 🖐 HALT! No code editing below this line!
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- nums40 = [0, 40, 80, 120, 160, 200, 240, 280, 320, 360, 400]
- nums15 = [0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165]
- assert nums_a == nums_b, "\n nums_b is " + str(nums_b) + "\n expected: " + str(nums40)
- assert nums_a == nums_c, "\n nums_c is " + str(nums_c) + "\n expected: " + str(nums40)
- assert nums_a == nums_d, "\n nums_d is " + str(nums_d) + "\n expected: " + str(nums40)
- assert nums_a == nums_e, "\n nums_e is " + str(nums_e) + "\n expected: " + str(nums40)
- assert nums_a == nums_f, "\n nums_f is " + str(nums_f) + "\n expected: " + str(nums15)
- print("+---------+---------+---------+---------+---------+---------+")
- input("FIXED You did it!")
- # ----------------------------------------------------------------------
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment