Coding Grace at Women Tech Makers - Python Workshop

Related event: Learn Python by making a text-based adventure game @ GDG Dublin (Tue 31 Mar 2015)

This is a write-up for the Python workshop. :-)

Big thanks to the organisers of GDG Dublin and Women Tech Makers for inviting Coding Grace to run AngularJS (Serena) and Python (Vicky) workshops. A shout out and hugs to the mentors for helping out in both workshops and to everyone who came along and participated in the workshops. By the way, feel free to comment below with questions, feedback, anything I missed out in the post.

After a delayed start to the workshop and introduction...

We got stuck in making sure people's laptops are set up. For those using Windows, there's a small configuration needed to get python working via command line.

Details of this can be found in a previous blog post: http://www.codinggrace.com/news/teaching-beginners-python-coderdojogirlsdcu-2014-11-8/

By the time we had people set up, it was already break time at 19:30 for the scramble for food and some networking. We were back at 20:15(ish) and got right into the code.

You can find the source (without the special.py for the raffle) at our bitbucket repository, you can also download the zip from here.

Re-usable modules

Never made it to re-usable modules which was meant to cover before I ran the raffle for the fitbit.

  • Modify myfirstscript.py with the following and save the file:

    from game_01 import say_hello
    
    say_hello()
    
  • Go to your terminal/cmd and make sure you are not in the Python interpreter, i.e. no >>>.

  • Type python myfirstscript.py to run it from command line and see what happens.

Remember to check out the files ending with _comments.py, I've extra information and links to docs.

And while we are on the subject of documentation, check out Python's official docs at https://docs.python.org/2/.

During the workshop, we encountered if "__name__" == "__main__": very early on, this makes the Python script (or module) re-usable. This is pretty handy if you want to use the same function in multiple places in different files.

You can also do this via your terminal/cmd. So launch into your Python interpretor by typing python and you will see >>>. Now type the following:

>>> from game_01 import say_hello
>>> say_hello()

You can do the same thing via the Python interpretor, which is pretty cool.

The special.py script

This is used to pick a random winner.

Here it is:

import csv
import random

def main():
    # Get list of attendees and create a list of names
    # with first name followed by surname
    with open("attendees.csv", "r") as f:
        reader = csv.reader(f)

        # skip the first line in the csv file
        reader.next()

        name_list = []
        for row in reader:
            name_list.append("{} {}".format(row[0], row[1]))

    # Pick someone at random from the list
    winner = name_list[random.randint(1, len(name_list))]
    print("\nThe winner of FitBit thanks to GDG Dublin is \n\n\t\t{}\n".format(winner.upper()))


if __name__ == '__main__':
    main()

You need another file called attendees.csv:

First Name,Last Name
Naya,The First
Heather,Meowsalot
Jackie,Mewadith
Bob,Mewsli

Make sure they are in the same directory, and in your terminal/cmd, type python special.py.

Here's what happens:

  • Line 1 and 2: Imports two libraries that is bundled with Python but not part of its core libraries like print() or raw_input().

  • Line 49: Opens the "attendees.csv" file as a read-only file object

  • Line 50: Create the csv reader object with the file object we just created
  • Line 53: Skip the first line as it only contains "First Name,Last Name"
  • Line 55: Created an empty list and assigns it to name_list
  • Line 56 - 57: For each row of the csv reader object, we add each entry in the row into the list as a string. It's formatted as firstname followed by lastname, and will look like the following in a list: [Naya The First, Heather Meowsalot, Jackie Mewadith, Bob Mewsli]
  • Line 60: Using randint function of random module, we pick a random integer between 1 and the size of the list, and this is the position of name_list, and assign it to the variable winner.
  • Line 61: Print the name of the winner.

Congratulations to winner of fitbit

Congrats to Rachel for winning the fitbit from the random draw of registered workshop attendees, this was after a few goes. :-) Winner of fitbit thanks to GDG Dublin and Google Women Tech Maker

Best programming practices

We never got a chance to cover best programming practices, we did say one though:

  • Important of whitespaces and tabs. Always indent with 4 spaces, not tabs. Mixing the two is bad, and cause needless problems and cleaning up time.

More information on best programming practices for Python is called PEP-8. Do make sure your editors are set to this. Most programming editors support this.

Since it's easter... here's something to try...

  • Go to your terminal/cmd, and launch your Python interpreter.
  • Type import this.

This is what being Pythonic is all about. :-)

More info on Python.org.

Tweets from the evening

A couple of nice tweets from the evening in general for both workshops:

And it was super nice of them to put us up on the screens:

Newsletter out soon

I'll be sending out the monthly newsletter for April soon on what workshops are in the works, what's upcoming and also events of interest around Ireland. So subscribe via codinggrace.com. If you want me to include something, email me at vicky@codinggrace.com with its details.

Upcoming workshop

Dive into Javascipt @ Zendesk

Python-related events

Python Ireland April Meeting @ CHQ

  • Meets on the 2nd Wednesday of each month
  • When: Wednesday 8th April (18:30 - 21:30)
  • Details: http://python.ie / @pythonireland
  • Email: contact@python.ie

PyLadies Dublin @ Wayra

PyCon Ireland 2015

  • Conference: Sat 10 - Sun 11 October
    • x2 tracks of talks / x2 tracks of workshops (incl. beginners)
  • Sprints: Mon 12 - Tue 13 October
  • Details: TBA - http://python.ie / @pythonireland
  • Email: contact@python.ie

Diversity-related Event

weAREhere unconferece @ Guinness Enterprise Centre

Comments

comments powered by Disqus