2018-03-12 HW

posted Mar 12, 2018, 5:42 AM by Konstantinovich Samuel   [ updated Mar 12, 2018, 10:55 AM ]
HW: Codingbat new section (list creation)


2. Functions can make lists by returning a list that was created and filled, this can be very useful!
On paper: Write a function that creates a list of the numbers 0 to N not including N.
#hint: remember we talked about how to add to a list from Thursday... (list methods)
#Feel free to use your notes!

def makeListUpTo(N):

makeListUpToN(2)  # returns [0,1]
makeListUpToN(5)  # returns [0,1,2,3,4]
makeListUpToN(10)  # returns [0,1,2,3,4,5,6,7,8,9]

Good News Everyone!

Parent-Teacher conferences are upon us. As such, I once again need goons to help me keep the parents in line. If you are available 3/15, Thursday, evening (5:15 - 8), or 3/16, Friday afternoon (12:45 - 3) and are interested, please:

Email me with 

Subject line: 
  PTC 2018 Spring Volunteer
    -State which day(s) you would like to help.
    -State if you can bring a laptop (not required!)
    -State why you picked me!


def makeListFromAtoB(a,b):

def makeListFromAtoBCountingByC(a,b,c):

New Python Commands:
1 - range() -> you just wrote it. Used to generate lists.

2 - for loop!

   #code does something to the variable

This creates a variable to store the elements of the list, and copies each element into the variable.

for x in "asd" :
   print x
#x is a variable created to hold the letters "a", "s" and "d"

a = 0
for thing in [1,2,3]:
   a += thing  
print a
#thing is a variable that holds 1, 2 then 3. 

Strings as an iterable

string = "Hello World"
for x in string:
    print x

x is assigned "H"

loop body executes.

x is assigned "e"

loop body executes.

x is assigned "l"

loop body executes.


Lists as an iterable

collection = ['hey', 5, 'd']
for x in collection:
    print x

x is assigned 'hey'

loop body executes.

x is assigned 5

loop body executes.

x is assigned 'd'

loop body executes.


You can still access by index with a for loop

s = "some words heh"
for i in range(len(s)):
   #do something to s[i]

nums = [1,2,3,8,7,4]
for i in range(len(nums)):
   #do something to nums[i]