EDIT: My first "real" python program: pycalc

Started by
18 comments, last by Fruny 17 years, 7 months ago
You can print more than 1 line using only 1 call of print like in this example:
print """First line.
Second line."""
Advertisement
I just wanted to post I appricate the example on using dict and lamba functions together. I can see major use for this in python game programming changing AI states.
Quote:Original post by tldalton1622
hey im learning python too. send me a pm if you want to exchange IM screen names. We can bounce ideas off each other if you'd like. everyone else is doing the whole c++ thing.


Which handle do you want?
Hey I am learning Python too, maybe we could start a Python support group :P
Quote:Original post by mikeman
Although I think this might be a little advanced for you right now, it never hurts to check out what the language can really do. You can substitute the long 'if' chains:

*** Source Snippet Removed ***

with:

*** Source Snippet Removed ***

Basically, we create a dictionary that stores lambdas(think of them as unnamed functions defined on-the-fly). We can then access the desireable operation using the string "operation" as key.


This sacrifices some more error checking, but you can also make use of Python's runtime evaluation, like so (which also has an advantage from a UI perspective, because you don't expect the user to type out full words and spell them correctly ;) ):

answer = eval('lambda x, y: x %s y' % operation)(num1, num2)


That is, substitute the operation string (which should now be one of the appropriate symbols) into a longer string (which just happens to be Python code to create one of those lambdas), interpret the string as a Python expression (not a statement! You couldn't eval() an if-construct or print statement for example; use 'exec' for that, but you won't get a return value), so as to create the appropriate lambda function, then call the function with the arguments. [smile]

Of course, the whole exercise is more or less moot because the Python command line is already a calculator:

Python 2.3.2 (#49, Oct  2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> 6 * 954>>> # Not 42, apologies to DNA fans


Of course, because it's interpreting your input as Python code rather than just input to a calculator, it's quite a bit more powerful. So the real exercise in making a calculator program is *restricting* things ;) Oh, and modelling the interface that a real calculator provides. Try playing around with the Windows Calculator, or a real one, for example.

By the way, you can get access to the "command line calculator" from within a program by simply using input() instead of raw_input(); input() should basically be equivalent to eval(raw_input()).
Quote:Original post by Zahlman
Quote:Original post by mikeman
Although I think this might be a little advanced for you right now, it never hurts to check out what the language can really do. You can substitute the long 'if' chains:

*** Source Snippet Removed ***

with:

*** Source Snippet Removed ***

Basically, we create a dictionary that stores lambdas(think of them as unnamed functions defined on-the-fly). We can then access the desireable operation using the string "operation" as key.


This sacrifices some more error checking, but you can also make use of Python's runtime evaluation, like so (which also has an advantage from a UI perspective, because you don't expect the user to type out full words and spell them correctly ;) ):

answer = eval('lambda x, y: x %s y' % operation)(num1, num2)




Yeah, I didn't want to mention eval() since that makes things too easy - I understand he's doing this to learn. Heck, with eval you don't even need to create lambdas, you could just do:

answer = eval('num1 %s num2' % operation)

wxPython. Get busy.
There's no need to bother with lambdas: just use the operator module:

import operatoroperations = {  'addition'       : operator.add,  'subtraction'    : operator.sub,  'multiplication' : operator.mul,  'division'       : operator.truediv     # bypasses integer division issues}answer = operations[operation](num1,num2)


Quote:Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32


2.3? How quaint. [grin]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
There's no need to bother with lambdas: just use the operator module:

import operatoroperations = {  'addition'       : operator.add,  'subtraction'    : operator.sub,  'multiplication' : operator.mul,  'division'       : operator.truediv     # bypasses integer division issues}answer = operations[operation](num1,num2)


Yeah, and you can also get at it with e.g. float.__add__ etc. (although that restricts the input type). And they say TMTOWTDI is for Perl hackers... [smile]

Quote:
Quote:Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32


2.3? How quaint. [grin]


Yeah, so I haven't updated; sue me. I'm probably running a positively ancient version of Firefox, too. [smile]
Original post by Zahlman
Yeah, and you can also get at it with e.g. float.__add__ etc. (although that restricts the input type).

Ah, but using operator is the obvious way... assuming you know the module is there. Given that it is a built-in module, I suspect it might give better results than a lambda, performance-wise.

Quote: And they say TMTOWTDI is for Perl hackers... [smile]

Quote:From the Zen of Python
There should be one-- and preferably only one --obvious way to do it.


import this [smile]

Quote:Yeah, so I haven't updated; sue me. I'm probably running a positively ancient version of Firefox, too. [smile]


You should hear from my lawyer shortly.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement