r/Python • • Oct 27 '10

Learning Python and can't see the difference?

I started with self teaching myself HTML about 9 years ago. Since then I have moved on to taking a basic C++ class. I since have self taught myself further in C++ with some Java. I am working on teaching myself python at the moment, maybe it is late, or maybe I am just that un-educated in programming, but I can't wrap my head around why I get such different results from this short code. Could someone kindly explain, or point me to a resource that could better explain why? I am currently using www.diveintopython.org and openbookproject.net//thinkCSpy/index.html

prefixes = "JKLMNOPQ" suffix = "ack"

for char in prefixes: if char == "O" or "Q": print char + 'u' + suffix else: print char + suffix

Juack Kuack Luack Muack Nuack Ouack Puack Quack

for char in prefixes: if char == "O": print char + 'u' + suffix elif char == "Q": print char + 'u' + suffix else: print char + suffix

Jack Kack Lack Mack Nack Ouack Pack Quack

0 Upvotes

22 comments sorted by

24

u/norwegianwood Oct 27 '10

This line,

if char == "O" or "Q":

should be,

if char == "O" or char == "Q":

Your version could be rewritten with parentheses as,

if (char == "O") or "Q":

and you will see clearly now that the condition as a whole is always True because the value Q is always equivalent to True.

5

u/fokkre Oct 27 '10

Awesome! Don't know why I couldn't see that, guess I need some caffeine. =)

5

u/mehum Oct 27 '10

Or (sorry to suggest it) sleep.

1

u/fokkre Oct 28 '10

NOT SLEEP NOOOOOOOOOOOOOOOOOOOOOOO!!!! well maybe.... =)

22

u/usernamenottaken Oct 27 '10

In these situations I find myself using this a lot: if char in ["O", "Q"]:

8

u/pingveno pinch of this, pinch of that Oct 27 '10

(...) is better because tuples creation is lighter than list creation. It's not a big deal by any means, but still...

4

u/[deleted] Oct 27 '10

How about strings? Is

if char in 'OQ':

better or worse?

2

u/Dave9876 Oct 27 '10

Might be faster (haven't tested it), but ugly and could cause confusion.

5

u/pingveno pinch of this, pinch of that Oct 27 '10 edited Oct 27 '10

It's probably faster because in ("O", "Q") creates two strings, creates a tuple, uses __contains__ on the tuple, and uses __eq__ on both of the strings.

in 'OQ'

creates the string, then uses __contains__ on the string. The actual algorithm that implements __contains__ is ultra fast so no worries there.

That said, don't worry about the difference. The difference is usually tiny compared to the rest of the program.

2

u/Chun Oct 27 '10 edited Oct 27 '10

I wouldn't say it's ugly; I mean, conceptually, in python a string is just a sequence of characters. So it fits the purpose perfectly.

Also I just ran a quick benchmark (strings vs tuples) and the difference is there, but fairly negligible.

1

u/darjus Oct 27 '10

have you tried?

in set(('O', 'Q'))

For small sets it might be around the same, but for bigger ones it should make a big performance difference.

1

u/itsmememe Oct 28 '10

as it is a constant, frozenset(['O','Q']) should be preferred

1

u/[deleted] Nov 02 '10

I find it perfectly fine. That char == 'O' or char == 'Q' is clearly low level C-style stuff.

3

u/[deleted] Oct 27 '10

It doesn't matter. When using it with in a list whose members are all constants (at the bytecode level) is turned into a tuple.

1

u/Tetha Oct 29 '10

And then you go another step further and write: if char in charsFollowedByU:

5

u/sanktuaire Oct 27 '10

A more pythonic way of doing it would be: if char in ("O","Q"):

Writing: if char in "OQ": would work but isn't very readable.

5

u/hippocampe Oct 27 '10

It is very readable.

2

u/jigs_up Oct 27 '10
if (char == "O") or "Q":

wouldn't that just return true every time because Q evaluates to True [not None] ?

5

u/torrible Oct 27 '10

Yep. That's why norwegianwood said "the condition as a whole is always True" and that's why the output words in the first test all had 'u' inserted.

2

u/fokkre Oct 27 '10

Sorry for the formatting, this is my first post on reddit

1

u/Samus_ Oct 27 '10

add four spaces before each line of code and it'll print

like this

there's some cheatsheets on the help/faq

1

u/jimauthors Oct 27 '10

You probably will get a better answer/explanation/guidance if you post this in stackoverflow.com