Microsoft Virtual Academy

I was planning to complete a Microsoft Virtual Academy (MVA) course on Python. Specifically I was interested in the Developing Web Applications with Python and Flask. Upon review of the topics for the course, I thought it might not be what I really want. I want to write apps. Not so much web apps.

So I perused the MVA courses related to Python. I tried out the Intro to Python. After viewing the first module, I think it might be too rudimentary for me. The presenters were interesting. Their links were out of date. Don't know why. They hosted their stuff on Github. It should still be out there.

Learned that there are a lot of flavors of Python: CPython, JPython, IronPython, PyPy. Not exactly sure what version I have been learning. Maybe these are specific to Microsoft Windows or something. Of course the MVA recommends you use Visual Studio to do your development. So far I have gotten used to the Python Shell that comes with Python 3.4.

One week left until the local Python Hackathon. I think my skills need more practice. Even the MVA guys say you need to practice to learn. Is it time to implement the game of Othello? Or maybe go full blast a try to code up a mini-roguelike?

Python Battleship

I implemented a crude version of the game of Battleship in Python. This is all about x and y coordinates. How should one store such a pair of coordinates? I chose a tuple. A tuple in Python is a set of comma separated numbers. Normally they are programmed within parentheses. I just happened to represent the ship coordinates using tuples. Then I put those tuples in a list.

The good thing about tuples is that you access the elements of a tuple like you would a list, with brackets. A zero based index goes in the bracket. The values in the tuple are immutable. Once set, you cannot change them. This is fine as the ships in Battleship are not changing during the game.

Previously I had learned to use the int() function to convert a string to an integer. Well now I know there is also a str() function to convert an integer to a string. Useful information. Where to go next? I might implement a simple encryption method in Python. Or I might go full tilt and try to code the game of Othello. Still might also want to investigate a Python library. So much to learn. So little time.

Missing From Python

This week I have been concentrating on the features that are present in the Python programming language. However I have noticed some omissions in Python that other programming languages have. One is the case statement. There is no such thing in Python. You got to do a bunch of if else statements.

Another thing that does not seem to be supported out of the box is graphics. Sure you can pipe some characters to the console output. But there are no bitmapped graphics that I can see. Luckily there are popular add on packages that supplement this shortcoming.

I have read about PyGame which will give me graphics and sounds. And there are other choices too I presume. My last big project is going to be implementing the game of battleship. Unfortunately I did not play this game as a kid. I expect that is it something akin to Minesweeper. Let's see how hard that will be using Python.

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.