2015-02-23 16:05:40

Guitarman,

I have sent you a forum email, whatever that means smile  Please see my response there smile

2015-02-24 07:57:35

Hi.
Well that was weird yesterday I couldn't post in this topic now I can.
Anyway I've been working hard on python. I'm actually understanding a lot. But I got stuck on arguments. I tried to write a file and import the module argv when I did this I got an error message is this module under a different name?
Also when I tried this:
print """
Text goes all through here.
"""
When I typed this in the program I got an error. But when I tried this same thing in a different file without new line characters it worked fine so what am I doing wrong?
And can someone explain what an argument is and why on earth I would need it?

Guitarman.
What has been created in the laws of nature holds true in the laws of magic as well. Where there is light, there is darkness,  and where there is life, there is also death.
Aerodyne: first of the wizard order

2015-02-24 17:32:20

I'm not sure what you're doing wrong because I don't have both files.  If you did one in the REPL, it is quite possible that you simply typoed something.  If you had two script files and one was working and the other not, then there is obviously a difference.  In such cases, turn your screen reader's punctuation higher: if you accidentally hit apostrophe instead of quote it can screw up, for example.
As for command line arguments (so called to distinguish them from function arguments), I assume that whatever you're looking at is referring to sys.argv.  Sys is the module, argv is a variable in the module.  It is worth mentioning at this point that Python programmers dislike from foo import * because then it's not so easy to answer the question "where does this thing come from?" and it makes interactive exploration in the console hard (import a module sometime and call dir() on it).  What you're reading may not be doing that, I don't know, but it seems like a very good place to say that it's considered bad practice.
As for what sys.argv is, it's what lets you write programs that take command line arguments.  For example something that displays a file.  I use that in a Python script that takes some input files and generates some C++ for Libaudioverse.  Pip uses it to let you install new packages.  When you do:

python myscript.py a b c

Then sys.argv gets set to ['myscript.py', 'a', 'b', 'c'].
So why you care about that depends on what you're doing.  There are more advanced helper modules that we typically use when the arguments get too complex or you're involving many options that can be set or not, but they all get back to that eventually, one way or another.  Whether or not you see the use now depends on whether or not you want to write command line tools of some sort, or really something that opens a file that you specify.  If you're not, then no, you don't need it yet.

My Blog
Twitter: @ajhicks1992

2015-02-24 21:03:55

Thank you camlorn. In the book there is a typo it says import sys argv instead of import sys.argv. Funny I guess I'm starting to learn about debugging code. This book is a miracle worker it's the first time I've been able to read and write programs that I can understand.
Right now I'm learning about raw_input. I was thinking of writing a program that would print something and ask for your name. When I do this do I use print like this?
print "Hello raw_input() how are you?"
I left out the rest of the code but would that call my name when I run it?
There is one more thing I didn't understand. What on earth is an escape character with all these single and double quotes and why would I want to use it? I read the escape character section about four times but it's just not getting through to me.

Guitarman.
What has been created in the laws of nature holds true in the laws of magic as well. Where there is light, there is darkness,  and where there is life, there is also death.
Aerodyne: first of the wizard order

2015-02-24 21:54:29

So the first example.  You want this:

print "Enter your name:"
name=raw_input()

And then you can use any of the following lines.  Knowing about all of these is important, though you need not understand everything here.  Which one you want depends on formatting.

print "hello,", name, "."
print "hello, "+name+"."
print "Hello, {}.".format(name)

Notice the space between your name and the period in the first one.  The second one works but is slow in other contexts because it's creating  and throwing out strings all day long.  The third replaces {} with the name itself, and is probably the fastest and best if you're doing something with string manipulation in a loop.  As a bonus, you can do:

result="hello, {} {}.".format("bob", "smith")

There are a bunch more ways to use the format function.  That's the basics of it.
Never put input or raw_input inside print.  You're not actually guaranteed that that will run at a specific time, and it's going to add extra newlines.  Expressions that do things are fine, but expressions that print things or ask for input are not a good idea as arguments to print.  Instead, specifically ask the questions first, put the answers in variables, and then use the variables to print whatever you want to print.
Now for escape characters and the use of apostrophe.  This is kind of bad I'm afriad, in the sense that it's a lot.  It's not complicated, it's just one of those stupid thigns you ahve to know, kind of like semicolon or indentation levels.  The nice thing is that this is almost the same in every programming language ever, so you only have to do it once.
Python has one type of string, but there are two ways to write it.  You may begin and end strings with either apostrophe or quote.  If you begin and end with quote, you can use apostrophes inside the string.  If you begin and end with apostrophe, you can use quotes inside of strings.  This works for multiline strings too, save that in a multiline string single or pairs of apostrophe and quote are always fine.  If you need to use both apostrophe and quote in a string, you have to pick one or the other to escape.  The following lines demonstrate this.  The first is invalid:

result="She said "hello""
result= 'she said "hello"'
result= "she said \"hello\""

And the last does it using escape characters.
There are other things you can't put in strings.  Unless you're using a multiline string, an example is the newline character.  To that end, \n becomes newline.  This is also an escape character.  If you have a string with newlines in it in the repl and don't explicitly print it, you can see them.  Other examples of escape characters include anything unprintable, the Unicode code points, and basically any character that's not on your keyboard (though for many of them it's something like \xfe86 or somesuch.  That's not correct, I think, but it demonstrates the idea).
\ is special in a string, basically.  To type a single \ in a string, you have to use two of them in a row.  Alternatively in Python specifically, you may turn off escape characters and use \ normally.  The cost of this is that you won't be able to use quote or newlines or anything inside the string itself.  Still, it's super helpful for file paths.  You do this by putting an r before the string as in:

r"c:\test\python"

This works with apostrophes too, so if you do need a quote you can get it by using apostrophes to begin and end the string.  It should also work with multiline strings, but I have never seen that, ever.
Here's a nice experiment.  DO this at the repl:

result="""Demonstration of escape characters.
notice the newlines.
Notice the newlines some more.
"""

And then type result so that the repl shows you the value of the variable without putting it through the stuff that processes strings when you print them with the print statement.
The only place you see the really exotic escape characters these days is when dealing with debugging binary data.  \n and the rules for quotes should be everything you need for a very long time.  You can also come back to this, if you want.  It will be really, really obvious when the day comes that you need it.  It's stupid-sounding until then.

My Blog
Twitter: @ajhicks1992

2015-02-25 22:57:12

So basically an escape character prevents you from having to do things like adding a new line character and things like that do I have that right?

Guitarman.
What has been created in the laws of nature holds true in the laws of magic as well. Where there is light, there is darkness,  and where there is life, there is also death.
Aerodyne: first of the wizard order

2015-02-25 23:29:40

More like lets.  There are things that cannot be typed in the middle of strings.  I thought of this explanation later.  I wish I had thought of it first.
Take your favorite editor out.  Write a string with some escape sequences.  Do a find/replace on those escape sequences with the thing they actually represent.  This is what your programming language does to them.  I could say that it's not allowed to write the word "test", but that I'm going to allow "\t" to stand for it.  Python says that it's not allowed to put newlines inside strings that start with only one quote or apostrophe, but that it's going to allow \n to stand for them.  Python says that you can't put a quote inside a string surrounded by quotes because then it won't know where the string ends, but it will understand \" to explicitly *not* end the string and then replace \" with just ".
The more advanced ones start representing characters that you can't type normally.  Why you need those is important.  But it's complicated and you'll figure it out for yourself at exactly the point you need to know it without any help from us, and I don't want to confuse things further than they have to be.

My Blog
Twitter: @ajhicks1992

2015-02-26 00:52:06

Hi Camlorn.
I think I've got it this time! It's funny when I started reading about escape characters I started thinking of a character that would use the escape key lol.
Anyway, I think I understand you use escape characters especially in strings because putting a new line or quote in the middle of strings would confuse the python interpreter so that's what escape characters are for.
Sorry for rambling on like this it's just that talking things out helps me to understand things better.

Guitarman.
What has been created in the laws of nature holds true in the laws of magic as well. Where there is light, there is darkness,  and where there is life, there is also death.
Aerodyne: first of the wizard order

2015-02-26 01:13:58

yeah.  You seem to have it.  Familiarity will come with practice, as always.

My Blog
Twitter: @ajhicks1992