User Tools

Site Tools


lists

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
lists [2023/10/27 02:27] appledoglists [2024/07/29 00:08] (current) appledog
Line 1: Line 1:
 = Lists = Lists
 +Note: This course can be skipped for older kids or people with prior experience. They can go to pyhang from here. They just need to know what a list is and what the difference is between a list and a string, and they can do pyhang (in general).
 +
 If you didn't know what a //Class// is in Python, you should know by the end of this lesson series. That is because this lesson series dives deep into what a list is, and we will also explore trees! We're not going to specifically focus on //'what is a class'//, it should be intuitive based on how the code works. If you didn't know what a //Class// is in Python, you should know by the end of this lesson series. That is because this lesson series dives deep into what a list is, and we will also explore trees! We're not going to specifically focus on //'what is a class'//, it should be intuitive based on how the code works.
  
Line 106: Line 108:
     num = 5     num = 5
     list = []     list = []
-     + 
-    for x in range(num):+    while len(list< num:
         f = random_fruit()         f = random_fruit()
         list.append(f)         list.append(f)
Line 138: Line 140:
     main()     main()
 </Code> </Code>
 +
 +== Homework
 +Now you can build a function that pairs students into groups. There basic idea is the same, but, make sure that the same student is not added to a group using the check "if name not in list:". This will allow you to make a group of students which do not contain the same student. If you wish to create a number of groups at the same time, you can use the pop, del, or remove commands. For example;
 +
 +<Code:Python>
 +    n = random.choice(len(students))
 +    s = students.pop(n)
 +    group.append(s)
 +</Code>
 +
 +The code above moves the student out of the list called "students" and adds them to the list called "group". The benefit of this is that since it removes the student from the pool of possible choices, you do not need to check for duplicates when randomly picking a student.
 +
 +== Next
 +Next, we can look at [[Lists II]], however, you might also want to look at [[Basics III]] to see how lists are used in action!
lists.1698373627.txt.gz · Last modified: 2023/10/27 02:27 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki