Daniel de Castro

CPSC 217 (T01) - Winter 2009 (Old format)

CPSC 217 (T01) Introduction to Computer Sciences for Multidisciplinary Studies I

Daniel Castro

TA Sessions: Mondays and Wednesdays at 2pm (MSC 160)
CT Sessions: Wednesdays 3pm and Thursdays at 1pm (CT Desk)
Sorry... you need images

Week 11: April 13,15 - Demo Sessions

Week 11: April 06,08 - Reviews

Week 10: April 01 - Modules and Random

Slides

Week 10: March 30 - Parameters, Files and Exceptions

Slides

Week 9: March 18 - Python Debugger

Slides

Week 9: March 16 - Debugging

Slides

Week 8: March 11 - Functions and Turtle

Some questions from last tutorial:

Q) I have Ubuntu, but when I try to "import turtle", I receive an error.

A) On Ubuntu, you'll need a special package to work with turtle and other graphical modules called "Tkinter", this comes as default for windows. To install it, you type in the command line:

sudo apt-get install python-tk

It would request your password and then it will install the package (if you have internet connection working at that moment).

Q) How can I know where my turtle is?

A) There are two interesting functions from turtle that you might find useful:

1)

turtle.position()

gives you the current position in a list of two elements, where the first element is the X coordinate and the second element is the Y coordinate.

2)

turtle.heading()

gives you the current angle of the turtle (remember 0 means go to the right).

During my CT time, one student came to me saying that there was a problem with his account and the turtle only worked in the Python command line but not in his script.

What happened is that his script was called "turtle.py". YOU SHOULD NOT DO THIS. When you create a script called "turtle.py" and use "import turtle", it will import the very script you're writing, thus without any "turtle" functionality. And it will also create another file called "turtle.pyc" in your directory, so if you try to use "import turtle" later, it will use that "fake turtle" and keep not working, even if you rename your original file.

If this happens to you remember of deleting the "turtle.pyc" file too.

And here are the Slides from last tutorial

Week 8: March 2-4 - Assignment 2 Demo

Some have asked me how to do Assignment 3 from home. As this requires graphics, you cannot use SSH to connect to the university network. If you are a MacUser or a Linux User, this shouldn't be a problem, as most of them come with Python installed.

But if you are a Windows user, you need to install python. I dug a bit and found a windows version of Python 2.4.4 at http://www.python.org/download/releases/2.4.4/ (the installer is here). This is an old version of python (currently, the most used is 2.6.x but there's also a 3.x) but I chose this one because it is closer to the version we can find at the lab (2.4.3), which will avoid problems of compatibility.

When you install it, be sure that you are installing the Tcl/TK option, which is required for the "Turtle".

I did not test it yet, so if you have any problems, let me know.

Week 7: February 26 - Tips on sorting dictionaries

Consider a dictionary where my key is the Student Id and the value is the student's first name:

students = {1: 'daniel', 2: 'rachel', 3: 'john', 4: 'heather', 5:'james', 6: 'eric', 7:'james' }

How can I sort this data by student first name?

Note that this is quite different from our example in class, keys can be easily sorted as they are unique, but values can be repeated (james) and if I just try to sort the values, I might not be sure what was the key.

Remember: Dictionaries cannot be sorted. So you must convert all of this into a list. Dictionaries in python have a function items(), that you can call as follows:

student_list=students.items()

This will return a list of tuples, in this case a list containing the pairs (id,name). So, if you print it you'll have:

print student_list
[(1, 'daniel'), (2, 'rachel'), (3, 'john'), (4, 'heather'), (5, 'james'), (6, 'eric'), (7, 'james')]

Okay. Unlike dictionaries, lists can be sorted... but how can I sort by the second element?

The longer, but simpler way to undestand, is by creating a new list, swapping the pairs and then sorting this new list:

inverted_list=[]
for student in student_list:
inverted_list.append( (student[1], student[0]))
inverted_list.sort()
print inverted_list
[('daniel', 1), ('eric', 6), ('heather', 4), ('james', 5), ('james', 7), ('john', 3), ('rachel', 2)]

Hey, you said "longer and simpler"... so is there another way to do this?

Well... Yes. There is. And we can discuss this later, but, honestly, I'm afraid it could be quite confusing for beginners, so let's try this for now, okay?

Week 7: February 23, 25 - Midterm Review

Unix and Number conversions

Python: Here is a resume of what you are going to see on Wednesday, but, during the tutorial, we are going to focus on practical examples. I believe it could help to make things clear. The basic idea is not to give you the solutions, but pieces of a jigsaw puzzle. Figuring out how to put them together, you can, hopefully, solve your problems.

Download resume

Here you can download the files of our session today (Feb 25):

Slides classlist.dat grep.py sort.py cut.py emails_phil.py

Week 6: Reading Week

Week 5: February 9,11

Introduction to Python and questions about assignment 2

Week 4: February 2,4

Demo sessions

Assignment 1

Week 3: January 28

Free session

Questions about your assignments?

Week 3: January 26

Converting numbers

Here is the basic table for conversion

Decimal Binary Octal Hex
0 0000 0 0
1 0001 1 1
2 0010 2 2
3 0011 3 3
4 0100 4 4
5 0101 5 5
6 0110 6 6
7 0111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F

Week 2: January 23

Frequent Asked Questions (FAQ)

I just realized that it could be a good idea to make a FAQ page, so we can have a collection of common questions in a single place and it might make life easier.

This is the link to the new page: http://www.cpsc.ucalgary.ca/~dmncastr/w09/faq.html (hopefully, it'll be always "under construction")

Week 2: January 21

More unix commands

  • cut [-f LIST | -c LIST] FILE: display sections from each line of files. LIST is a number or a range (eg. -f 2 or -c1-23)
  • sort [-rn] FILE: sort a file (r is used to reverse and n to numeric comparisons)
  • uniq FILE: Discard all but one of successive identical lines in FILE
  • grep [options] PATTERN FILE: print lines matching a pattern (PATTERN should be quoted)
  • head [-NUMBER] FILE: print the first NUMBER lines of a file (default: 10)
  • tail [-NUMBER] FILE: print the last NUMBER lines of a file (default: 10)
  • diff FILE1 FILE2: shows differences betweeen two files
  • wc [-l] FILE: print the number of lines, words, and bytes in files (print just number of lines)

Redirection

  • COMMAND > FILE: send output to a new file (any previous data is discarded)
  • COMMAND >> FILE: append output to a file
  • COMMAND < FILE: send a file as input to a command
  • COMMAND | COMMAND: output of COMMAND1 is sent as input to COMMAND2

Saving session into a file

  • script FILENAME: start the file
  • CTRL+D: finish

Download task list

Week 2: January 19

Printing instructions for undergrad students

Unix commands

  • pwd: print name of working directory
  • cd [DIR]: change current directory
  • mkdir DIR: make directory
  • ls [-alR] [FILES]: list directory contents
  • cp SOURCE DESTINATION: copy files or directories
  • mv SOURCE DESTINATION: move (or rename) files or directories
  • rm [-r] FILES: remove files or directories
  • cat FILE: display contents from a file
  • more FILE: display contents in a page-by-page fashion
  • less FILE: improved "more"
  • man COMMAND: display description and options for a command

Download slides

Note: If you want to work from home, take a look at these pages: