Editing DSCC mini workshop 1

From CommunityData

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 2: Line 2:
If not go here: [[CDSW/Day_0_setup_and_tutorial]].
If not go here: [[CDSW/Day_0_setup_and_tutorial]].


== Understanding Lists ==
You can ['''go here'''][http://greenteapress.com/thinkpython/html/thinkpython011.html] for a quick overview of lists
- A list is simply a sequence of values e.g [1,2,3,4,5,6]
- It is often identified through the use of square brackets []
- A list can contain integers, strings or other lists. There are all lists: [10, 20, 30, 40], ['crunchy frog', 'ram bladder', 'lark vomit'],
  ['crunchy frog', ['ram bladder', 'lark vomit']]


== Exercise 1: Python Tutor ==
== Exercise 1: Python Tutor ==
Line 17: Line 7:


Click "edit code" to try out your code!
Click "edit code" to try out your code!
Let's try the Fibonacci sequence! It is a common introductory program.  
Let's try the Fibonacci sequence! It is a common introductory program.
 
    l = [0,1]
    for i in range(0,10):
        l.append(l[-1]+l[-2])
    print(l)
 
Notice what [-1] and [-2] do. Negative indexing lets you get the elements at the end of a list!
Python Tutor is also useful for understanding how functions work. We will learn how to define our own functions later.
 
 
== Exercise 2: Assigning Speeches ==
Many Comgrads TA Speech Communication.  Imagine you have 30 students in your quiz section.
There are 6 speech assignments in a quarter. Every student gives a speech.
Some students do not like being the first to give their speech and other students do not like going last.
To make things fair we should randomize the order of the speeches and make sure that all students will get a chance to give their speech early or late in order.
Write a program to generate the list of speech assignments to solve this problem!
 
    # begin by assigning each student a number 0 .. 29
    students = list(range(0,29))
 
    # use the random module to shuffle the students.
    import random
    random.shuffle(students)
    # this can be our speech order for week 1.
    print(students)
 
    # now that we have randomized the students one time,
    # how can we make sure that students don't have to give their speeches first or last more than once per quarter?
    # hint: you might try using indexes or slices in a loop


== The second mini workshop==
<code>
[[DSCC Mini Workshop 2]]
l = [0,1]
for i in range(0,10):
    l.append(l[-1]+l[-2])
print(l)
</code>
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see CommunityData:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel Editing help (opens in new window)