184_notes:lists

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
184_notes:lists [2022/04/20 15:08] woodsna1184_notes:lists [2022/05/07 01:20] (current) woodsna1
Line 1: Line 1:
 ====== Coding: Lists ====== ====== Coding: Lists ======
  
-We often need a way to represent a group of objects or values in code. Python makes this easy with lists. A list gives a name to a collection of any Python objects. In Glowscript, we often use lists that include spheres, arrows, rings, and other shapes. Creating a list is easy:+We often need a way to represent a group of objects or values in code. Python makes this easy with lists. A list gives a name to a collection of any Python objects. You can think of a list like an excel spreadsheet, it has many rows (each representing an object), and each row has several columns (representing the attributes of the object).  
 + 
 +In Glowscript, we often use lists that include spheres, arrows, rings, and other shapes. Creating a list is easy:
  
 <code>  <code> 
Line 13: Line 15:
 </code> </code>
  
-You can think of a list like a column in an excel spreadsheet, it has many fields that can each be filled with an object. You can also access each bucket in a list to update or retrieve the object within it. One way is by using list indexing. Indices in a Python list start at 0 for the first element:+You can also access each bucket in a list to update or retrieve the object within it. One way is by using list indexing. Indices in a Python list start at 0 for the first element:
  
 <code> <code>
Line 20: Line 22:
 </code> </code>
  
-To change the value in a bucket, simply use = to assign to it:+To change the value in a bucket, simply use "=to assign to it:
 <code> <code>
 list_of_numbers[0] = 250               # Change the first element list_of_numbers[0] = 250               # Change the first element
Line 27: Line 29:
  
 ===== Lists and For Loops ===== ===== Lists and For Loops =====
 +==== Lecture Video ====
 +
 +{{youtube>Rvpmo95XBNA?large}} 
 We often want to move through a list and read or change each value one-by-one. The easiest way to iterate through a list in Python is using a for loop.  We often want to move through a list and read or change each value one-by-one. The easiest way to iterate through a list in Python is using a for loop. 
  
Line 42: Line 47:
 index = 0 index = 0
  
-while index < len(list_of_numbers): +while index < len(list_of_numbers):  # Conditional 
-    list_of_numbers[index] += 1 +    list_of_numbers[index] += 5      # Increase each number by 5 
-    index += 1+    index += 1                       # Increment the counter variable
 </code> </code>
  
  • 184_notes/lists.1650467308.txt.gz
  • Last modified: 2022/04/20 15:08
  • by woodsna1