Python Lists

I am in the process of learning the Python language and have been thinking of ways to help reinforce what I have learned. One thing that helps me in the learning process is to write about the subject I am learning. So I thought I would start a Python category on my personal blog and start documenting the journey I have been on. In my first post I focused on my interest in programming and using the main() function in Python programs (a tip my youngest enlightened me on). In my second post I cover how to open text files, read them and then close them. This post will be a multi-parter on lists.

From the very first program I worked on (a notes processing application) it became very apparent that I was going to be using a lot of lists. A list in Python is a group of things (strings, integers, etc...). Lists are quite malleable. You can have duplicate items in them and you can move, add or remove items on a whim. Lists are ordered so items can be identified or manipulated by their index number. The items of a list are encapsulated within brackets ([]).

fruit = ['apple','banana','orange']

In the example above I created a list of fruit. Fruit is the variable I have assigned the list using the equals sign. Inside the brackets are my list items: apple, banana and orange. The single quotes around each item in the list indicates that the items are strings, but the list could consist of integers as well. If you want to see the contents of a list you can use the print() method:

print(fruit)
['apple','banana','orange']

Once you have a list defined you can do interesting things like loop through them and compare items in the list to other things like strings. In future posts I will cover ways to update lists, retrieve values, loop through them, and run comparisons on list items with strings.

Opening and reading text files in Python

So far a lot of projects that I have worked on in Python have utilized external files for some kind of processing. Early on I had to become familiar with the open() function and some supporting functions for working with external files. Initially I worked with plain text files, but have moved on to more complex file types and have learned how to use a few new libraries to help process those. For this post I will stick with plain text.

The open() function does just what it describes and opens the specified file. This function takes two parameters: filename and mode.

open('file.txt','r')

The filename parameter is a text string for the file one wants to open and will need to include the full path if necessary. The mode parameter has the following options:

  • Read "r" - The read parameter opens the file for reading. This is the default value if this parameter is left blank. If the file does not exist an error message will be returned.

  • Append "a" - The append parameter opens the file for appending additional data. If the file does not exist the function will create a new file.

  • Write "w" - The write parameter opens the file for writing data. This parameter will overwrite and existing file or create a new file.

  • Create "x" - The create parameter creates the specified file. An error will be returned if it already exists. Additionally, one can specify if the file should be handled as binary "b" or text "t" (text is the default mode).

Once the text file has been opened it is time to read the contents for processing. I have used both the read() and readlines() functions in the projects I have worked on. The read() function will read the entire document as one block of text. The readlines() function will read each line of text as a list. Once the text is in a readable format for Python one can start manipulating or processing it.

file = open('file.txt','r')

readTheFile = file.read()
print(readTheFile)
This is some text.
Here is some additional text.

readlinesTheFile = file.readlines()
print(realinesTheFile)
['This is some text.\n','Here is some additional text.']

Of course, once you are finished with the file it is good practice to close it and that is where the close() function comes into play.

file.close()

You can get a bit fancier and have Python close the file automatically once one has finished processing it, but I will save that for another post.

So there you have it, the basics of opening, reading and closing files in Python. I am sure there are many ways people have discovered with file management in Python so if you have a tip please leave it in the comments!

My Python Journey and the Main Function

I have been interested in computers and how they function since my Dad purchased our first family computer back in the mid-eighties. It was an IBM clone made by Commodore that had an 8088 chip, no hard drive and two 5 1/4 floppies. I used to spend a lot of time playing games and just tinkering with the applications we had for it. I did a little Basic programming but never really got into it. Over the years I became increasingly interested in learning a programming language and tried to do so on many occasions, but nothing seemed to stick. A few months in to the pandemic I decided to give it another go and picked Python to start tinkering with. I started with "Learn Python 3 The Hard Way" by Zed Shaw. This was a great book for me as it got me in coding right away so I could start building things and learning from what I built. "Automate the Boring Stuff with Python" by Al Sweigart and "Python for Everybody: Exploring Data in Python 3" by Dr. Charles Severance quickly became staples as well. Once I learned Python syntax a bit and created a few things from the examples in the book I was ready to start on some of my own projects. I am by no means a Python expert and still consider myself a noob at this, but I wanted to start documenting my journey and some things I have learned that have helped me the most. Writing this stuff down helps reinforce my learning and if it can help anyone else out there, then all the better. So on to my first tip!

One of the best Python tips I was ever given was from my youngest and that was to create a main function that is used to refer to the other functions in your app. This tip is great because it really helped me organize the flow of my programs and makes it easier for others to review and provide feedback. The main function is structured the way any other function, but as you build out your application you can reference your other functions here making it easy to follow the flow of the application, which comes in handy when you need to troubleshoot an issue.

def main():

otherStuff = otherFunctions()

Another component that can be added is a conditional that will allow the program to execute automatically and make it easy for people to use your application as an additional library.

If __name__ == "__main__":

main()

Essentially, this conditional will execute the main function if there is one to be called. Learning this structure has helped me immensely in making my code more readable and easier to troubleshoot. This is a tip that I learned only very recently and I wish I had learned it at the very beginning of my journey. This is why I have added it as my first tip.

Playstation 4

On February 20th, Sony announced the new Playstation 4.  The specs are much more advanced than the current Playstation 3, but nothing earth shattering.  It looks like their focus will be on some interesting social sharing features of the system.​

Here is a video showing the video capabilities of the system:

​This video discusses the development process and some of the social sharing aspects of the system:

​I am very interested in the social sharing aspect and can't wait to see the great content that gamers share with this system.

Badges and the Help Desk Environment

One of my responsibilities as eLearning Director at Tacoma Community College is to manage our student computer lab, the Information Commons.  In this lab we have a fully staffed help desk for studnets and faculty.  I have been trying to figure out a way to showcase a help desk staff member's expertise with applications to make it easier for students and faculty to find the help they need.  We have a board in the Information Commons with everyone's picture and I thought maybe we could post little icons on the board, but realized we would soon run out of space.  Then I started to hear about an open badge project with Mozilla and became very intrigued.  I recently joined the Open Badges and Assessment group at P2PU and have been learning quite a bit about the Mozilla project.

A way I envision using this open badge system in the Information Commons is to develop assessments around competencies for the different applications/devices we support.  When one of our staff complete an assessment they are awarded a badge that can be added to an Information Commons Help Desk Staff profile page.  This profile page would make it easy for students and faculty to find someone that can help them with their particular issue.  This would help expedite the resolution of a support issue which would make the student/faculty member happy.  In addition, our support staff would have a way to document their skill level with various applications/devices which could be added to a resume or portfolio and help me determine areas of support where we need to build capacity.

A Learner's Story

Andy is working on a project for his chemistry class and needs to plot some data points in Excel, but he hasn't done anything like that before.  He approaches the help desk to find someone that can help him with Excel.  One of the help desk staff, Amy, follows Andy to his work station to give him a hand.  Andy explains the issue he is having and Amy realizes that this issue is outside her area of expertise with Excel.  Amy goes back to the help desk and asks her colleagues if anyone has the skill necessary to help Andy.  She finds Bob who knows how to do this and brings him over to Andy.  Bob is able to show Andy how to plot data in Excel and Andy is able to finish his project.

How Badges Can Help

With the implementation of badges in the help desk environment staff members can showcase their competencies with various applications.  This will make it much easier for end users to determine who will be their best line of support with a given issue saving valuable time.

A Learner's Story with Badges

Andy is working on a project for his chemistry class and needs to plot some data points in Excel, but he hasn't done anything like that before.  Andy opens up the help desk staff page on his computer in the lab to see who might be avialable to assist him with his project.  He sorts the list by Excel and further by charts/graphs.  He sees that Bob has sufficient experience and will be coming on duty in a few minutes.  He sends Bob a quick email message letting him know what computer he is on.  When Bob comes to work he checks his messages and notices a message from Andy stating he needs help with Excel and heads to the listed work station to provide support.