-1

Possible Duplicate:
Boolean in Python

I cant seem to find references to Boolean variables in Python 3. Are there boolean variables in python?

So for example, how would I do this in python:

bool checker;
while(checker = false)
{
//do this
}

...

Thanks

Community
  • 1
  • 1
fdama
  • 194
  • 3
  • 6
  • 15
  • 8
    Before the downvotes start to rain down, read this: http://docs.python.org/py3k/library/stdtypes.html – Martijn Pieters Sep 11 '12 at 20:17
  • 3
    And I could mention this as well: http://docs.python.org/py3k/reference/datamodel.html#the-standard-type-hierarchy – Martijn Pieters Sep 11 '12 at 20:19
  • @MartijnPieters -- I'm pretty sure you've managed to save OP a whole bunch of downvotes just by starting your first comment with "Before the downvotes start to rain down". – mgilson Sep 11 '12 at 20:21

2 Answers2

4

There is no such thing as "boolean variable": variables in Python do not have types as Python is a dynamically typed language.

However, there are "boolean values". The [only] two such boolean values are named by True and False. The REPL shows:

>>> True.__class__
<class 'bool'>
>>> False.__class
<class 'bool'>

However, it is often not needed to == True or == False and is often considered poor practice. In addition, = is always an assignment operator in Python (and C and Java) and is not an equality operator.

  • "variables in Python do not have types". Is this new with Python 3? In 2.7 I can write `foo = False; print type(foo)` and get back ``, which leads me to believe that foo has the type bool. – Kevin Sep 11 '12 at 20:29
  • @Kevin in Python 3 this seems to return `` rather than type... but `type(True) is bool` still holds – Andy Hayden Sep 11 '12 at 20:35
  • 2
    @Kevin The *variable* does *not* have a type. The **value** which is the result of evaluating the *variable*, does have a type. (Strongly-typed languages like C, Java, and Haskell attribute types - often with a declaration like `int i` - to variables/bindings and thus limit the values a particular variable can name.) –  Sep 11 '12 at 20:40
  • 1
    @pst: Python is strongly typed. – Wooble Sep 11 '12 at 22:54
  • 1
    @Wooble. It is. However, [Strong-typing](http://en.wikipedia.org/wiki/Strong_typing) (with is the opposite of Weak-typing) is *not related* to [Static-typing](http://en.wikipedia.org/wiki/Type_system#Static_typing) (which is the opposite of Dynamic-typing). *Python is Strongly-typed **and** Dynamically-typed*. –  Sep 11 '12 at 23:10
  • @Wooble Actually, I suppose Strong-typing *could* imply Static-typing in *some* contexts as Static-typing can "specify restrictions on operations involving different data types" (adapted from previous link); however, to do so in the case of Python would be incorrect as Static-typing implies much more. –  Sep 11 '12 at 23:17
  • @Wooble Oh, I see. I wrote that comment wrong .. it should have been "Statically-typed" way back up there in comment #3. Weak Sauce. –  Sep 11 '12 at 23:21
2

First of all, you don't declare variable types in python. With that in mind, your example is pretty easily translated:

checker = False
while not checker:
    # do this
Wilduck
  • 13,822
  • 10
  • 58
  • 90