Friday, May 18, 2012
Since you've been waiting so patiently, I have something fun to teach you, List Comprehensions. Python List Comprehensions are a great way to generate a list quickly and easily. Their syntax looks a little weird at first, but once you get a grasp on it, you'll find that they're an excellent tool in any Python Programmer's arsenal.
Here's an example:
>>> x = [num for num in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
that's pretty cool, but how did we do it? Here's the same code stretched out into 3 lines.
x = 
for num in range(10):
you can already see the similarities.
Here's the basic structure of a Python List Comprehension:
[i for i in range(10)]
The first value is the one that gets added to the list, after that, we have a simple for loop. The examples I've shown you thus far are fun, but here's something a little more useful.
Let's say I want to create a list holding 200 random integers between 1 and 1000. A list comprehension can do this for us easily!
>>> import random
>>> x = [random.randint(1,1000) for i in range(200)]
Done! This simple little line of code just gave us 200 random integers from 1 to 1000.
Python List Comprehensions are a valuable tool, and there is another feature to them I haven't even talked about yet, but that'll wait for next time!
Thursday, September 8, 2011
99 bottles of beer on the wall, 99 bottles of beer.
Take one down pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer.
Take one down pass it around, 97 bottles of beer on the wall.
2 bottles of beer on the wall, 2 bottles of beer.
Take one down pass it around, 1 bottle of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer.
Take one down pass it around, no more bottles of beer on the wall.
No more bottles of beer on the wall, No more bottles of beer.
Go to the store buy some more, 99 bottles of beer on the wall.
can you make a program to model this behavior? Let's make it more fun. The program must accept an optional argument for the number of bottles to start with. Also, notice that the grammar fr 2 bottles and 1 bottle are a bit different from the other values. At the end of the song, a different rhyme is spit out which also states the original number. Lastly, if the starting number is less than 1, you should probably state "NEED MORE BEER!".
Have fun, and always feel free to post questions and email me your submissions!
Wednesday, July 13, 2011
This email is from China domain name registration center, which mainly deal with the domain name registration and dispute internationally in China and Asia.
On July 11th 2011, We received Tianhua Ltd's application that they are registering the name " learningpythonprogramming " as their Internet Keyword and " learningpythonprogramming .cn "、" learningpythonprogramming .com.cn " 、" learningpythonprogramming .asia "domain names etc.., It is China and ASIA domain names. But after auditing we found the brand name been used by your company. As the domain name registrar in China, it is our duty to notice you, so I am sending you this Email to check. According to the principle in China, your company is the owner of the trademark, In our auditing time we can keep the domain names safe for you firstly, but our audit period is limited, if you object the third party application these domain names and need to protect the brand in china and Asia by yourself, please let the responsible officer contact us as soon as possible. Thank you!
Oversea marketing manager
Shanghai Office (Head Office)
3002, Nanhai Building, No. 854 Nandan Road,
Xuhui District, Shanghai 200070, China
Tel: +86 216191 8696
Fax: +86 216191 8697
If you are not appropriate person, please forward this to your CEO, because this is urgent. Thanks.
Monday, April 18, 2011
I haven't forgotten you guys nor have I forgotten my promises. I've been busy, but here's a little something that should be an easy challenge:
"Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"."
This program came from: http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html
Sunday, March 20, 2011
Friday, March 4, 2011
That’s right everyone, we can use Python to simulate playing cards. Based on these fundamentals, we could quite easily build a powerful card game!
What is a card? Seems like a dumb question, but let’s examine it just for giggles. A card consists of a suit and a rank. A suit might be clubs, spades, diamonds, or hearts whereas a rank is any number 2 through 10, and jack, queen, king, ace. For simplicity, it might be easier to assign all ranks to a numeric value. 2 – 10 are 2-10, Jack is 11, Queen is 12, King is 13, Ace if 14. For now, this seems like an easy way to create a card. As for suits, we can also easily abbreviate them to just their first letter, thus "s12" is the Queen of Spades. Bearing that in mind, we can easily think of a card as a string.
Making a single card is easy, but a little pointless. Instead, let's create an entire deck. We can think of a deck simply as a list holding all the cards.
#jack is 11, queen is 12, king is 13, Ace is 14
deck = 
for suit in 'sdhc':
for value in range(2,15):
This deck is in order, but we can easily shuffle it.
from random import shuffle
deck = makeDeck()
We now have a deck of cards! Lastly, let's create a hand. A hand can be thought of as a smaller list of cards.
def dealHand(deck, handSize=5):
hand = 
for i in range(handSize):
hand.append(deck.pop())#technically deals from botttom of deck
It's very easy to make and work with cards in Python. Perhaps next time we can start looking at some games we can play with our new cards.
Monday, February 14, 2011
Ciphers are a means of encoding data to securely share it with some people, but not all. In the simplest of situations, a cipher can be:
Normal letter -> cipher equivalent
a -> b
b -> c
c -> d
d -> e
z -> a
Deciphering this code is more of a chore than a challenge, but using Python we can develop our own, more powerful ciphers. Later on, we could move to full blown cryptography building on these fundamental principles. Let's start by writing a simple program that can take a string and encode it. Before we begin, you should know that every character has a numerical equivalent (this is called an ASCII value). To find out a character's ascii value, simply type ord(character). To take an ascii value and turn it into a character, you can type chr(asciiValue). Also, to simplify things, let's only concern ourselves with letters. We don't want to worry about changing around whitespace.
retString = ""
for letter in givenString:
if letter.isalpha(): #skip non a-z characters
if letter == 'z':
retString += 'a'
elif letter == 'Z':
retString += 'A'
retString += chr(ord(letter)+1)
retString += letter
This function should take any string and cipher it. Can you write a function to decipher a given string?