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().
- csv module is imported so we can access functions to read and manipulate data from a csv file. See reference in python.org documentation
- random module is imported so I can pick a random winner from the list. See references in python.org documention
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 ofname_list
, and assign it to the variablewinner
.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. :-)
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. :-)
Tweets from the evening
A couple of nice tweets from the evening in general for both workshops:
Fab evening learning AngularJS @GDGDublin with @CodingGrace. Great tutors. Totally encouraging. My head hurts ... in a good way!
— Sarah Barrow (@mssarahbarrow) March 31, 2015
And it was super nice of them to put us up on the screens:
Made the wall of names @sarahd0ran @whykay #WomenTechmakers pic.twitter.com/N6maCwFv9v
— Ireland Girl Geeks (@irelandggd) March 31, 2015
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
- When: Saturday, 18th April (11:00 - 18:00)
- Details: http://www.codinggrace.com/events/dive-javascript/44/
- Email: contact@codinggrace.com
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
- Meets on the 3rd Tuesday of each month
- When: Tuesday 21st April (19:30 - 21:00)
- Details: http://dublin.pyladies.com / @pyladiesdub
- Email: dublin@pyladies.com
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
- When: Saturday 25th April (10:00 - 16:00)
- Details: I've posted a blog post, so check it out for more info and tickets.
- URL: https://www.facebook.com/wearehere.ireland