PDA

View Full Version : Learning Python.




larswik
Mar 28, 2009, 04:51 PM
Hello. This might be an odd question. Are there any references books or websites that help explain the terminology in programing? I am learning Python and I can follow along just fine but I am getting lost in the terminology. Example "passing an argument to a variable". Words that are between "" are STRINGS but they are know as ARGUMENTS too.

So... Is there any kind of reference material that outlines what types of things can pass to a variable? Something that outlines scopes, parameters, functions, floats, strings and so on.

Following along with the book is easy. But trying to write my own code and understanding the flow or coding is difficult.

Sorry if my question is strange.

-Lars



Jpoon
Mar 28, 2009, 04:54 PM
I generally google'd the definition I was looking for in the context of the programming language that I was currently utilizing. Why buy a book when it's just a search engine away ? :D

hhas
Mar 28, 2009, 06:12 PM
Hello. This might be an odd question. Are there any references books or websites that help explain the terminology in programing? I am learning Python and I can follow along just fine but I am getting lost in the terminology. Example "passing an argument to a variable". Words that are between "" are STRINGS but they are know as ARGUMENTS too.

Yeah, learning jargon's a chore.

What are you learning Python from? A good book should explain the terminology used as it goes, e.g.:

http://greenteapress.com/thinkpython/thinkCSpy/

martychang
Mar 29, 2009, 05:50 AM
Have any experience messing with OS'es at the command line level? Alot of THAT terminology is just as applicable there.

Arguments are additional things you tell a function or command about what it's supposed to do. Most command line utilities have a default behavior when no arguments are supplied, but alternative ones when one or more are supplied. Likewise a command like "cp"(copy) has to have at least two arguments: the file it is to copy and the destination. Same for functions and such in programming, though they will often REQUIRE an exact number of specific arguments. A string might be an argument in programming if you're having a function operate on that string.

larswik
Mar 30, 2009, 03:10 PM
Thanks for the reply's. I am learning from a book called "Python Programming" by Mike Dawson. The book is great and he explains things fine. Wrapping my head around jargon is tough. I got to page 165 and started learning functions. I understand the concept so I thought I would write my own code using it as an example to better understand it, but it failed and I don't know why.

here is the code.....

import random

def roll_um():
roll = random.randrange(6)+1

#main

raw_input("Press Return to roll a 6 sided dice")
print roll_um

....... I thought with everything I learnded this would print a random number from 1 to 6 but it returns something that is this...<function roll_um at 0xe15af0>


Being a monkey and reproducing what the book wrote is one thing. Trying to use that knowledge to write your own code is tuff.

-Lars

jpyc7
Mar 30, 2009, 04:41 PM
Thanks for the reply's. I am learning from a book called "Python Programming" by Mike Dawson. The book is great and he explains things fine. Wrapping my head around jargon is tough. I got to page 165 and started learning functions. I understand the concept so I thought I would write my own code using it as an example to better understand it, but it failed and I don't know why.

here is the code.....

import random

def roll_um():
roll = random.randrange(6)+1

#main

raw_input("Press Return to roll a 6 sided dice")
print roll_um

....... I thought with everything I learnded this would print a random number from 1 to 6 but it returns something that is this...<function roll_um at 0xe15af0>


Being a monkey and reproducing what the book wrote is one thing. Trying to use that knowledge to write your own code is tuff.

-Lars

At the same indentation level as "roll = random.randrange(6)+1", you need the statement "return roll".
Replace the line "print roll_um" with "print roll_um()".

The parentheses indicates that you are calling the function. When a function requires arguments (also called parameters), they are inside the parentheses.

You should also know, if not already mentioned, that Python is a dynamically typed language. Without going into it, other languages are statically typed. It is quicker to start programming a dynamically typed language because you don't have to declare the variable type when you write the program. The downside is that using the wrong type of a variable later will cause a problem when you execute the program.

I don't know macrumors well enough to get the "code" snippets indented correctly (and I guess you don't either), because they aren't appearing with it. So I hope you can figure out where I said to change your code.

Gruffalo
Mar 30, 2009, 04:45 PM
Thanks for the reply's. I am learning from a book called "Python Programming" by Mike Dawson. The book is great and he explains things fine. Wrapping my head around jargon is tough. I got to page 165 and started learning functions. I understand the concept so I thought I would write my own code using it as an example to better understand it, but it failed and I don't know why.

here is the code.....

import random

def roll_um():
roll = random.randrange(6)+1

#main

raw_input("Press Return to roll a 6 sided dice")
print roll_um

....... I thought with everything I learnded this would print a random number from 1 to 6 but it returns something that is this...<function roll_um at 0xe15af0>


Being a monkey and reproducing what the book wrote is one thing. Trying to use that knowledge to write your own code is tuff.

-Lars

I'm not sure I can really help with your reference book. I think the terminology you sort of pick up as you go along, and the best way to learn is by trying to achieve something yourself. So you are probably going about it the right way. Stick at it and things will get clearer.

You are almost there with your test, however, what that message is telling you is that roll_um is a function - so you've done something right! ;-) Two things need to be fixed.

First is that when you 'call' a function you are expected to use the notation function_name(argument list). The number of items you pass to the function needs to be the same as the function expects to receive. In your case, your function doesn't take any 'arguments' so you call it using 'roll_um()' i.e. no arguments.

The other thing is that you are expecting the roll_um function to 'return' a value back to the place where it was called from, the print statement. So your function needs to have a return statement, i.e. return roll, after the 'statement' where you 'assign' the value to the roll 'variable'.

As an exercise you could make the function roll_n_sided_dice, which does take an argument of the number of sides of the dice.

eddietr
Mar 30, 2009, 04:50 PM
Functions in Python are objects. So "roll_um" in your case is an object that happens to be a function.

So printing "roll_um" will print the description of that object, which is what you are seeing.

To actually execute the function and print the result (what the function returns), then you would want:


print roll_um()


But you have one more issue, which is that your function does not return anything. So you probably want to do this:


def roll_um():
return random.randrange(6) + 1


or I would probably prefer:


def roll_um():
random.randint(1,6) # imho, this is more clear


The latter is a matter of personal preference/style of course. It's up to you.

larswik
Mar 30, 2009, 10:54 PM
Thanks guys... That clears things up. When I wrote the code I did have a RETURN ROLL but I was missing the ...print roll_um() parenthesis. I will test it tonight when I get home.

It's the small things like this that I wanted a reference manual of some kind that broke down the jargon. Things are fine when I follow the book but goes crazy when I try to write my own stuff.

Thanks for the... if help >= no help:
print "Thanks you"
else:
print"i'm freaking out!"