[Tutor] Python Regex References

2013-01-08 Thread Devyn Collier Johnson
I am trying to convert the SED code below to Python3.3. How do I
reference the value of the first and second wildcards so that I can put
the variable values in the "replace" portion of the code?

SED (this works):
export DATA=$(echo "$DATA" | sed -r -e "s|^ $|\1(>=>)\2|gI")

Python3.3 (attempted):
DATA = re.sub('^$', '\1(>=>)\2',
DATA, flags=re.I)


Thank you,

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


[Tutor] (no subject)

2013-01-08 Thread Dustin Guerri
Hi there,

I'm completely new to Python and to programming.  I have the Python
Launcher (v2.7.3) app installed on my system, and I'm running Mac OS X
Mountain Lion.  Does this mean I have Python itself installed ?

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


[Tutor] Python Regex References

2013-01-08 Thread Devyn Collier Johnson
I am trying to convert the SED code below to Python3.3. How do I
reference the value of the first and second wildcards so that I can put
the variable values in the "replace" portion of the code?

SED (this works):
export DATA=$(echo "$DATA" | sed -r -e "s|^ $|\1(>=>)\2|gI")

Python3.3 (attempted):
DATA = re.sub('^$', '\1(>=>)\2',
DATA, flags=re.I)



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


[Tutor] help

2013-01-08 Thread Mike G
Hi Randy

> I am an older newbie teaching myself Python programming.
>

Me too :)

> My problem is I hear no system bell; the enter doesn't respond by quitting 
> the program;
> The problem with the program code the enter key hasn't worked in earlier 
> programs.
>
> I appreciate any advice I may recieve with this coding glitch.
>

I copied the code into a blank .py file and ran it from cmd in Windows
XP x86 using Python 273, it worked fine - including the beep. As well,
hitting "Enter" exited the program.

It sounds like (no pun intended) it may be how you're running the
program. I would use a py file and run it using cmd - holler if you
need help, you may if the path to Python isn't good to go.

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


Re: [Tutor] writing effective unittests

2013-01-08 Thread Japhy Bartlett
The general idea is to write tests that use your code in realistic ways and
check the results.  So if you have a function that takes an input and
returns a result, you write a test that passes that function an input
checks the result.  If some inputs should make it error, you write a test
that checks that it errors.

In web programming, a common thing is to make a web request with different
variations of GET / PUT params, then check that it returns the right status
code, or that the result is valid JSON, etc.  Basically, try to simulate
all the things a real world user would be able to use, and test that your
code does what you intend for it to do.

TDD is a good principle but usually seems a little too pedantic for real
world programming.  Where tests (in my experience) get really useful is in
making sure that a new change hasn't unexpectedly broken something already
written.


On Thu, Jan 3, 2013 at 2:31 PM, Tino Dai  wrote:

>
>
> I think what I need is a conceptual shift: how do python programmers use
>> unittests?
>>
>>
> Here at the Library, we use unit test to test the cases that have many
> known inputs. For example, some of the data could come in "foo bar baz",
> and some others could come in as "foo, bar, baz", and others could come in
> "foo; bar; baz". As we code for each one of these cases, we write tests to
> make sure that changes that we make don't break any of our former code. I
> think that TDD is a good thing to strive for, but in most cases is an ideal
> that can't be reached in most cases.
>
> Hope that helps,
> Tino
>
>
> ___
> 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


[Tutor] modifying global within function without declaring global

2013-01-08 Thread Nathaniel Huston
As far as I understood, this shouldn't be possible. this is python 2.7


weirdness is in deal() at the end





import random

class card:
    def __init__(self, suit, rank, name):
    self.suit = suit
    self.rank = rank
    self.name = name
    

def makedeck():
    #creates an ordered deck of card objects
    #ranks are clumped (four 2's, four 3's etc)
    
    suits = ['Spades', 'Diamonds', 'Clubs', 'Hearts']
    ranks = []
    for x in range(2, 15):
    ranks.append(x)

    names = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 
'Ten', 'Jack', 'Queen', 'King', 'Ace']

    stack = []

    for rank in ranks:
    for suit in suits:
    name = names[rank - 2]
    stack.append(card(suit, rank, name))

    return stack


def deal(quantity):
    
    hand = []
    for cards in range(0, quantity):
    hand.append(deck.pop())
    return hand

#

#if you run this and do:

deck = makedeck()
hand = deal(5)

#and then finally

len(deck)

#we find that the global deck has been modified within the deal() function 
without including

global deck

#within the deal() function

#it seems insane to me that it's possible to alter global variables within 
functions without declaring global or passing the variable in as an argument.___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Question

2013-01-08 Thread Dylan Kaufman
Greetings,

I take Computer Science in school and for a Python program, I have:

from winsound import Beep

Beep(196, 1500)#G
Beep(262, 270)#C
Beep(196, 200)#G
Beep(165, 300)#E
Beep(175, 200)#F
Beep(196, 200)#G
Beep(262, 550)#C
Beep(196, 200)#G
Beep(208, 150)#G#
Beep(196, 200)#G
Beep(175, 200)#F
Beep(165, 200)#E
Beep(175, 200)#F
Beep(147, 200)#Low D
Beep(131, 750)#Low C
Beep(131, 150)#Low C
Beep(131, 350)#Low C
Beep(131, 150)#Low C
Beep(262, 350)#C
Beep(262, 150)#C
Beep(247, 350)#B
Beep(247, 150)#B
Beep(220, 350)#A
Beep(220, 150)#A
Beep(294, 200)#D
Beep(294, 200)#D
Beep(294, 200)#D
Beep(294, 200)#D
Beep(247, 200)#B
Beep(220, 200)#A
Beep(196, 550)#G
Beep(196, 150)#G
Beep(220, 200)#A
Beep(220, 200)#A
Beep(220, 200)#A
Beep(220, 200)#A
Beep(220, 200)#A
Beep(247, 200)#B
Beep(262, 300)#C
Beep(196, 150)#G
Beep(165, 300)#E
Beep(196, 150)#G
Beep(165, 200)#E
Beep(165, 200)#E

When I run it at school it works great, but at home when I try to run it,
it says:

Traceback (most recent call last):
  File "/Volumes/PICKLES/School/MyClass/Comp. Sci/Chapter 02
Stuff/program2_cep.py", line 5, in 
from winsound import Beep
ImportError: No module named 'winsound'

Could you tell why this is happening  and how I can make my program work?
-- 
--Dylan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] got stuck in equation

2013-01-08 Thread usama zohad

my name is usama khan . . i am the student of civil engeering and we got 
assignment to make a project program on flexible pavement design using python.i 
know very litle about programing. still learning from tutorials. u can call me 
a beginner.now i need to solve this equation so that i can put this in python 
but the formula of design is very complext
Formula is log(W18) = (Z)(S)+9.36log(SN+1) 
-2.0+(log(dpsi/(4.5-1.5))(/(.40+1094/(SN+1)^2.5)+2.32log(Mr)-8.07
every thing is constant except this SN. . i want to seperate SN like SN= rest 
of the stuff. how can i seprate it because manualy its impossible to take SN 
out.
so plz help mend  kindly tell me the solution that is easiest in python as am 
beginner.   ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2013-01-08 Thread Alan Gauld

On 16/12/12 23:09, Dustin Guerri wrote:

Hi there,

I'm completely new to Python and to programming.  I have the Python
Launcher (v2.7.3) app installed on my system, and I'm running Mac OS X
Mountain Lion.  Does this mean I have Python itself installed ?


MacOS uses Python so you will undoubtedly have it onboard.
It may not have all the features enabled (eg GUI support) so you may 
want to download the latest Mac build too (don't delete the standard 
install though you could break some tools on MacOS that use it!)


Try launching it and see if you get a Python prompt:

>>>

coming up

If so check it works with the classic

>>> print ('hello world')

Also take note of the version in the banner display above the prompt.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


[Tutor] flushed queue

2013-01-08 Thread Alan Gauld
I just finished flushing the moderation queue. It was full of spam over 
the holidays but there were some genuine messages there too. You'll no 
doubt see the older dates.


Apologies for the delayed response in dealing with these.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] modifying global within function without declaring global

2013-01-08 Thread Alan Gauld

On 05/01/13 01:27, Nathaniel Huston wrote:


def deal(quantity):

 hand = []
 for cards in range(0, quantity):
 hand.append(deck.pop())
 return hand


> #we find that the global deck has been modified within the deal()
> function without including
>
> global deck
>
> #within the deal() function

Notice deal() modifies the contents of deck but not deck itself - it 
still points to the same list object.


You only need to use global if you are changing the value. Since you are 
only modifying the contents you don't need the global statement.


You have to think in terms of objects and names as references to the 
objects. If you are changing the object that the name refers to you need 
to use global. If you are only changing the content of the object then 
there is no need for global. The object here is the list.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] (no subject)

2013-01-08 Thread Oscar Benjamin
On 16 December 2012 23:09, Dustin Guerri  wrote:
> Hi there,
>
> I'm completely new to Python and to programming.  I have the Python Launcher
> (v2.7.3) app installed on my system, and I'm running Mac OS X Mountain Lion.
> Does this mean I have Python itself installed ?

OSX ships with a Python installation, so every Mac has Python.

I guess you mean that you have installed a newer Python installation?
Try creating a plain text file called hello.py and putting the
following text in it:

print('hello world')
raw_input('Press any key to continue')

If you can run that program with the launcher then you're in business.


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


[Tutor] Robot Radio program

2013-01-08 Thread Kirk Bailey
Simulates an old time radio station. Place program files in the numbered 
folders, station identifications in the ID folder, commercials in the 
com folder. run from the command line.


directories under the program are: ID, Com, 1,2,3,4,5... ID contains 
station identifications. COM is commercial recordings. 1,2,3,4,5... each 
numbered directory contains mp3 files of a particular show.


#!c:\Python25\pythonw.exe
#
from ctypes import *# Don't change any of these
import os, time, glob, random# they are all needed
import cgitb; cgitb.enable()# for operations or problem handling
#
###
# Robot Radio WHEN V:1.0.0 (C)2012, 2013 Kirk D Bailey All rights 
reserved. #
# Visit our website and jawbone with us on the list! 
http://www.RobotRadio.info#

###
#
# there are 2 directories under this one- com and id. There are also 
numbered folders which
# contain files of old broadcasts. The system only referrs to number 
name directories to play programs
# but is taken by the hand to ID or com to play station identifications 
or commercials respectively.

#
# To add a broadcast group of 'programs', just create a new numbered 
directory and load the program
# files into it- and that's it, the program adapts to it. DO NOT add any 
directories EXCEPT numbered
# broadcast library file directories. The only 2 exceptions are com and 
ID. com contains commercials.
# ID contains station identification blip, anywhere from 5 to 30 seconds 
long. You can make your own mp3

# files and place them here.
#
winmm = windll.winmm# a shorthand way to access a function.
#
def mciSend(s):# we access a dlll in the operating system to 
play mp3 files.

   i=winmm.mciSendStringA(s,0,0,0)
   if i<>0:
  print "Error %d in mciSendString %s" % ( i, s )
#
def playMP3(mp3Name):# and we arap it in code to make using it 
really easy.

   mciSend("Close All")
   mciSend("Open \"%s\" Type MPEGVideo Alias theMP3" % mp3Name)
   mciSend("Play theMP3 Wait")
   mciSend("Close theMP3")
#
def RandomFile():# and this lets us pick a ramdom file in the 
current directory- whatever that is.
files=glob.glob("*.mp3") # files is a list of the 
CURRENT

file=random.choice(files)# directories contents
return file# and return the 
filename to be played

#
os.chdir("./RobotRadio_WHEN/")# starts in cgi-bin, move 
to application's
dirlist=glob.glob("*")# WE WILL NEED THIS LATER 
IN RandomDir(); once is enough

#
def RandomDir():
dir=str(1+int(random.random()*(len(dirlist)-2)))
return dir
#
COUNT=8# this is how many times to do the 
loop, then quit.
limit=COUNT+1# this masages a offset to helpo 
display the loop count
print "- - - - - - - - - - - - - - BEGIN Broadcast Day - - - - - - - - - 
- - - - - - - - -"

#
while COUNT: # this sets the number of loops. 
Get creative to stop it.

# ok, let's do the loop.
print "Loop #", limit-COUNT# this lets us display the loop 
count
os.chdir("./ID")# go to the station identification 
folder

mp3file=RandomFile()#
print "Station Identification file:",mp3file," AT: ", 
time.strftime("%a, %d %b %Y %I:%M %p", time.localtime())

playMP3(mp3file)# and pick one at random and play it.
os.chdir("..")# go back to the home directory 
for this program

playMP3("./ID/NBCtones.mp3")# play the top of the hour NBC tones
print "NBCtones.mp3 - play the NBC tri-tone" # we terimmed it off 
the end of another file, nice and clear and crisp.

time.sleep(2)# a breif pause between file plays
os.chdir("./com")# now back go home, then on to the 
commercial folder

mp3file=RandomFile()# pick a commercial
print "commercial: ",mp3file," AT: ", time.strftime("%a, %d %b %Y 
%I:%M %p", time.localtime()) # show me the commercial

playMP3(mp3file)# and play it.
time.sleep(2)# another pause- so there is a 
breif hesitation between one and next.
os.chdir("..")# this takes us back to the home 
directory of the application

newdir=RandomDir()# pick a dir, any dir,
os.chdir(newdir)# and go there.
mp3file=RandomFile()# pick a random file from this 
directory

print "Program=", os.getcwd()+"\\"+ mp3file # show us what it is,
print "AT:"+ time.strftime("%a, %d %b %Y %I:%M %p", 
time.localtime()) # And when,

playMP3(mp3file) # and play it.
os.chdir("../com")# now back go home, then on 

Re: [Tutor] got stuck in equation

2013-01-08 Thread Oscar Benjamin
On 1 January 2013 15:47, usama zohad  wrote:
> my name is usama khan . . i am the student of civil engeering and we got
> assignment to make a project program on flexible pavement design using
> python.i know very litle about programing. still learning from tutorials. u
> can call me a beginner.now i need to solve this equation so that i can put
> this in python but the formula of design is very complext
>
> Formula is
> log(W18) = (Z)(S)+9.36log(SN+1)
> -2.0+(log(dpsi/(4.5-1.5))(/(.40+1094/(SN+1)^2.5)+2.32log(Mr)-8.07
>
> every thing is constant except this SN. . i want to seperate SN like SN=
> rest of the stuff. how can i seprate it because manualy its impossible to
> take SN out.
>
> so plz help me
> nd  kindly tell me the solution that is easiest in python as am beginner.

Didn't you post this exact question very recently? You should add
another post to the original thread if you are not satisfied with
answers you got there rather than starting a new one.

In any case your problem is not really a Python problem. You could use
Python to solve it, but then you can probably get the answer you want
just by putting your equation into something like Wolfram Alpha
without any programming:
http://www.wolframalpha.com/


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


Re: [Tutor] got stuck in equation

2013-01-08 Thread Oscar Benjamin
On 9 January 2013 01:15, Oscar Benjamin  wrote:
> On 1 January 2013 15:47, usama zohad  wrote:
>> my name is usama khan . . i am the student of civil engeering and we got
>> assignment to make a project program on flexible pavement design using
>> python.i know very litle about programing. still learning from tutorials. u
>> can call me a beginner.now i need to solve this equation so that i can put
>> this in python but the formula of design is very complext
>>
>> Formula is
>> log(W18) = (Z)(S)+9.36log(SN+1)
>> -2.0+(log(dpsi/(4.5-1.5))(/(.40+1094/(SN+1)^2.5)+2.32log(Mr)-8.07
>>
>> every thing is constant except this SN. . i want to seperate SN like SN=
>> rest of the stuff. how can i seprate it because manualy its impossible to
>> take SN out.
>>
>> so plz help me
>> nd  kindly tell me the solution that is easiest in python as am beginner.
>
> Didn't you post this exact question very recently? You should add
> another post to the original thread if you are not satisfied with
> answers you got there rather than starting a new one.

I'm sorry, I just saw Alan's message about flushing the queue. I see
now that you actually sent this message 8 days ago. My bad...


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


Re: [Tutor] Python Question

2013-01-08 Thread Oscar Benjamin
On 7 January 2013 23:31, Dylan Kaufman  wrote:
> Greetings,
>
> I take Computer Science in school and for a Python program, I have:
>
> from winsound import Beep
>
[SNIP]
>
> When I run it at school it works great, but at home when I try to run it, it
> says:
>
> Traceback (most recent call last):
>   File "/Volumes/PICKLES/School/MyClass/Comp. Sci/Chapter 02
> Stuff/program2_cep.py", line 5, in 
> from winsound import Beep
> ImportError: No module named 'winsound'

Judging from the path "/Volumes/PICKLES/..." I'd say you're not
running Windows and...

>
> Could you tell why this is happening  and how I can make my program work?

winsound only works on Windows (it quietly says "Platforms: Windows"
at the top of this page):
http://docs.python.org/2/library/winsound.html


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


Re: [Tutor] modifying global within function without declaring global

2013-01-08 Thread Oscar Benjamin
On 9 January 2013 01:07, Alan Gauld  wrote:
> On 05/01/13 01:27, Nathaniel Huston wrote:
>
>> def deal(quantity):
>>
>>  hand = []
>>  for cards in range(0, quantity):
>>  hand.append(deck.pop())
>>  return hand
>
>
>> #we find that the global deck has been modified within the deal()
>> function without including
>>
>> global deck
>>
>> #within the deal() function
>
> Notice deal() modifies the contents of deck but not deck itself - it still
> points to the same list object.
>
> You only need to use global if you are changing the value. Since you are
> only modifying the contents you don't need the global statement.

I agree entirely with what Alan said but have a slightly different way
of thinking about it: you only need the global statement if you want
to assign directly to a name (and have the assignment apply globally).
Examples:

x = some_obj()

def some_function():
x = b# direct assignment (global statement needed)

def some_other_function():
x.e = c# attribute assignment (global not needed)
x[f] = d# item assignment (global not needed)
x.mutating_func(g) # method call (global not needed)

The only one of the above statements that is affected by a "global x"
statement is the first. The other three calls will all modify x in
place regardless of any global statement. A global statement is needed
when you want to assign directly to a variable name and have the
assignment apply in an outer scope as is the case with the "x = b"
line above. In any other situation the global statement does nothing.


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


Re: [Tutor] modifying global within function without declaring global

2013-01-08 Thread Mitya Sirenef

On Tue 08 Jan 2013 08:07:57 PM EST, Alan Gauld wrote:

On 05/01/13 01:27, Nathaniel Huston wrote:


def deal(quantity):

 hand = []
 for cards in range(0, quantity):
 hand.append(deck.pop())
 return hand


> #we find that the global deck has been modified within the deal()
> function without including
>
> global deck
>
> #within the deal() function

Notice deal() modifies the contents of deck but not deck itself - it
still points to the same list object.

You only need to use global if you are changing the value. Since you
are only modifying the contents you don't need the global statement.

You have to think in terms of objects and names as references to the
objects. If you are changing the object that the name refers to you
need to use global. If you are only changing the content of the object
then there is no need for global. The object here is the list.





Actually, modifying a global name is OK, it is not OK to rebind it, 
e.g.:


# OK:
l=[]
def f(): l.append(1)

# won't rebind global:
def b(): l=[2]

After you run both, you'll see l is [1]


This is a very important difference, if it helps, it's similar to this
real-world example:

Let's say you're a student in a school and
you have a teacher who is standing before a blackboard and
writing something. After he gets to the end of the blackboard,
he picks up a phone and says "supply room? Can you please
order a new blackboard and deliver to me, I've run out of
space". Then a few minutes go by and a delivery person
shows up, with a spanking new black board, installs it
and then leaves.

The teacher goes back to writing down his formulas but
soon runs out of space again and has to call for a new
board again.

Someone raises a hand and asks him why he  does not
erase the board, instead. He looks puzzled and replies
that he thought it's the same thing, whether you erase
the board or make a new one.

In Python, of course, both operations are equally fast
(more or less), so it's easy to forget about the difference.

# order new board
board = []
# OR
board = list()

# erase the existing board
board[:] = []


So, back to local and global bindings... If the board is
not a global name, you CAN modify it, but you can NOT
order a new board and have it available globally --
unless you use the 'global' keyword.

Integers, floats and strings are immutable so you can't
modify them anyway, when you change them, you can
only rebind:

# global is NOT changed!
x = 5
def f(): x = 6


Hope this helps,  -m


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] using 'and ' and 'or' with integers

2013-01-08 Thread ken brockman
I was looking through some lab material from a computer course offered at UC 
Berkeley and came across some examples in the form of questions on a test about 
python.
1 and 2 and 3
 answer 3
I've goggled it till i was red in the fingers, but to no avail.. Could someone 
be kind enuff to direct me to some docs that explain this?? I've no clue what 
the hell is going on here..
Thanks much for any help you may supply.___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HELP- Regarding working with python

2013-01-08 Thread Gayathri S
Hi..
I would like to use Principal component analysis independent
component analysis in python. Wanna know whether it will support this
efficiently or not?

On Wed, Jan 2, 2013 at 4:59 PM, Alan Gauld wrote:

> On 02/01/13 07:20, Gayathri S wrote:
>
>> Hi..
>>   I am using python 2.7 and scikit-learn for machine learning.
>> And OS is Windows 7. Wanna know how to import our own data sets  in
>> scikit-learn?
>>
>
> Further to my last mail there is a gmane group
>
> gmane.comp.python.scikit-learn
>
> I'd try looking there, or wherever it is sourced originally.
>
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 




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


Re: [Tutor] using 'and ' and 'or' with integers

2013-01-08 Thread Andreas Perstinger

[Please don't send HTML to this list. Just use plain text]

On 09.01.2013 07:56, ken brockman wrote:

I was looking through some lab material from a computer course
offered at UC Berkeley and came across some examples in the form of
questions on a test about python. 1 and 2 and 3 answer 3 I've goggled
it till i was red in the fingers, but to no avail.. Could someone be
kind enuff to direct me to some docs that explain this??


Language Reference - 6.10 Boolean operations (for Python 3.3; it's 5.10 
for Python 2.7):

http://docs.python.org/3.3/reference/expressions.html#boolean-operations

"The expression x and y first evaluates x; if x is false, its value is 
returned; otherwise, y is evaluated and the resulting value is returned."


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


Re: [Tutor] using 'and ' and 'or' with integers

2013-01-08 Thread Mitya Sirenef

On Wed 09 Jan 2013 01:56:20 AM EST, ken brockman wrote:

I was looking through some lab material from a computer course offered
at UC Berkeley and came across some examples in the form of questions
on a test about python.
1 and 2 and 3
 answer 3
I've goggled it till i was red in the fingers, but to no avail.. Could
someone be kind enuff to direct me to some docs that explain this??
I've no clue what the hell is going on here..
Thanks much for any help you may supply.


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



It returns last evaluated value. If you have '1 and 2', 1
is 'truthy', so it needs to evaluate 2, and then 2 is returned.

With '0 and 2', 0 is falsy, so it does not need to evaluate
past it and returns 0.

With '0 or 2', it needs to evaluate 2 and returns its value.

This is particularly useful to assign only when a value is
falsy:

default_x = 5

def f(x):
 x = x or default_x


So if x is provided, it is used, but if x is, let's say, None,
default_x will be used.


HTH,  - mitya




--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor