def learn_python():

Welcome to the third module–you’ll be halfway done at the end of this week. More power to you! You deserve this cute kitten.

cute kitten image

Now let’s push forward to learn about lists and functions. What are those, you ask?

Let’s start with the list. It is a very useful way of storing a lot of variables. Let’s use our Tetris project again to explain this concept very plainly. In the game there are 6 types of shapes that are falling down the board. Until now we had to store the value like this: J_shape = "_|", and then write out print J_shape, but there is a way we can avoid having a new variable for each of the shapes and just store it like this:

shapes = ["_|", "|_|_|", "|_", "_/˜", "|", "˜\_"]

Now we can “walk” through the list and print out each shape, like this:

for shape in shapes:
  print shape

list is just one of the so called “data structures” that Python has. It is probably the most widely used data structure. Next week we will also read about dict, which are similar, but more powerful.

On to Functions!

Functions are usually a command or a list of commands that are repeated throughout the program. Let’s say we would like to set a color for each of our shape and then print it out. We could do it like this:

for shape in shapes:
  get_color_and_print(shape)

And the function would look something like this:

def get_color_and_print(shape):
  if shape == '_|':
    print '\033[95m' + shape + '\033[0m'
  elif shape == '|_|_|'
    print '\033[94m' + shape + '\033[0m'
  .
  .
  .

If you can explain it better, or if you have any questions, then please do not hesitate to come to the discussion forums and ask for explanations and (even more importantly) give some too. Find me and the others and have a laugh with us, we are always talking about some interesting stuff.

All right? We are getting really proper now, so let’s get this week’s tasks out:


Reading

Get to know the Functions and Lists more deeply in chapter 3, sections 6.5, 6.6, 6.7, 6.8, 6.9 and Chapter 8 from the online textbook How to Think Like a Computer Scientist. They are the very building blocks of computer programing, so yeah… we’re into some serious depth now.

Trying it out

We will be typing and thinking away at Codecademy doing Functions, Lists and functions units.

Exercises

From the MIT OCW’s course A Gentle Introduction to Programming Using Python

  • Complete sections 2.0-2.6 and 2.7 from a Handout 2

Project

Now we will really put our new gained knowledge to the test. Let’s make proper, decent looking Tetris shapes. We will use the graphics.py library from the MIT OCW’s course A Gentle Introduction to Programming Using Python.

Just make a new file called graphics.py and then copy everything on that page and paste it to that file. Be sure to save this in the same directory where your code is saved! We can use all of this code now with the import declaration on the top of the file like this: from graphics import *. I find that a very good way of learning about the code is to read it. A lot of the time I can’t make heads and tails of it, but sometimes it happens that I understand a little portion of what it says, and that shows me that I am progressing, even if I’m feeling that I am going backwards with my learning. So let’s prepare our tetris.py file to import graphics.py module and then we can open a window like this:

win = GraphWin('Rect', 800, 500)

and add win.mainloop() at the end of the file, so the window will stay opened.

Now we can draw on this window and since I like rectangular shape, let’s draw one:

Rectangle(Point(200, 200), Point(400, 400)).draw(win)

Neat, right?

OK, I would like to start with something easy. Can you show me how you drew the shape I Shape (four rectangles with different coordinates). I would much appreciate your help! Find me here.

Also, don’t forget: You don’t need to feel bad if you are struggling to keep up. After all, we’re not keeping score here, we’re helping each other to learn Python. Just keep trying, ask some questions, join the discussions; I promise you’ll get there.

Much happy Pythoning.

moocie face
–Your mechanical friend MOOC-E


Previous section: