Sunday, January 26, 2014

The beginning of all things

Objects

There are some very basic "data types" that python can manipulate. In this post I will discuss different types of numbers. 

Python is an object oriented programming language, meaning that all data (information) in a python program is wrapped in a "container" called an object. Every object consists of three parts, an identity, a type, and a value. So far, you've had experience with identity and value. When you assigned a number to a variable name, the name was the identity and the number was the value. Now we'll discuss the third property type.

The numbers we used in the birth year calculator were whole numbers or "integers". An integer is a "type" of object. If we wanted to use a number with a decimal, we would have to use a "floating point" number. A floating point number or "float" is a different data type. Both represent numbers even though they are different types of objects. 

There is a method for testing an object's type. We can test this by opening our command prompt. (Windows key + r, then type cmd and press "Ok") Type "python" to enter your interpreter.

Create two variables, a and b. Assign a a value of 5, and b a value of 5.0.



Now divide a by 2. Then divide b by 2. Notice that when a is divided by 2 the answer returned is 2, but when b is divided by 2 the answer returned is 2.5.


In order to test the type of an object you can enter type(object name), where object name is the name of the variable you are testing. Do this now with the a and b variables.


Notice that a is a type int object (short for integer), and b is a float type object (short for floating point). Because they are objects of different types they behave differently. Integer objects are only concerned with whole number values, so when two integer values are divided, any non-whole value is discarded. Even though 5 divided by 2 equals 2.5, the decimal is dropped and the answer returned is 2. On the other hand, when the floating point variable, b, is divided by 2 the decimal point is retained even though 2 is an integer.

When performing a calculation on two numbers, the more complex number will determine the 'type' of the result. 

Integer and Integer = Integer

Integer and Float = Float

Float and Float = Float

Object Methods and Properties

Objects often have different properties and methods associated with them. Properties are kind of like additional variables that exist within an object. If you had an object that represented a person, you might have properties like height, weight, and gender. In the case of our two object (int and float), each has different properties. For example, because a is an int object it has a numerator and a denominator property. To access the properties you can simply type a.<property>.

 

However, b is a float object so it doesn't have a numerator or denominator property. Instead it has other properties, real and imag which our int object doesn't have. You can access these properties the same way.

 

In addition to properties, objects can also have "methods". Methods are a type of function that can be used to manipulate or interpret data within an object. Our float object has a "method" called "as_integer_ratio". In order to "call" the method on b type b.as_integer_ratio(). The '()' are how methods and functions are used in python. If you type b.as_integer_ratio without the () and instead of running the method, python will simply tell you that what you are accessing is a method of float object.


Notice that the values returned from the as_integer_ratio method are 5 and 1. This is because in order to represent the value of b as an integer fraction you would use 5/1. If we set b = to 0.5 and run the method again the return values will be 1 and 2 because 0.5 is equal to 1/2.


In order to see all of the properties and methods available for a given object you can type dir(<object name>). Doing this is a good way to get acquainted with objects that you aren't familiar with. For now, just ignore anything with a double underscore. We will discuss these properties and methods later on. For a more detailed description of the properties and methods of an object type help(<object name>).



Objects are a complex subject. I won't go into the intricacies of them all at once. I will try to describe information as it becomes necessary. In this post we've discussed the main types of number objects, next time we will discuss "strings", getting input from a user, and write our first program.

Wednesday, January 22, 2014

Why learn programming?

There are many reasons to learn programming.

  • Beginning programming can be learned easily and can be done for free and only takes a few weeks!
  • Programming doesn't require a huge problem to be worthwhile. Like drawing, or juggling, or cartwheeling, can be done for it's own sake and for fun.
  • Learning programming affects the way your mind works. It allows you to systematically approach and solve problems; a skill which can improve the skills you already have.
  • Programming can be useful to anyone who needs to organize information, or do small tasks repeatedly. It can help with keeping track of how much you spend on dog food, when your friend's birthdays are coming up, what your current grade is in class, or which of your portfolio investments are under performing.
  • Programming is a means of empowering yourself and improving your employability. It can help to separate you from the pack.


Why learn Python?


Python is a simple yet powerful programming language with many applications. It is simple enough for young children to learn but powerful enough to be applied in some of the most high-tech cutting edge research going on today. Python can be applied to many different fields including:

  • Digital Art (particularly in the film industry)
  • Scientific Computing
  • Computer Vision (for robotics and image analysis)
  • Web Development
  • Business Applications
  • Education
Additionally, Python can act as a spring board into more complicated and powerful programming languages such as Java, C++, PHP, and Ruby. Once you learn the basic principles of programming in Python, it is much easier to learn the intricacies of more advanced programming languages.

Monday, January 20, 2014

Introduction - Variables and Interpreters

Now that you have Python set up you can open the interpreter. 

First, it is important that you know how to open the command prompt window. Press the "Windows key + R", type in "cmd" and press "Ok". This will open the "command prompt" or "console" window. This is where most of your early programs will run from so you may want to create a shortcut to it, or get familiar with opening it. 

Once in the console you can open the interpreter by simply typing "python" into the console and press enter. If you installed everything correctly you should get a couple of lines of information followed by ">>>". The ">>>" is an indicator that you are now running the Python interpreter.



The interpreter allows you to write code in real time. That is you can create and manipulate variables without ever having to save your program. To demonstrate, let's create some variables and manipulate them using the interpreter.

Start by typing:
5+7
then press enter.


Notice that the interpreter prints 12 on the next line, which is the sum of 5 + 7. The interpreter is reading your "code" and calculating it.

Now let's try dealing with some variables, then I'll explain what variables are and why they are important.

Type:
a = 35
and press enter.

Now type:
a
and press enter.

Notice that when you input a the interpreter interpreted it to mean 35. What you have done by typing "a = 35" is told the computer that a is a symbol (known as a variable) that will represent the number 35. The obvious question at this point is "Why not just type 35?" The answer is that by having a variable, a doesn't have to represent 35, it could also represent 5 or 4,000. By using a variable we can write a program that takes any value and manipulates it. Let's write a simple program that can act as an example.

Let's create a variable called year and assign it to represent 2014.

year = 2014

Next let's create a variable called age and assign it to be your age.

age = 300

Now, let's find out the year that you were born:

year - age


Now I can see that I was born in the year 1714. Your results are probably different than mine, but that is exactly the point. Because we used a variable instead of a "literal" number, the program will work for anyone, not just people who are 300 years old. Without variables I would have to write a program for people who are 10 and another one for people who are 11 and 12 and so on... Then all of those programs would only work during 2014, then I'd have to write a new set of programs for 2015. So, we can be thankful that variables exist and we only need to write one program.

Of course, this program is not very useful since it doesn't take into account actual birth date which will cause it to be off by one year depending on whether the user has had a birthday this year or not, but it demonstrates the power of variables.

There is a lot more to variables than I have mentioned here, which we will eventually get into, but for now I wanted to make sure that you understand what a variable is and why they are essential to writing programs.

Play around with the concept of variables to familiarize yourself with their use. Try assigning a number to a variable and then assigning a different number to the same variable. See if you can create code that "breaks" or doesn't work properly.

Next time I will go deeper into what variables are and how they work and describe different types of variables and what they can each be used for.


Python - Setting up Python on your computer






I'm going to begin with an introduction to Python because it is much more accessible to those with no programming experience. It will allow you to learn how programming works without having to worry about the complexities of memory management and compiling. All of the set up instructions will be based on a Windows operating system because that is what I use. If you have a Linux operating system, you probably already have a pretty good idea of how to set up a programming environment. Python comes pre-installed on Mac OS X or later, but the version is usually outdated. It is recommended that Mac users download on of the current installers from the link below.

In order to begin programming, you'll need to download the programming language. You can find the download for Python here. I currently use Python 2.7.6 but there are other versions. The two "current" versions are 2.7.6 and 3.3.3. Simply download and install your version of choice.

Once installed, you'll want to make sure that the proper environment variables have been created and create them if necessary. To check your environment variables, go to your control panel and search for "system".


Next, if the "Edit the system environment variables" is an option, choose it. Otherwise click on "System", choose "Advanced system settings" and in the "Advanced" tab select "Environment Variables".


For now there are two variables to be aware of: Path and PYTHONPATH. First, under system variables select your "Path" variable and select edit.

Check that the "variable value" field of your path variable contains the folder where you installed python. It will probably be something like "C:\Python27" or "C:\Python33". If the folder is not there, add it at the beginning. Be sure to put a semicolon at the end of the folder name to separate it from the other entries. When you are finished adding the entry, press "OK".

 Note - Your install directory might be different from the above example, be sure to use the path from your system.

Next, check for a system variable called PYTHONPATH. If it is not present create it by pressing the "New..." button under system variables. Be sure to type PYTHONPATH in caps.

Make sure that the same install folder as above is in the "Variable value" field for the PYTHONPATH variable as well.

That should be enough for the setup. You are now ready to begin using Python to write code. Some systems might require a restart at this point.