Help a noob w/ multiple on-going questions...

This is the place for queries that don't fit in any of the other categories.

Help a noob w/ multiple on-going questions...

Postby josh623 » Thu May 15, 2014 9:17 pm

Instead of me posting a bunch of different threads (because I am sure there will be multiple questions) I figured I would just post everything here. Thanks for being patient with me guys!

So I am working my way through learnpythonthehardway.org and am up to lesson 20. I understand everything so far, but only because my father in law (who has been a programmer forever) has helped me along the way. I just don't always want to bug him :)
josh623
 
Posts: 13
Joined: Tue Apr 08, 2014 4:15 am

Re: Help a noob w/ multiple on-going questions...

Postby josh623 » Thu May 15, 2014 9:17 pm

here is lesson 20 for reference (so you can see the whole code).
http://learnpythonthehardway.org/book/ex20.html

so anyway, this is the part about functions... i understand how to define your own functions (neat feature!) but i don't understand this code below:

Code: Select all
def rewind(f):
    f.seek(0)


what does the 'dot' in f.seek(0) mean? is it just specifying which file you want to 'seek' in so python knows? shouldn't it just know what to 'seek' from the line above (since the f.seek is below and indented under where you define the rewind(f) function).

also... couldn't I just use this code below instead? what is the purpose of labeling it as 'f'?

Code: Select all
current_file.seek(0)


Thanks for any help guys.
josh623
 
Posts: 13
Joined: Tue Apr 08, 2014 4:15 am

Re: Help a noob w/ multiple on-going questions...

Postby josh623 » Thu May 15, 2014 9:44 pm

Code: Select all
current_file = open(input_file)


in this same example (lesson 20), i don't think i am fully understanding the open() function. is it right to say 'open(input_file)' is returning a file handle (i.e. returns a certain integer spot) in the RAM? can someone explain more fully in laymen's terms? thanks
josh623
 
Posts: 13
Joined: Tue Apr 08, 2014 4:15 am

Re: Help a noob w/ multiple on-going questions...

Postby Marbelous » Thu May 15, 2014 9:57 pm

The idea here is that you're passing in an object (called 'f') to the rewind function that already has a method called seek, then calling that seek method within the function called rewind.

Dot notation is a way to access methods and attributes contained in an object. http://www.codecademy.com/forum_questio ... 2d9a0012f5

You need to understand the concept of namespaces which throw a lot of people but it's really just a simple naming convention. Think of a namespace as a container used to differentiate similar objects. In my company there are several men named Bob. I can differentiate all the Bobs without even knowing their last name by calling them Bob from Accounting, Bob from Sales, etc. In this example Accounting and Sales are namespaces. This concept can be expanded. If we have Eastern and Western divisions in my company I can identify a particular Bob by saying "The Bob from Sales in the Western Division or Western.Sales.Bob! In Python pretty much everything is an object and you can access the "stuff" inside it with dot notation.

Code: Select all
my_string = "hello world"  # Poof, a new string object is created with all the power of python strings.
print my_string.title  # Capitalizes Every Word
print my_string.index('w')  # Returns index of the "w" in "world"

Play around with this. Even with IDLE, once you create an object (like my-string) you can type the dot (period) and if you wait a second, IDLE will open a pop-up with all the attributes and methods of that object type for you to select. Try it...
Marbelous
 
Posts: 143
Joined: Fri May 31, 2013 8:12 pm

Re: Help a noob w/ multiple on-going questions...

Postby Marbelous » Thu May 15, 2014 10:03 pm

josh623 wrote:
Code: Select all
current_file = open(input_file)


in this same example (lesson 20), i don't think i am fully understanding the open() function. is it right to say 'open(input_file)' is returning a file handle (i.e. returns a certain integer spot) in the RAM? can someone explain more fully in laymen's terms? thanks


Kind of. The take away point though is that all that tricky hardware based stuff like RAM address and file IO are abstracted away so you don't need to know about it. Instead you have a FILE OBJECT that has it's own attributes and methods which you can conveniently use to do FILE-LIKE things such as read, seek, flush, etc.
Marbelous
 
Posts: 143
Joined: Fri May 31, 2013 8:12 pm

Re: Help a noob w/ multiple on-going questions...

Postby 7stud » Thu May 15, 2014 11:03 pm

what does the 'dot' in f.seek(0) mean? is it just specifying which file you want to 'seek' in so python knows?

Exactly. Suppose you did this:

Code: Select all
f1 = open('data1.txt')
f1.read(5)
f2 = open('data2.txt')
f2.read(10
f3 = open('data3.txt')
f3.read(7)

seek(0)


python wouldn't know which file seek() applies to.


shouldn't it just know what to 'seek' from the line above (since the f.seek is below and indented under where you define the rewind(f) function)

It's possible the python parser(i.e "code reader") could be programmed to try and deduce that, but it's much simpler to write a parser that calls seek() on whatever is specified to its left.

what does the 'dot' in f.seek(0) mean?

You've probably been dealing with functions and variables so far, but there are also things called classes. You'll learn about those later. For now, just accept that you have to use that syntax to do stuff to the file you opened.
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am

Re: Help a noob w/ multiple on-going questions...

Postby josh623 » Thu May 15, 2014 11:07 pm

Marbelous wrote:The idea here is that you're passing in an object (called 'f') to the rewind function that already has a method called seek, then calling that seek method within the function called rewind.

Dot notation is a way to access methods and attributes contained in an object. http://www.codecademy.com/forum_questio ... 2d9a0012f5

You need to understand the concept of namespaces which throw a lot of people but it's really just a simple naming convention. Think of a namespace as a container used to differentiate similar objects. In my company there are several men named Bob. I can differentiate all the Bobs without even knowing their last name by calling them Bob from Accounting, Bob from Sales, etc. In this example Accounting and Sales are namespaces. This concept can be expanded. If we have Eastern and Western divisions in my company I can identify a particular Bob by saying "The Bob from Sales in the Western Division or Western.Sales.Bob! In Python pretty much everything is an object and you can access the "stuff" inside it with dot notation.

Code: Select all
my_string = "hello world"  # Poof, a new string object is created with all the power of python strings.
print my_string.title  # Capitalizes Every Word
print my_string.index('w')  # Returns index of the "w" in "world"

Play around with this. Even with IDLE, once you create an object (like my-string) you can type the dot (period) and if you wait a second, IDLE will open a pop-up with all the attributes and methods of that object type for you to select. Try it...


That helps a lot! Thanks! A few questions about your points...

in the 'Western.Sales.Bob' example, western and sales would be namespaces but bob would be the method? is the last thing in the 'dot string' (my made up word) always the method? just wanting to clarify.

also, what is IDLE?
josh623
 
Posts: 13
Joined: Tue Apr 08, 2014 4:15 am

Re: Help a noob w/ multiple on-going questions...

Postby josh623 » Thu May 15, 2014 11:13 pm

7stud wrote:
Code: Select all
f1 = open('data1.txt')
f1.read(5)
f2 = open('data2.txt')
f2.read(10
f3 = open('data3.txt')
f3.read(7)

seek(0)



So in your example, I could do

Code: Select all
f1.seek(0)


correct? Thanks... I like getting multiple answers on this stuff because everyone has a different view of it and it helps me a ton to have different viewpoints coming at me!
josh623
 
Posts: 13
Joined: Tue Apr 08, 2014 4:15 am

Re: Help a noob w/ multiple on-going questions...

Postby josh623 » Thu May 15, 2014 11:15 pm

do you always have to open(a_file.txt) before you can use it in the program? I am assuming so
josh623
 
Posts: 13
Joined: Tue Apr 08, 2014 4:15 am

Re: Help a noob w/ multiple on-going questions...

Postby josh623 » Fri May 16, 2014 12:07 am

Code: Select all
def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

def subtract(a, b):
    print "SUBTRACTING %d - %d" % (a, b)
    return a - b

def multiply(a, b):
    print "MULTIPLYING %d * %d" % (a, b)
    return a * b

def divide(a, b):
    print "DIVIDING %d / %d" % (a, b)
    return a / b

print "Let's do some math with just functions!"

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)


# A puzzle for extra credit, type it in anyway.
print "Here is a puzzle."

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print "That becomes: ", what, "Can you do it by hand?"


so I pretty much understand this... but the part that I don't understand is when it says

Code: Select all
age = add(30, 5)


and then later says

Code: Select all
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)


how come python, for the first one (age = add(30, 5)) it returns the string under that defined function at the top. but for the second one (print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)) it returns an intiger (i.e. the 'return' part under the SAME defined function. Shouldn't they return the same thing?
josh623
 
Posts: 13
Joined: Tue Apr 08, 2014 4:15 am

Re: Help a noob w/ multiple on-going questions...

Postby 7stud » Fri May 16, 2014 4:55 am

is the last thing in the 'dot string' (my made up word) always the method? just wanting to clarify.

No, it can be a variable name as well. Here is a simple example:

Code: Select all
class Dog:
    def bark(self):
        print "bow wow"

d = Dog()
d.bark()
d.age = 10
print d.age

--output:--
bow wow
10

HOWEVER...you are getting waaaay ahead of yourself.

correct? Thanks

Yes. All file objects can call the seek() method, so you have to tell python which file is calling seek().

how come python, for the first one (age = add(30, 5)) it returns the string under that defined function at the top.

add() doesn't return a string--add() prints a string, then returns something else, which is assigned to the age variable. Here is a similar example:


Code: Select all
def add():
    print "hello"
    print "world"
    return "goodbye"

x = add()
print "x is equal to %s" % x

--output:--
hello
world
x is equal to goodbye

Python replaces all "function calls" in your code with the return value of the function. A "function call" is a name with some parentheses after it, e.g. add(1, 5). Whenever python sees a function call in your code, execution stops at the point of the function call, execution jumps to the function and python executes the function, then execution jumps back to the point where execution was previously halted and python substitutes the function call with the return value of the function; then execution continues.

A rule of thumb when you are substituting into a string: ALWAYS use %s unless that doesn't do what you want. If you don't get the formatting you want with %s, then you can try %d, %f, etc.

(print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)) it returns an intiger (i.e. the 'return' part under the SAME defined function. Shouldn't they return the same thing?

The function name is add(). add() is not called in that print statement, so it is irrelevant what add() does or doesn't do. All that matters are the values assigned to the variables age, height, weight, and iq.
Last edited by 7stud on Fri May 16, 2014 6:14 pm, edited 1 time in total.
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am

Re: Help a noob w/ multiple on-going questions...

Postby Kebap » Fri May 16, 2014 6:01 am

josh623 wrote:do you always have to open(a_file.txt) before you can use it in the program? I am assuming so


Don't just assume. Open your python interactive console and see what happens, if you try to search through a file which you did not open before. There is quick learnings here. :)
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Help a noob w/ multiple on-going questions...

Postby Marbelous » Fri May 16, 2014 2:13 pm

josh623 wrote:
That helps a lot! Thanks! A few questions about your points...

in the 'Western.Sales.Bob' example, western and sales would be namespaces but bob would be the method? is the last thing in the 'dot string' (my made up word) always the method? just wanting to clarify.

also, what is IDLE?


No, not always. In this case, most likely "Bob" would be an instance of a class with his own methods and attributes that can be accessed by typing another DOT. I would suggest you don't go too far into OOP concepts yet. Stick with learning how variable, data structures and functions work. When you get to OOP you'll see that variables and structures are now called "attributes" and functions are called "methods" but they're really the same thing made even more useful since they apply to classes and the instances made from those classes. OOP is something you eventually "Grok" by working with it until the light bulb comes on in your head. Just give it time...

IDLE is the Integrated Development Environment (IDE) that comes included with most python distributions. It's used to write, run and de-bug python code. You don't need to use an IDE. You can always just write code in a text editor (Notepad, etc.) and run it from the command line. Once you get comfortable with python syntax though, you'll find a good IDE can be a big help. NOTE: IDLE in not very good but it's simple and almost always available so it's not a bad starter IDE. Since you're using LPTHW I assume they want you to avoid the shortcuts an IDE provides so you can focus on core python. BTW, it's named after Eric Idle, one of the Monty Python guys.
Marbelous
 
Posts: 143
Joined: Fri May 31, 2013 8:12 pm

Re: Help a noob w/ multiple on-going questions...

Postby josh623 » Mon May 19, 2014 11:34 pm

7stud wrote:
how come python, for the first one (age = add(30, 5)) it returns the string under that defined function at the top.

add() doesn't return a string--add() prints a string, then returns something else, which is assigned to the age variable. Here is a similar example:

Code: Select all
def add():
    print "hello"
    print "world"
    return "goodbye"

x = add()
print "x is equal to %s" % x

--output:--
hello
world
x is equal to goodbye


so when you assign a function to a variable (i.e. age = add(30, 5)) then initially when you do the "assigning" (forgive my incorrect terminology!) then it will print the string. But then whenever you call the function it will return a value?

Python replaces all "function calls" in your code with the return value of the function. A "function call" is a name with some parentheses after it, e.g. add(1, 5). Whenever python sees a function call in your code, execution stops at the point of the function call, execution jumps to the function and python executes the function, then execution jumps back to the point where execution was previously halted and python substitutes the function call with the return value of the function; then execution continues.


so in this example:

Code: Select all
age = add(30, 5)


is the add(30, 5) not a function call? I am just confused as to why this doesn't return a value and returns a string initially, but later when you refer to the age it returns the value. it seems to me like the age is supposed to equal or be the same thing as the value, just like when you assign variables... sorry, I'm just confused!

(print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)) it returns an intiger (i.e. the 'return' part under the SAME defined function. Shouldn't they return the same thing?

The function name is add(). add() is not called in that print statement, so it is irrelevant what add() does or doesn't do. All that matters are the values assigned to the variables age, height, weight, and iq.


this is what I am confused about... it seems to me that add() is ultimately called because it uses 'age' which refers up a couple lines to the function add()... but I don't think I am thinking about this correctly... age should just refer to the VALUE of the called function add(), is this more correct?
josh623
 
Posts: 13
Joined: Tue Apr 08, 2014 4:15 am

Re: Help a noob w/ multiple on-going questions...

Postby josh623 » Mon May 19, 2014 11:36 pm

Kebap wrote:
josh623 wrote:do you always have to open(a_file.txt) before you can use it in the program? I am assuming so


Don't just assume. Open your python interactive console and see what happens, if you try to search through a file which you did not open before. There is quick learnings here. :)


Good point thank you!
josh623
 
Posts: 13
Joined: Tue Apr 08, 2014 4:15 am

Re: Help a noob w/ multiple on-going questions...

Postby josh623 » Mon May 19, 2014 11:39 pm

Marbelous wrote:IDLE is the Integrated Development Environment (IDE) that comes included with most python distributions. It's used to write, run and de-bug python code. You don't need to use an IDE. You can always just write code in a text editor (Notepad, etc.) and run it from the command line. Once you get comfortable with python syntax though, you'll find a good IDE can be a big help. NOTE: IDLE in not very good but it's simple and almost always available so it's not a bad starter IDE. Since you're using LPTHW I assume they want you to avoid the shortcuts an IDE provides so you can focus on core python. BTW, it's named after Eric Idle, one of the Monty Python guys.


Thanks for the tip on slowing down and not diving in too deep... that is the hard part, to focus on what i need to be focused on and the rest will come.

So right now I am using a Linux Mint OS right now with bash as my command prompt and gedit as my text editor... seems to be working well! Yeah I definitely need to learn the basics before graduating to something that gives you shortcuts =)
josh623
 
Posts: 13
Joined: Tue Apr 08, 2014 4:15 am

Re: Help a noob w/ multiple on-going questions...

Postby 7stud » Tue May 20, 2014 5:07 am

then initially when you do the "assigning" (forgive my incorrect terminology!) then it will print the string.

The assignment does not cause the function to print the string, it's the function call that causes the function to print the string. If you put "(...)" after a function name, it tells python to execute the function. Try this:

Code: Select all
add(30, 5)


The try this:

Code: Select all
age = add(30, 5)


They will both print the string. But only the second one assigns the return value of add() to the age variable.

You have to explain why it is that when you see a string printed to your screen, you think that is the return value of the function...when the rule is: the return value of a function is the value specified in the function's return statement. Here is a quiz, identify the return value of the following function:

Code: Select all
def do_stuff():
    print 'hello'
    print 'world'
    return 'goodbye'


One you can identify the return value of a function, then you know that python will replace the function call in your code with that return value.

age should just refer to the VALUE of the called function add(), is this more correct?

age will refer to the return value of the function add().

It's really as simple as printing out your code, crossing out every function call and replacing it with the return value of the function, and then trying to determine what the resulting code does. Try it with the following code, and post the code after you have replaced every function call with the function's return value:

Code: Select all
def add(x, y):
    print x + 200
    print 'hello'
    return 10

def do_something():
    f = open('output.txt', 'w')
    f.write("Today is a fine day.")
    f.close()
    return "hello world"

def speak(sound):
    print "I speak like this: {}".format(sound)
    return 100

def dance():
    print "Tappity tap tap tap"
    return add(1, 1)

a = add(10, 20)

do_something()
b = speak('woof')

print a+b

x = dance()
print x
print dance()

7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am

Re: Help a noob w/ multiple on-going questions...

Postby josh623 » Tue Aug 19, 2014 3:43 am

7stud wrote:Identify the return value of the following function:

Code: Select all
def do_stuff():
    print 'hello'
    print 'world'
    return 'goodbye'


One you can identify the return value of a function, then you know that python will replace the function call in your code with that return value.


Let me see if I get your quizes right ;) thanks!

The return value in the above would be 'goodbye' so that when you assign the function to a variable, such as:
Code: Select all
work = do_stuff()

then Python will print:
Code: Select all
hello
world


but when you refer to 'work' later in the program then the return value would be 'goodbye', correct?

It's really as simple as printing out your code, crossing out every function call and replacing it with the return value of the function, and then trying to determine what the resulting code does. Try it with the following code, and post the code after you have replaced every function call with the function's return value:

One you can identify the return value of a function, then you know that python will replace the function call in your code with that return value.

Code: Select all
def add(x, y):
    print x + 200
    print 'hello'
    return 10

def do_something():
    f = open('output.txt', 'w')
    f.write("Today is a fine day.")
    f.close()
    return "hello world"

def speak(sound):
    print "I speak like this: {}".format(sound)
    return 100

def dance():
    print "Tappity tap tap tap"
    return add(1, 1)

a = add(10, 20)

do_something()
b = speak('woof')

print a+b

x = dance()
print x
print dance()



Ok here you go... I commented in the below code, see what you think.

Code: Select all
def add(x, y):
    print x + 200
    print 'hello'
    return 10

def do_something():
    f = open('output.txt', 'w')
    f.write("Today is a fine day.")
    f.close()
    return "hello world"

def speak(sound):
    print "I speak like this: {}".format(sound)
    return 100

def dance():
    print "Tappity tap tap tap"
    return add(1, 1)
# since we have not assigned the add() function to a variable yet all it knows to do is to 'print' when this function is called. and it will print '201' and 'hello'

a = add(10, 20)
# here you assign the function add() with the perimeters of '10' and '20' as being variables within this function. So in the program when executed is this will not actually give me a return value yet, but will look like:
[code]210
hello[/code]

do_something()
# we are getting into things a little bit above my head here, but I will take a wild guess. Shouldn't your original defined function 'do_something()' have a 'do_something(f)' with an f in it? I don't understand how it can still work and be blank? Anyway, whatever file is passed to this function it will open it with writable permissions, write "Today is a fine day." in the file, and then close it.

b = speak('woof')
# you are assigning the function speak('woof') to the variable 'b' so that whenever 'b' is called the return value would be '100'

print a+b
# now is when we get into getting the return values. the return value for 'a' is '10' and the return value for 'b' is '100' so Python will actually print '110', correct?

x = dance()
print x
# Python will print the return value of dance() which we actually have a function add(1, 1) within a function dance(). The return value that will be printed will be '10'

print dance()
# it will print "Tappity tap tap tap."

josh623
 
Posts: 13
Joined: Tue Apr 08, 2014 4:15 am

Re: Help a noob w/ multiple on-going questions...

Postby snippsat » Tue Aug 19, 2014 7:37 pm

Ok here you go... I commented in the below code, see what you think.

There are problems,the basic understanding of function seems lacking.
Argument y is never used,and i point out what make no sense.
Code: Select all
def add(x, y):
    print x + 200 # 200 why?
    print 'hello' # why?
    return 10 # make no sense

This is how it should look.
Code: Select all
def add(x, y):
    return x + y

>>> add(4, 8)
12

A function that have 2 argument shall use both,and have a clear task here to add 2 number.
You do not bring in a lot of nonsense and other task.

Some fix to other functions.
Code: Select all
def write_to_file(file_in, text):
    f = open(file_in, 'w')
    f.write(text)
    f.close()

Code: Select all
def speak(sound):
    print "I speak like this: {}".format(sound)

Functions are first-class objects in Python.
you can pass them around as argument,this much better than magic take function from global space.
Code: Select all
def add(x, y):
    return x + y

def dance(func,x,y):
    return 'Tappity from <{}> result from <add> {}'.format(dance.__name__, func(1,5))

print dance(add,5,4)
#--> Tappity from <dance> result from <add> 6
User avatar
snippsat
 
Posts: 221
Joined: Thu Feb 21, 2013 12:04 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests

cron