LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-19-2007, 01:07 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Super simple python questions: how do you do (!x) and '? x:y'


I can't believe I'm actually asking this question, but I can't seem to find an answer (granted, I haven't looked *that* hard).

How can I make this work the way I want it to?
Code:
foo = False
if (!foo):
  print "false"
... the interpreter chokes on !foo. Do I have to do something silly like (foo != True)?


edit:
While I'm asking, how about this:
Code:
maxLen = (maxLen > len(bar)) ? maxLen : len(bar)
Is there something similar in python, or do you have to do an if/else?

Last edited by BrianK; 09-19-2007 at 01:19 PM.
 
Old 09-19-2007, 01:39 PM   #2
Brydon
Member
 
Registered: Dec 2006
Location: Lancaster, Ontario
Distribution: Fedora 6-7, CentOS 5, Red Hat 9, Ubuntu
Posts: 36

Rep: Reputation: 15
You can try this!

Code:
foo = False
if (foo != True):
        print "false"
Hope it helps.

Scott
 
Old 09-19-2007, 02:16 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by BrianK View Post
...
Do I have to do something silly like (foo != True)?
...
Quote:
Originally Posted by Brydon View Post
You can try this!

Code:
foo = False
if (foo != True):
        print "false"
Hope it helps.

Scott
I already thought of that.
 
Old 09-19-2007, 02:29 PM   #4
rshaw
Senior Member
 
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,692

Rep: Reputation: 45
you have already assigned 'false' to 'foo'

Code:
foo = False
if foo:
  print "false"
 
Old 09-19-2007, 03:14 PM   #5
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by rshaw View Post
you have already assigned 'false' to 'foo'

Code:
foo = False
if foo:
  print "false"
that will not print out anything because foo is false.

The question is how to test for false? In C/C++, php, java, perl(?), etc, you can test for this by using the '!' logical operator (i.e. (!False == True) evaluates to True), but python doesn't seem to have that - at least not by itself... it appears you have to use '!='.

Is that correct?
Is there a way of doing this test without using "(foo != True)"?



... and just to clarify, in python, "False" is a keyword, "false" is not. Maybe I should change my example to print out "foo is false" rather than just "false"

Last edited by BrianK; 09-19-2007 at 03:19 PM.
 
Old 09-19-2007, 03:23 PM   #6
rshaw
Senior Member
 
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,692

Rep: Reputation: 45
really? it prints "False" on my rig.

Code:
if not foo:
   print "false"
although assigning something to a variable then testing that it's not there is a round about way to do it.
 
Old 09-19-2007, 03:57 PM   #7
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by rshaw View Post
really? it prints "False" on my rig.

Code:
if not foo:
   print "false"
although assigning something to a variable then testing that it's not there is a round about way to do it.
AH HA!! in your first post, you left out "not". That's what I was looking for. Thanks!

... and clearly, that's a round about way of doing things - the actual code in my program has more meaning, but this was the simplest example to post.

Thanks again!

If anyone knows a method for acheiving my second example, feel free to chime in.

Last edited by BrianK; 09-19-2007 at 04:00 PM.
 
Old 09-20-2007, 10:51 AM   #8
maroonbaboon
Senior Member
 
Registered: Aug 2003
Location: Sydney
Distribution: debian
Posts: 1,495

Rep: Reputation: 48
I think (at least initially) the python design philosophy was to keep the language simple by only having only one way to express each construct. So if/then/else/elif was supposed to be enough without ?/: and switch statements. In this situation you can also (rather artificially) use a dictionary:

maxLen = {True: maxLen, False: len(bar)}[maxLen > len(bar)]

although this trick is more useful for making switch type statements. Of course with your example the simplest form is

maxLen = max(maxLen, len(bar))

although I don't think that's what you were asking.
 
Old 09-20-2007, 12:23 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by BrianK View Post
While I'm asking, how about this:
Code:
maxLen = (maxLen > len(bar)) ? maxLen : len(bar)
Is there something similar in python, or do you have to do an if/else?
Code:
if maxLen < len(bar): maxLen = len(bar)
 
Old 09-20-2007, 12:50 PM   #10
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by maroonbaboon View Post
I think (at least initially) the python design philosophy was to keep the language simple by only having only one way to express each construct. So if/then/else/elif was supposed to be enough without ?/: and switch statements. In this situation you can also (rather artificially) use a dictionary:

maxLen = {True: maxLen, False: len(bar)}[maxLen > len(bar)]

although this trick is more useful for making switch type statements. Of course with your example the simplest form is

maxLen = max(maxLen, len(bar))

although I don't think that's what you were asking.
ahh.. interesting re: dictionary.

... and yes, max(a,b) that's not what I'm asking - just trying to put up a generic example. Of course, I would exploit the functionality of such an operation if it existed.

Thanks for the replies.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extremely simple Python question Travis86 Programming 3 01-02-2007 01:50 PM
Simple Python Question peter_89 Programming 3 05-10-2006 12:13 PM
Xfce! A few simple questions for a super-noob Taigrr Linux - Software 3 04-04-2006 06:16 PM
how do I do this in Python? (simple sh script) johnMG Programming 6 12-22-2004 11:05 PM
simple python function question Tyir Programming 2 03-28-2004 11:53 PM


All times are GMT -5. The time now is 03:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration