Teaching Beginners Python at CoderDojoGirls@DCU

Related event: Coding Grace at CoderDojoGirls@DCU (Sat 08 Nov 2014)

It was a wet, or so the Irish would say, a soft morning. Mick and I (Vicky) arrived at DCU and found the venue. Everyone was setting up. It might be 10AM but there's a buzz in the air.

Here are the two videos of Grace Hopper I mentioned during the introduction about Coding Grace:

Once we settled down, and was introduced by Niambh, we got started setting folks machines up, it was mostly Windows and we do have a few Macs.

One of the few things we had to do was make sure everyone had Python on their laptops, Sublime Text Editor installed and the workshop content copied over and unzipped.

See if Python works

Mac OSX and Linux

For Mac and Linux users, Python is on by default. To check if it works

  • ⌘ + Space
  • Type "terminal" and hit ⏎
  • The following should appear

    Mac OSX terminal

  • Type python and the following should appear, if you see >>> then it is working, this is called the Python interpretor. Only Python code works here.

    Opening the Python interpretor on Mac OSX terminal

  • To exit the Python interpretor, type exit() and hit ⏎. This should bring you back to command line. Typing Python code doesn't work here.

    Exiting out of Python interpretor and back to Command Line

Windows

For those on Windows, this is what we did to set the environment settings, this makes life a lot easier when we start using CMD:

  • Click on Windows button.
  • Right-click on the Computer, and click on Properties
  • Click on Advanced system settings (refer to the image below)
  • Click on the Environment Variables... button (refer to the image below)
  • Under "Systems variables", scroll down to find Path, click on it to highlight it. (refer to image below)
  • Click on Edit... button (refer to image below)
  • Hit the right arrow key on your keyboard to make sure you are at the end of the text.
  • Type the following ;c:\python27
  • Click "OK" to close dialogs.

To find out if it's set up properly, we need to open Command Prompt.

  • Click on Windows button.
  • Type cmd in the search and hit ⏎.
  • Command Prompt
  • Type python, you should see >>> appear with details of the version of Python it's running.

A couple of things to do after installing Sublime Text editor.

Sublime Text Editor settings for Python before using it

Before we start using Sublime Text Editor, make sure you have the following set so Python-related code gets highlighted (easy to look at at, and makes it nice to use), and that we have the indentation set properly. The latter is important as part of the best programming practices in Python (see PEP8).

  • Make sure that the file is set to Python, if you open a completely new file, it's default is Plain Text. See image above.
  • Make sure that Tab Size:4 and also that Indent Using Spaces is checked. See image above.

On with the show...

There were 3 command line commands that was useful to know when navigating in the CMD (Windows)/Terminal (MacOSX/Linux).

  • pwd
    • Present working directory: where you are in the file system right at this moment.
  • ls (on Windows, it's dir)
    • List what is in the current directory you are in, e.g. files and other directories.
  • cd
    • Change directory: Go to a directory with this command. Say you are in "My documents" and you have a directory called "Projects", you will type cd Projects.
    • NOTE: If you ever get lost navigating your way around the file system, just type cd and hit ⏎. It will return you to your home directory.

Tips

  • Have your Windows Explorer / Finder (MacOSX) open alongside and you will see where you are and what directory you are going into.
  • For Mac users, you can drag the icon of an app to your Terminal to get the full path to that file. Just delete the name of the file and you have the full path.

    Drag and drop to get full path to a given file

If you want to learn more about CLI (Command Line Interface), see Coding Grace's CLI workshop slides

NOTE: One thing I didn't mention why command line is useful to learn, when you run a Python script, if there are any errors, you can see it right away in the CMD/Terminal. On Windows, if you run by double-clicking on the Python script, if there's an error, it will just close without a way for you to see what went wrong.

game_01.py

This shows where Python starts executing the code, in this case, on line 5 as if encounters if __name__ == '__main__'. Otherwise it will execute everything from line 1 onwards.

This script also contains a function, starts with def followed by the name of your function followed by :.

On the next line, it's 4 spaces (tab but make sure your editor has set "Indent to 4 spaces"), and your code.

To run this script via command line:

  • Open CMD (Windows) /Terminal (MacOSX/Linux).
  • It is important that you are in the same directory as your the Python script you are going to run. Use pwd, ls (and cd) to get to the directory where game_01.py is located.
  • Type: python game_01.py.
  • It should print "hello" if it works.

game_02.py

Here, I am introducing some interactiveness and the beginnings of a game. It will ask for your name and it will print it out.

Grand so far, now for some editing of game_02.py.

Change

print(raw_input("What's your name? > "))

to

player_name = raw_input("What's your name? >")

Now we have a variable (a way for the computer to store information like strings and numbers).

player_name is a variable and when you type your name and hit ⏎, whatever you types will be stored in player_name.

If you run the game_02.py, it seems that nothing is happening, but it has stored your name after you entered it, but that's all it's doing. To see what player_name stored, we will print it out.

On the next line in game_02.py, type:

print("Your name is {}".format(player_name.upper()))

This looks funny, but what it is doing is telling Python that when it sees anything between and opening and closing ", it is a string.

And in that string, if it sees {}, then it is replaced with something such as a string from a variable called player_name in this case.


For those on Python 2.6.x, we encountered an error when running game_02.py.

ValueError: zero length field name in format.

If you come across errors:

  • See if there's any typos.
  • Don't be afraid to do a web search on it (we all do it).

Regarding the zero length field name error, we realised it was the version of Python encountering {} in string formatting. In 2.7 (onwards), using {} is fine. If using Python 2.6.x, substitute {} with {0} and it should work now.


But sadly time flew fast and the workshop has to come to an end. It was a wonderful morning, so much enthusiasm and fun.

Big thanks to Niambh and CoderDojoGirls@DCU for inviting Mick and myself as Coding Grace.

Questions/Feedback

If you have any questions, you can email me at vicky@codinggrace.com. You can also leave a comment below. :-)

Our website for more information of upcoming events is codinggrace.com.

Other places to find us: Mailing list | @CodingGrace | FaceBook

References

Comments

comments powered by Disqus