Design

Monday, October 25, 2010

Guess the Number Game Python

With inspiration from InventWithPython.com, I wrote a game to guess a number between 1 and 20 with six attempts. This simple game is good practice for a beginner. My code is written in Python 2.6. The example from the link is written in Python 3 syntax.

import random

guessesTaken = 0

print 'Hello! What is your name?'
myName = raw_input() #get user name

number = random.randint(1,20) #random number between 1 and 20
print '%s I am thinking of a number between 1 and 20.' % myName

while guessesTaken < 6:
print 'Take a guess.'
guess = int(raw_input())
guessesTaken += 1
if guess < number:
print 'Your guess is too low.'
elif guess > number:
print 'Your guess is too high.'
else:
break #breaks out of loop

if guess == number:
if guessesTaken == 1:
print 'Good Job, %s! You guessed my number in 1 guess!'
else:
print 'Good Job, %s! You guessed my number in %d guesses!' % \
(myName,guessesTaken) #actual format on previous line without '\'


if guess != number:
print 'No. The number I was thinking of was %d' % number

Sunday, October 24, 2010

Python Debugger Article by Sontek

My husband wrote a wonderful blog post about debugging Python. Read it here.

String Formatting in Python

An easy way to combine strings with non strings is to use commas:
>>>>>> print 'The value of pi is often rounded to',3.14
The value of pi is often rounded to 3.14

But what if you want to create a website that welcomes guests by their name after he or she logs in? What if you are pulling information from a list or a dictionary? For personalization or multiple use functionality, we will be using string formatting. Below are the different format characters. There are a few others, but they aren't necessary.
%s   string
%d integers or decimals, but returns floor integer
%r anything that is not a string, converts object to a string
%f floating point, so we can control precision after decimal

Here is an example of using each of the formats:
>>> name='Anderson Silva'
>>> weight= 105 #integer
>>> height= 1.92 #decimal, but I only want 1.9 displayed
>>> home= 'Brazil' #string, watch what happens with %r
>>> fighter1='''%s
... Nationality: %r
... Weight: %d kg
... Height %.1f m''' % (name,home,weight,height) #in order
>>> print fighter1
Anderson Silva
Nationality: 'Brazil' #%r leaves quotes around strings
Weight: 105 kg
Height 1.9 m

Here is an example converting centimetres to inches
>>> def convert(cm):
... inches=cm*.39370
... print 'There are %.2f inches in %d centimetres' % (inches,cm)
...
>>> convert(10)
There are 3.94 inches in 10 centimetres
>>>#two places after decimal are shown
>>> convert(3.544445)
There are 1.40 inches in 3 centimetres

%f has a default to print six numbers after the decimal. By adding '.1', '.2', '.3', etc... between the '%' and 'f', you determine how many numbers are visible after the decimal. You can also add '+' or '-' before the '.x', such as showing a change
>>> print "Today's stock price changed %+.2f" % 1.4888
Today's stock price changed +1.49

Friday, October 22, 2010

Decimal Library for Changing Number Precision in Python

The built in math functions in Python use binary approximations, giving some funky results when dealing with numbers containing decimals:
>>> .1+.2
0.30000000000000004
>>> round(100.00/3.000,4)
33.333300000000001

One way to appropriately find the sum of decimals is to use strings
>>> str(.1+.2)
'0.3'

Also, the default is to round to the nearest whole number when dividing
>>> 1/3
0
>>> 100/3
33

The decimal library is a useful tool for floating point arithmetic. Instead of the command 'from decimal import *' that would import everything from decimal, all I need to import is Decimal and getcontext. When importing modules, simplicity is preferred. There are less problems with naming in your code and you can be more aware of the tools at your disposal. I already imported decimal and looked through the directory to determine which modules I wanted. I'm only going to show the precision feature of decimal. You may want to import the entire library if you want to use other functions.
>>> from decimal import getcontext
>>> from decimal import Decimal
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_UP, Emin=-999999999,
Emax=999999999, capitals=1, flags=[Inexact, Rounded],
traps=[DivisionByZero, Overflow, InvalidOperation])
>>> #our precision is also known as significant figures,
applied after arithmetic
... #let's change our precision
...
>>> getcontext().prec=6
>>> Decimal('1')/Decimal('7') #can be performed to strings
Decimal('0.142857')
>>> Decimal(1)/Decimal(7) #can be performed to integers
Decimal('0.142857')
>>> Decimal(10)/Decimal(7)
Decimal('1.42857') #notice that 6 is the total number of
figures, not the number after the decimal
>>> Decimal(10)/Decimal(5)
Decimal('2') #not '2.00000,' which is considered more
accurate than 2 by the science community

As someone with a science background, I found the decimal library's use of 'significant figures' interesting. Decimal can also be used in financial reporting or billing. You can also find maximums and minimums, change rounding properties, and do anything that you can do with the math library. I personally prefer the math library for the algebraic functions performed by decimal, because math's syntax is simpler. To learn more about decimal, click here .

Wednesday, October 20, 2010

Using Map Function in Python

Today I discovered the map function in Python. Map causes some simple for loops to be verbose and unnecessary. Let's look at how to change a list of integers to a list of strings. First we'll use a for loop:
>>> list=[1,2,3,4,5]
>>> index=0
>>> for x in list:
... list[index]=str(list[index])
... index+=1
...
>>> print list
['1', '2', '3', '4', '5']

Now we'll use the map function and we'll define our 1-5 list using the range function
>>> list1=range(1,6)
>>> map(str,list1) #performs str function to every index of list1
['1', '2', '3', '4', '5']

You can also make your list within the map function, such as splitting a string into a list. The following example shows how you can define a method using else, elif (else if), and if statements, then run your method to a single string with the map function, resulting in a list with the method performed to each index.
>>> def pluralize(word):
... if word[-1]=='y':
... return word[:len(word)-1]+'ies' #replaces the y with ies
... elif word[-1]=='s':
... return word+'es'
... else:
... return word+'s'
...
>>> map(pluralize, "The sexy waitress brought me a beer".split())
['Thes', 'sexies', 'waitresses', 'broughts', 'mes', 'as', 'beers']
>>>#my husband chose the sentence

I simplified my method to not account for every scenario put into it. Although this specific example does not produce correct English, the map function correctly split the string on the whitespace and carried out the method on each index of the list.

Monday, October 18, 2010

Indexes, Strings and Lists in Python

A Python string's properties can be best understood by using sentences and words, although numbers can also be stored as strings. Be careful not to name a string as 'str' or 'string', because these are built in functions in Python. Let's play with some basic functions you can do to strings
[gypsychemist@inspidell ~]$ python
Python 2.6.4 (r264:75706, Jun 4 2010, 18:20:16)
[GCC 4.4.4 20100503 (Red Hat 4.4.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> sentence1='This is two strings '
>>> sentence2='combined.'
>>> sentence1+sentence2
'This is two strings combined.'
>>> s=sentence1+sentence2
>>> print s
This is two strings combined.
>>> r='''Three quotes will
... preserve the formatting
... for multiple lines'''
>>> print r
Three quotes will
preserve the formatting
for multiple lines
>>> s.split()
['This', 'is', 'two', 'strings', 'combined.']
>>> r.split('e')
['Thr', '', ' quot', 's will\npr', 's', 'rv', ' th',
' formatting\nfor multipl', ' lin', 's']

The split command divides a string into a list based on the delimiter you define. In the example of splitting the s string, leaving the delimiter blank causes splitting on all white spaces (tab, enter, space). Anything can be the delimiter, such as using 'e' in the splitting of r example. '\n' is a new line.
>>> p="  The extra white space on the ends     "
>>> p.strip()+' is removed by the strip function'
'The extra white space on the ends is removed by the
strip function'


Indexes are useful for both strings and lists. A common graphic to explain indexes is:

The letters or numbers in a string begin with the index 0, or you can work from the back with index of -1. When selecting a range of indexes, the last number is excluded.
>>> f='hello'
>>> f[0:2]
'he'
>>> f[:2] #a starting index of 0 is assumed
'he'
>>> f[2]
'l'
>>> f[-2:] #completing until the end is assumed
'lo'
>>> f[-2:-1]
'l'
>>> f[:2]+f[-2:]
'helo'
>>> list=[0,1,2,3,4,5]
>>> list[1]
1
>>> list[-2:0] #doesn't work
[]
>>> list[-2:]
[4, 5]
>>> list[:1]
[0]
>>>


'#' is used in Python to write comments. They are ignored in programs and are very useful to solve problems, either to write out a plan or to type in a literal translation of how you expect your code to perform. If your code does not work correctly, you can go back through your notes to quickly find where your logic or syntax may be incorrect.

As I mentioned briefly in the beginning, a number can also be a string. If a number is a string, it will be used a returned with quotation marks surrounding it. In Python, single quotes and double quotes act the same; however, the opening and closing ends need to be the same. The example below shows why you may want to change a number to a string:
>>> pi=3.14
>>> # cannot add an integer to a string
>>> print 'The value of pi is often rounded to ' + pi
Traceback (most recent call last):
File "", line 1, in
TypeError: cannot concatenate 'str' and 'float' objects
>>> #so we change pi to a string
>>>print 'The value of pi is often rounded to ' + str(pi)
The value of pi is often rounded to 3.14
>>> #you can include a number after a string by using a comma
>>>print 'The value of pi is often rounded to',3.14
The value of pi is often rounded to 3.14


You can change a list of numbers to be a list of strings by using a for loop.

>>> list=[1,2,3,4,5]
>>> str(list[1]) #returns the list index as a string without
changing the list
'2'
>>> list #list is still composed of integers
[1, 2, 3, 4, 5]
>>> index = 0 #we're setting a variable to start at 0 because
index starts at 0
>>> for x in list: #begins with index 0
... list[index] = str(list[index]) #assigns index 0 integer as
a string of the previous list[0]
... index += 1 #after the previous command is performed on [0],
it will be performed on [1]
... #for loop continues through indexes 1 to 4 after index 0
>>> list #our list is now changed to be strings
['1', '2', '3', '4', '5']

Now lets play with some list commands that are nondiscriminate against strings or integers:
>>> list1=['8','9','10']
>>> list.extend(list1) #add list1 to end of list, list+list1
>>> print list
['3', '4', '5', '6', '7', '8', '9', '10']
>>> list.insert(2,'3') #inserts '3' at index 2
>>> print list
['3', '4', '3', '5', '6', '7', '8', '9', '10']
>>> list.insert(2,3) #now we'll insert an integer
>>> print list #list can have combination of strings & non strings
['3', '4', 3, '3', '5', '6', '7', '8', '9', '10']

For more documentation and to try writing example programs, visit Google's Python classroom: http://code.google.com/edu/languages/google-python-class/index.html

Math with Python

If this is your first time using Python in Windows, you will have to download it from python.org. Every operating system includes a terminal, which we will be using to practice and test. Google "How to open terminal" and your operating system name to find specific information about opening your terminal. You will also need either VIM, WING, Eclipse, or another editor to save and run your programs, though we do not need to use it for this tutorial. This article will focus on math capabilities within Python, either performing calculations as a common calculator or within logic commands. So open your terminal and play with me.

I start my code with telling my terminal that I would be working in python. [gypsychemist@inspidell ~]$ is my shell prompt, "python" is what I typed, and the next three lines are showing that python is installed and open for me to use.

[gypsychemist@inspidell ~]$ python
Python 2.6.4 (r264:75706, Jun 4 2010, 18:20:16)
[GCC 4.4.4 20100503 (Red Hat 4.4.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> 3+2
5
>>> 3*2
6
>>> 6/2
3
>>> 3-2
1
>>> 3*(3-1)
6
>>> pi=3.14
>>> 3*pi
9.4199999999999999
>>> abs(2-3)
1
>>> pow(3,2)
9
>>> 3**2
9
>>>

Basic math commands are shown above. '+' is for addition, '-' for subtraction. '*' for multiplication, and '/' for division. The basic math rules regarding parenthesis and order of operations are followed correctly. You can also store numbers as words or letters to perform math equations on, such as the example with pi. In Python, '=' is used to assign and does not imply equivalency. The absolute value function and two alternative ways to find a number with an exponent (also known as power) are shown.

A useful tool in python is the math library. To see the functions you can use within math, type 'import math' and 'dir(math)' on the next line. You just told the terminal you are opening the library and viewing the directory. The terminal will show you the commands including different trigonometric functions, advanced algebra, and rounding options. To learn more about a command (such as floor rounding), you can type 'help(math.floor)'. Press 'q' to exit help.

>>> import math
>>> dir(math)
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh',
'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign','cos',
'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod',
'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10',
'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt',
'tan', 'tanh', 'trunc']
>>> help(math.floor)
>>> math.floor(4.5)
4.0

You can also test to determine if one number is divisible by another, such as when you want to execute commands on only the even indexes in a list. To determine if 3 is evenly divisible by 2, type '3 %2'. Python returns '1' because there is a remainder of 1 when you divide 3 by 2. If 0 is a result, then numbers are evenly divisible.

>>> 3%2
1
>>> 3%3
0

If given a list of numbers and you want to perform math functions such as adding the numbers together, you can use a for loop. Follow the below example:

>>> list=[2,3,6,7,4,6]
>>> sum=0
>>> for x in list:
... sum+=x
...
>>> sum
28
>>>

In Python, spacing of indentions and capitalization are very important. After any statement ending with ':' , indent by a consistent amount (two spaces, four spaces, tab, your choice).'sum+=x' is the same as 'sum=sum+x'. 'x' is the value at the index as it goes through the list. So this code just said 0+2+3+6+7+4+6=28. Now that you understand how to do it with a basic for loop, the quicker way is to use the built in sum function. Because I used the variable 'sum', already, I need to first reinstate sum to its built in function.

>>> from __builtin__ import sum
>>> sum(list)
28

Using built in functions as variables should be avoided. As you learn more Python, you will learn safe and understandable variables to use in your code. For more information on numbers and math, you can visit http://docs.python.org/library/numeric.html

CS Degree and Other Qualifications

Don't believe the infomercials or college recruiters. You do not need a college degree to have a career in computer programming. You may want a degree to get a management position overseeing programmers, but it is possible for skilled programmers with less than a year of college to be offered six figure management positions after five years of employment in the industry. There are employers who are more sceptical of hiring college graduates than hobbyists. Such employers prefer people who have worked on open source projects. Open source projects are contribution efforts from many programmers, so the code is available to the masses and programmers do not make money from their efforts. Most college courses in computer science focus on old technologies and obsolete techniques. Hobbyists take advantage of other opportunities to learn and usually enjoy expanding their knowledge and skills through practical experience. There are plenty of online books and 'classrooms' to learn from for free. As my husband has told me, "Programmers control the internet." When you google a term that is in programming, your first couple hits will be programming tutorials, blogs, and discussions. As you feel more comfortable with basic programming, you can expand your knowledge from reading blogs, buying books, participating in user groups, and attending conferences.

As with any job or profession, you need to consider your personality, skills, interests, lifestyle, and values to determine if you will be happy and have the capability to excel. In terms of personality, most computer programmers are introverts. 'Introvert' does not mean a social outcast; an introvert enjoys time to themselves or less than three others, does not need a lot of noise and external stimuli to stay entertained, and usually analyzes what they plan to do and say before carrying out their actions. Computer programmers are very analytical. Syntax and logic are the basic necessities for every computing language. An interest in computers and technology is beneficial, but a lot of knowledge about hardware and operating systems is not necessary to begin programming. The industry you choose to work within as a programmer will be affected by your lifestyle, interests, values, and often preferred programming languages. My dad has chosen to work in a management role within the healthcare industry. My husband still enjoys coding, so he works on registration software for conferences. Nearly every industry requires software or websites, so there are many opportunities for computer programmers. You can choose to develop your own projects into a business, work in an office with colleages, or communicate with the office through conference calls from your own bed each morning.

My computer programming experience is very young. I will be using this blog to motivate myself to continue learning. The languages I want to learn are Python and Objective C. My reasons for wanting to learn these languages are so that I can pick up contract work as extra income and so that I can work on personal projects and companies with my husband.