DSCC mini workshop 1: Difference between revisions

From CommunityData
No edit summary
 
Line 50: Line 50:


== The second mini workshop==
== The second mini workshop==
[[DCSS Mini Workshop 2]]
[[DSCC Mini Workshop 2]]

Latest revision as of 20:30, 19 April 2016

Do you have python installed? If not go here: CDSW/Day_0_setup_and_tutorial.


Understanding Lists[edit]

You can [go here][1] 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[edit]

Python Tutor pythontutor.com is a useful educational tool that can help you visualize the execution of your code.

Click "edit code" to try out your code! 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[edit]

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[edit]

DSCC Mini Workshop 2