Python Pass By Reference

I implemented a bare bones version of Tic Tac Toe using the Python programming language. Since my last post, I added logic so that the computer made moves during its turn. The computer AI is not too intelligent. It just follows a preset path, and skips over spots that are taken on the board. The goal here is to learn Python, not figure out the Tic Tac Toe bot.

During this exercise, I needed to finally figure out whether parameters passed to a function are by value or reference. The answer is not so simple. Let's get one thing out of the way first. If you pass a string to a function, it cannot be changed as that type is immutable. But let's discuss passing mutable types to a function.

When you pass a variable, you are supplying a name that is associated to an object. That name is like a reference. The function gets its own reference that is separate from the formal parameter name. However the version that the function has can be used to reference and change the object it points to. So that is kind of like passing by reference.

You can reassign the local function reference to some other object. However the original variable (reference) that the caller has is unchanged by this reassignment. Confused? I guess you got to play with it a bit to understand what the heck is going on here. I will just treat this as pass by reference with some caveats.

Python Tic Tac Toe

I am in the middle of implementing a Tic Tac Toe game using the Python programming language. So far I can draw a board with ASCII graphics. I also get user moves and show them on the board. I can detect whether the latest move is a winning move. Next I need to implement the computer moves. That will require some rudimentary artificial intelligence.

Although I did not have to use it, I figured I should try two dimensional arrays to represent the board. In Python, it would actually be a two dimensional list. The creation of such a beast is not intuitive. I found some weird ways to specify it. However I decided to start simple and create a list with brackets. Then I appended lists to that main list using append(). Then I append the actual moves that sub-list.

The good news is accessing the two dimensional list is just as you would an array: myList[x][y]. I learned a couple other things today. Looks like you have to enclose expressions in parentheses for your if statements. And when there are compound expressions with parentheses themselves, wrap the whole thing in extra parentheses.

I did take advantage of a neat feature in Python. You can return more than one value from a function. Just do a return value1, value2. The caller can use the syntax global1, global2 = fxn(). You don't see that syntax in many (any?) other languages. Now back to tic tac toe implementation.

Python Hangman

I wrote a hangman style game using the Python programming language. This required me to use the skills I learned so far, and stretch a bit to learn more. One thing I picked up was that you can use the keyword elif to act like else if. Saves a few characters.

Also made use of a kind of for loop. You can use for i in range(n) to make i loop from 0 to n-1. Very handy to enumerate the indexes of a string. Speaking of a string, you can use a zero based index in brackets after the string variable name to access individual characters in the string.

The "in" keyword has other uses. You can check whether an item is in a list by using myItem in myList. That is pretty handy. Finally the values for a boolean variable are written True and False. Yes. You must use capital letters to start those keywords.

Now that I got hangman out of the way, it is time to reach for the stars. Next I think I shall implement tic tac toe. That will require a bit of artificial intelligence. But nothing too deep. I might even use the algorithm my book taught me so I can concentrate on the Python programming aspect.

Python Functions

It is pretty easy to write functions in Python. You use the keyword "def". Then you put the name of your function followed by parentheses. Optional argument names go inside the parentheses. After that you place a colon.

The rest of the indented code after the colon is the body of your function. The function can optionally return a value with the return keyword. Now there is variable scope business that I have read about and have not played with. More on that later I presume.

Calls to the function can be made after it has been defined. You just reference the function name followed by parentheses. You put any actual values for the parameters within the parentheses. And you can assign the return value of a function, if any, to the left hand side of the function call.

The colon seems to be standard fare in Python. You put it after the function declaration. You also put if after conditional checks and looping constructs. I am going to be kicking it into high gear next, writing a non-trivial game of hangman in Python.

Python Lists

Time to write my second program in Python. This one will need to hold a bit of data. So I decide to use an array. Ooops. Python does not seem to have an array. But they do have lists. All right. Set a variable name equal to a comma separate list in brackets. Then I have a list.

The list is accessed with a zero-based index in brackets. You can create an empty list with just brackets. Then you can add items using the append() function. The list items do not need to be the same type. But you cannot access an entry in the list that was not created yet. That would cause an error.

I also need some random number generation. I import the random package for help with that. Then I can use the randrange() function which takes the upper and lower bounds of the range for the random integer that is returned.

Next up I am going to learn how to create a function in Python.

Invent With Python

I have been interest in learning the Python programming language for a while. My college had offered a class to teach you Python. But I have not seen the class offered in the last two years or so. Finally I have figured it is time to take matters into my own hands and learn the language by myself.

To help with this, I got a copy of the book Invent Your Own Computer Games with Python. I call it Invent with Python for short. I read through the book over the last month or two. I took a lot of notes. But I did not work through any exercises. Bad move. When I sat down to write my first program, I was clueless.

Now it is time to really learn this language. I did complete my first program with the help of Google. My plan is to write the games that were covered in the book. But I will write them on my own without looking at the source code in the book.

My company is sponsoring a Python hackathon in less than two weeks. Time to really get up to speed so I can complete. All right. I can use the input() function to grab data from the user. That returns a string. Need to convert it to a numeric value using int() before comparing to other integers. I can also use the print() function to do my output.

This is going to be a long ride.

WordPress Carousel

I am taking a college class on WordPress. Have not been really doing any coding in the class. So I am not having a whole lot of fun. I planned out my semester project web site. The initial page was going to have an image slider on it. I figured I would use a Carousel plugin for WordPress. Easier said than done.

I don't know how many plugins I tried. It was at least five. Maybe closer to ten. Lots of them were duds. Yes they were free. But I figured that a carousel was a common item these days. Many times the plugin would not display a single image. Others would display an image or two, but they would not move.

Finally I found Tiny Carousel Horizontal Slider Plus, also known as tchsp. I will admit that I initially had some trouble getting my images into this carousel. Once I did though, the magic happened. Thank you Gopi.

I uploaded a bunch of images to my gallery. However I had trouble getting them to work with tchsp, which requires that you enter the image URL. Turns out if you edit the image in your gallery, there is a File URL in gray over on the right hand side of the screen.

I sure hope the rest of my site will not be as much pain as the carousel was.