Re: [Tutor] difference between expressions and statements

2014-04-10 Thread Jared Nielsen
Thanks for the thorough answer, Bob. I now understand the difference.
On Apr 10, 2014 2:11 PM, bob gailer bgai...@gmail.com wrote:

 Caveat: I began this before there were any other responses. So this may be
 overkill - but I ike to be thorough.

 On 4/9/2014 12:49 PM, Jared Nielsen wrote:

 Hi Pythons,
 Could someone explain the difference between expressions and statements?

 I know that expressions are statements that produce a value.

 No. Expressions are not statements. These are mutually exclusive.
 Expressions do produce values.

 An attempt at a thorough answer:

 In the language reference glossary under expression you will find:

 A piece of syntax which can be evaluated to some value. In other words,
 an expression is an accumulation of expression elements like literals,
 names, attribute access, operators or function calls which all return a
 value There are also statements which cannot be used as expressions,
 such as if. Assignments are also statements, not expressions.

 Tthe above is a quote; I don't like some of the grammar.

 In your examples print is a function. So all calls to print are
 expressions.

 In the language reference you will also find:

 7. Simple statements
 7.1. Expression statements
 7.2. Assignment statements
 7.3. The assert statement
 7.4. The pass statement
 7.5. The del statement
 7.6. The return statement
 7.7. The yield statement
 7.8. The raise statement
 7.9. The break statement
 7.10. The continue statement
 7.11. The import statement
 7.12. The global statement
 7.13. The nonlocal statement
 8. Compound statements
 8.1. The if statement
 8.2. The while statement
 8.3. The for statement
 8.4. The try statement
 8.5. The with statement
 8.6. Function definitions
 8.7. Class definitions

 With the exception of
 - 7.1. Expression statements
 - all of the above are either start with a keyword except 7.2 assignment
 which is indicated by an equal sign (=) .
 - all of the above cause something to happen (except pass), and do not
 return a value.

 7.1. Expression statement is either one expression or several separated by
 commas.
 Used interactively to display value(s).
 Used anywhere to make a function call.

 I'm unclear on functions and especially strings.
 Are any of the following expressions?

 print(42)
 print(spam)
 spam = 42
 print(spam)

 Is the first example producing a value or simply displaying an integer?

 All function calls return a value. In the case of print the return value
 is always None.
 spam = 42 is a statement. (indicated by the = sign. 42 is a value.

 Does a string count as a value?
 Yes - however I suspect you are limiting string to something within
 quotes. Those are string literals.
 Is a variable assignment considered a value?

 No

 If I print a variable is that considered production of a value?

 See above comment on print.

 Long but comprehensive answer. Feel free to ask questions.

 Note there are various subtleties here -some  keywords may be used to
 start a statement or in an expression - e.g. if, else, for yield.

 This also raises the fact that else (inter ala) is neither an expression
 or a statement; rather it is part of a compound statement. Nothing is
 simple.

 Oh there is more but I may never hit send


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about strip() and list comprehension

2014-04-09 Thread Jared Nielsen
Thank Danny,
That's much more clear.
But I still don't understand what's happening with:

if line.strip()

Is that stripping the line of white space at the same time that it is
testing it?


On Tue, Apr 8, 2014 at 3:44 PM, Danny Yoo d...@hashcollision.org wrote:

  Could someone explain why and how this list comprehension with strip()
  works?
 
  f = open('file.txt')
  t = [t for t in f.readlines() if t.strip()]
  f.close()
  print .join(t)


 Hi Jared,


 Let me rewrite this without the list comprehension, while preserving
 behavior.

 ##
 inputFile = open('file.txt')
 lines = []
 for line in inputFile.readlines():
 if line.strip():
 lines.append(line)
 inputFile.close()
 print .join(lines)
 ##

 I am changing the names of the variables from the original code
 because I find it very difficult to distinguish 't' from 'f'
 sometimes, and because those names are very tied in my mind to
 something else entirely (true and false).


 Does the above code make more sense to you than the version using the
 list comprehension syntax, or is there something there that is still
 confusing?


 Good luck to you.




-- 
http://jarednielsen.comhttp://bl-1.com/click/load/UWUANFE1UWRXMQRlAzI-b0231
http://thehelloworldprogram.comhttp://bl-1.com/click/load/UWUIPFczV2IDZVU0CDY-b0231
http://dototot.com http://bl-1.com/click/load/ATUBNQNnBjNWMFAxUm0-b0231
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about strip() and list comprehension

2014-04-09 Thread Jared Nielsen
Thanks Danny!
That was an awesome explanation.


On Tue, Apr 8, 2014 at 7:05 PM, Danny Yoo d...@hashcollision.org wrote:


 if line.strip()

 Is that stripping the line of white space at the same time that it is
 testing it?



 Two features about Python:

 1.  Strings are immutable, so the above is computing what a
 whitespace-stripped line would look like.  So that means that
 'line.strip()' is doing just a computation: it's not mutating the original
 line, but computing a new string that has its leading and trailing
 whitespace stripped away.


 2.  Empty strings are treated as false values.  I'm not happy with how
 loose Python treats truth, and would rather prefer:

 if line.strip() != : ...

 so that the thing being tested is explicitly either True or False.  I like
 my truth to be black and white, but I suppose I'll have to grimace and bear
 the fuzziness.  :P


 Together, we see those two features allow us to look at the test in the
 Python code:

if line.strip(): ...

 and rephrase it in English as:

If the line consists of at least one non-whitespace character: ...




-- 
http://jarednielsen.com
http://thehelloworldprogram.com
http://dototot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] difference between expressions and statements

2014-04-09 Thread Jared Nielsen
Hi Pythons,
Could someone explain the difference between expressions and statements?
I know that expressions are statements that produce a value.
I'm unclear on functions and especially strings.
Are any of the following expressions?

print(42)
print(spam)
spam = 42
print(spam)

Is the first example producing a value or simply displaying an integer?
Does a string count as a value?
Is a variable assignment considered a value?
If I print a variable is that considered production of a value?

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] question about strip() and list comprehension

2014-04-08 Thread Jared Nielsen
Hello,
Could someone explain why and how this list comprehension with strip()
works?

f = open('file.txt')
t = [t for t in f.readlines() if t.strip()]
f.close()
print .join(t)

I had a very long file of strings filled with blank lines I wanted to
remove. I did some Googling and found the above code snippet, but no clear
explanation as to why it works. I'm particularly confused by how if
t.strip() is removing the blank lines. I also don't fully understand the
'print .join(t)'.

The above didn't remove the leading white space on several lines, so I made
the following addition:

f = open('file.txt')
t = [t for t in f.readlines() if t.strip()]
f.close()
s = [x.lstrip() for x in t]
print .join(s)

List comprehensions are still magic to me. How would I go about
incorporating lstrip() in the first list comprehension?

Many thanks,
J.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] how to iterate through a dictionary and assign list values?

2013-12-04 Thread Jared Nielsen
I want to create a dictionary, assign it keys, then iterate through a for
loop and assign the dictionary values from a list. I'm trying this, but
it's not working:

dictionary = {one, two, three}
list = [1,2,3]

for key in dictionary:
for value in list:
dictionary[key] = value

I get this error:
TypeError: 'set' object does not support item assignment

What am I doing wrong? Any help is greatly appreciated.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] how to only loop over first 'x' items of a list; writing a Twitter bot with tweepy

2013-11-25 Thread Jared Nielsen
Hi all,
Noob. For a beginner project I'm hacking together a Twitter bot with tweepy.

I've got one more or less functional, but I'm running into a problem when
making a list of followers by calling the Twitter api. I'm getting a 'Rate
limit exceeded' error, which, evidently is due to changes in the Twitter
api not allowing one to make more than a certain number of calls. I think
150 at a time. I have more than 150 followers in the account.

My code is:

for follower in tweepy.Cursor(api.followers).items():
follower_ids.append(follower.id)

My question is: how do I grab the first 'x' items in
tweepy.Cursor(api.followers)items(), say 15 or 20, without looping through
the entire list of items, which then gives me the error.

Thanks!


-- 
http://thehelloworldprogram.com
http://dototot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I need a good resource for python Django

2013-08-20 Thread Jared Nielsen
 Hi,

 Can anyone suggest me a good resource for python Django. I've gone through
 the official website of Django but it is of limited use to me. Any help on
 this would be highly appreciated.

I recommend Mike Hibbert's YouTube series on Django.

http://www.youtube.com/watch?v=oT1A1KKf0SIfeature=c4-overview-vllist=PLxxA5z-8B2xk4szCgFmgonNcCboyNneMD

It's the most comprehensive and easy to follow tutorial I've found.

Otherwise go to the Google django-users group and search. Someone asks this
question everyday.



-- 
http://jarednielsen.com
http://thehelloworldprogram.com
http://dototot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python as poetry

2013-01-03 Thread Jared Nielsen
I don't know if it's appropriate to post things like this on the list, but
I've learned a lot from this group and thought I'd share something I think
you all will enjoy:
http://www.thehelloworldprogram.com/videos/poetry-corner-red-wheelbarrow/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to split/partition a string on keywords?

2012-08-24 Thread Jared Nielsen
Thanks everyone. As I'm learning programming what I find most interesting
is that there's always more than one way to solve a problem.

I implemented eryksun's suggestion and used the replace() method.
But, playing around with it, what I discovered is that it won't store the
change.
For example, when the input text is, Ham and cheese or chicken and
waffles:

#!/usr/bin/python

text = raw_input(Enter text: )

print text.replace(and, \nand).replace(or, \nor)

I get:
Ham
and cheese
or chicken
and waffles.

But if I run the following:

#!/usr/bin/python

text = raw_input(Enter text: )

text.replace(and, \nand)
text.replace(or, \nor)

print text

I get the text as it was entered.
Is there a way to replace text in a string without splitting or
partitioning?

The bigger picture for this little project is a poetry machine, in which
a user enters some prose and the program chops it up into modern poetry.

So, this is a long shot naive noob question, but is there any way to count
syllables in words in a string? Or at least approximate this procedure?



On Thu, Aug 23, 2012 at 3:08 PM, aklei...@sonic.net wrote:

 This question seemed a good excercise so I banged out a little script
 (which worked) but latter I saw posts showing code that by using string
 method 'partition' provided a more elegant solution.
 I was previously unaware of this method.  My bible has been David M.
 Beazley's Python Essential Reference (3rdEd) in which this method is not
 mentioned (that I can see.)
 Should I switch bibles?
 (I often find myself wanting to hack in off line environments so
 something as old fashion as a book would be nice:-)

 Here's my script for what it's worth:

 #!/usr/bin/env python

 import sys

 usage = test0 separator
 Requires one parameter, the text to be used to separate the input which
 will be requested by the program.

 if len(sys.argv) != 2:
 print usage
 separator = sys.argv[1]

 def separate(string, separator):
 ret = []
 i = string.find(separator)
 l = len(separator)
 while i  0:
 ret.append(string[:i])
 ret.append(separator)
 string = string[i+l:]
 i = string.find(separator)
 ret.append(string)
 return ret

 def repart(string, separator):
 Does the same as separator but using string method 'partition'
 parts = string.partition(separator)
 if parts[0] == string:
 return (parts[0], )
 else:
 return parts[:-1] + repart(parts[-1], separator)

 input_str = raw_input(Enter text to split on '%s': %(separator, ))

 separated_array = separate(input_str, separator)
 for s in separated_array:
 print s
 parted_array = repart(input_str, separator)
 for s in parted_array:
 print s




   Hi all,
  I'm new to programming and Python.
  I want to write a script that takes a string input and breaks the string
  at
  keywords then outputs the pieces on separate lines.
  I'm not sure how to break the string, though.
  I looked through the docs and found split() and partition(), which come
  close.
  But split() doesn't retain the separator and partition() retains the
 white
  space and returns a 3-tuple which I'll have to figure out how to rejoin
  nor
  does it partition on subsequent instances of the separator.
 
  Here's the script in its basic form:
 
  #!/usr/bin/python
 
  text = raw_input(Enter text: )
  print You entered , text
 
  objects = text.partition(' and')
  print objects
 
  for object in objects:# Second Example
 
 print object
 
  For example, if I run this with the input:
  Ham and cheese omelette with hasbrowns and coffee.
  I get:
  Ham
   and
   cheese omelette with hashbrowns and coffee.
 
  Any help is greatly appreciated.
  ___
  Tutor maillist  -  Tutor@python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor