The Big Think

December 23, 2010

Hello World

Filed under: Computing — jasony @ 10:24 pm

Beginning my plow through the Python programming language. Thanks to Sean (and co-worker) for pointing out this free online book. I had just put the book into my Amazon basket when he pointed out the free PDF version. Thanks, Sean!

*update* Just wrote my first program (my blog software won’t indent, but you get the picture):

#this is the number guessing game
import random

guessesTaken=0

print (‘hello! what is your name?’)
myName=input()

number = random.randint(1,20)
print (‘well, ‘ +myName+ ‘, I am thinking of a number between 1 and 20.’)
print (‘You have 6 guesses’)
while guessesTaken < 6: print("Take a guess.")#There are four spaces in front of print. guess=input() guess=int(guess) guessesTaken=guessesTaken+1 if guess < number: print ('your guess is too low.') if guess > number:
print (‘your guess is too high’)

if guess == number:
break

if guess == number:
guessesTaken = str(guessesTaken)
print (‘Good job, ‘ +myName+ ‘! You guessed my number in ‘ +guessesTaken+ ‘ guesses!’)

if guess != number:
number = str(number)
print (‘Nope. The number I was thinking of was ‘ + number)

12 Comments »

  1. I also found http://docs.python.org/tutorial/ useful. But I was coming from it from a different perspective: someone who knows how to write software wanting to learn a new language. Worth a look though. It’s free, and pretty good.

    Comment by greg — December 23, 2010 @ 11:19 pm

  2. If you can tell me what the following does without running it, you’re well on your way to being a Jedi Master 🙂

    z = lambda n: [x for x in range(2, n + 1) if len([i for i in range(2, x) if x % i == 0]) == 0]
    print(z(50))

    Comment by greg — December 24, 2010 @ 12:29 am

  3. So let me deconstruct it here and take a shot:

    z = lambda n: [x for x in range(2, n + 1) if len([i for i in range(2, x) if x % i == 0]) == 0]
    print(z(50))

    working backwards, you’re trying to get the computer to print the value of z times the integer of 50? Is that what the print(z(50)) means?

    Going to the beginning, you say z= the lambda of n and then calculate n to be the long expression of [x for x in range(2, n + 1) if len([i for i in range(2, x) if x % i == 0]) == 0]. But I have no idea what the expression means. I see a “len” in there, and knowing that you work in Labview, I’m going to guess that it’s a “length” value.

    So I’ll take a shot and say the distance on the Z axis is equal to some multiplier of the length of n, and for some reason you’re multiplying it by 50 at the end. Am I even close?

    Jason

    Comment by jasony — December 24, 2010 @ 9:00 am

  4. Sorry, but not even close 🙂 But that had a lot of curve-balls, and was somewhat deliberately made unreadable 🙂 I found it off the web.

    A lambda is an anonymous function, and is assigned to z. So that makes z a function, and in the lambda, the value passed to z is n. So z(5) sets the value on n in the lambda. So the last line prints the result of the function with parameter 50.

    The function actually computes all the primes numbers between 2 and n.

    “x % i” == 0 means “the remainder of x divided by i is equal to zero”, or x is evenly divided by i.

    for… means “for each of the following values, do something on each one”. So basically, the code looks at each number from 2 to n, and on that number, tries to divide it by every number lower than it. If there are no numbers that evenly divide it, put it in a list. All the square brackets do some magic related to the list. As written, this code is very hard to decipher. I found it by searching google for “python obfuscate primes” 🙂

    Comment by greg — December 24, 2010 @ 11:26 am

  5. Wow, this padawan has a lot to learn. 🙂

    And that was mean.

    Comment by jasony — December 24, 2010 @ 11:31 am

  6. Yes, it was mean. I’m not a nice person 🙂

    Comment by greg — December 24, 2010 @ 11:41 am

  7. ps-A sane programmer would write it more like this, which can actually be read:

    def primes(n):
    for x in range(2, n):
    foundDivisor = False
    for y in range(2, x):
    if (x % y) == 0:
    foundDivisor = True
    break
    if foundDivisor != True:
    print(x)

    primes(50)

    Comment by greg — December 24, 2010 @ 3:13 pm

  8. One of the big breakthroughs for me was when I realized that doing programming was more forgiving than I had thought. More akin to making brownies than writing an equation. When I saw how different programmers would accomplish the same thing and realized that you could go about solving a programming problem in many different ways it made me much more comfortable about my horrendously inept and convoluted code. As long as it gets the job done, right?

    Seeing super-elegant and condensed code still scares me off, but I realize more now that this sort of thing is the goal, not a requirement. I can still hack my way through to get it done.

    Comment by jasony — December 24, 2010 @ 3:17 pm

  9. Yes, writing code is wonderful in how forgiving it is. It’s unlike making a physical object in that if you screw up, it usually means you can fix your mistake and try again, and nothing was lost/broken/consumed in the process. Which is very unlike things such as wordworking, where if you mess up, you may have to throw away a piece of wood and buy a new one, so mistakes can be very expensive. In programming, mistakes can be very cheap (at least at home; programming for a living with paying customers and making mistakes a whole different mess). This paradigm really encourages experimentation and creativity, because you can always try something, and if it doesn’t work out, try something else.

    ps-because whitespace is important in python, the above code can be found with proper indentation at http://wreck.org/~greg/primes.py.txt

    Comment by greg — December 24, 2010 @ 3:28 pm

  10. How would you define “Whitespace”? It’s mentioned in the tutorials but I can’t figure out an exact definition. Is it just the indents around blocks?

    Comment by jasony — December 24, 2010 @ 3:31 pm

  11. Whitespace is spaces, tabs, and newlines. Characters in a text file that aren’t drawn as a letter or a number, but as “whitespace”.

    In some languages, scoping and control flow and indicated through various punctuation characters. For instance, in C, you would write:

    if(someCondition)
    {
    callSomeFunction();
    }

    The curly braces indicate what code is only executed when someCondition is true. In python, this is indicated with indentation (which is actually very uncommon in programming languages because it makes them harder to parse for a computer). In C, you can remove all of the tabs and newlines, that program still means the same thing. In python, that would change the meaning of the program.

    Comment by greg — December 24, 2010 @ 4:04 pm

  12. Ah! Simultaneously answers the question I *had* (what is ‘whitespace’) as well as the unspoken one I’ve *always* had (what the heck are those funky, intimidating brackets in C?). Thanks, Sensei!

    Comment by jasony — December 24, 2010 @ 4:20 pm

RSS feed for comments on this post.

Leave a comment

You must be logged in to post a comment.

Powered by WordPress