MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1&1

Python, Ruby, Perl: True and False

Xah Lee, , …,

Python

Following evaluates to false:

# -*- coding: utf-8 -*-
# python

if True:                        # y
    print "y"
else:
    print "n"

if 3:                           # y
    print "y"
else:
    print "n"

if "abc":                         # y
    print "y"
else:
    print "n"

if False:                       # n
    print "y"
else:
    print "n"

if None:                        # n
    print "y"
else:
    print "n"

if []:                          # n
    print "y"
else:
    print "n"

if 0:                           # n
    print "y"
else:
    print "n"

if "":                          # n
    print "y"
else:
    print "n"

Ruby

true and false are builtin objects.

The following evaluates to false:

Everything else evaluates to true (including 0, 0.0, "0", "", []). (this is similar to emacs lisp. 〔☛ Emacs Lisp Basics〕)

# -*- coding: utf-8 -*-
# ruby

if false then p "y" else p "n" end # n
if nil then p "y" else p "n" end   # n
if () then p "y" else p "n" end    # n. Empty paren eval to nil, so false.

if true then p "y" else p "n" end # y
if 0 then p "y" else p "n" end    # y
if 0.0 then p "y" else p "n" end  # y
if [] then p "y" else p "n" end   # y
if {} then p "y" else p "n" end   # y
if "" then p "y" else p "n" end   # y

Perl

Perl does not have a boolean type. Basically, anything that eval to {0, undef, empty string, empty array, empty hash}, are false. Everything else is true.

Perl does automatic conversion between number and string, so '0' is false in some contexts because it converts to 0. But '0.0' is true, because it remains a string, and is not empty string.

The value of Perl's {array, list, hash}, depends on context, and is not very intuitive.

The best thing is to test what you need exactly. For example, check if the length of a list is 0, or whether a var has value 0, or whether it is undef.

# -*- coding: utf-8 -*-
# perl

use strict;

if (0) { print "yes"} else { print "no"}     # ⇒ no
if (0.0) { print "yes"} else { print "no"}   # ⇒ no
if ("0") { print "yes"} else { print "no"}   # ⇒ no
if ("") { print "yes"} else { print "no"}    # ⇒ no
if (undef) { print "yes"} else { print "no"} # ⇒ no
# -*- coding: utf-8 -*-
# perl

use strict;
# empty array is false

my @myArray = ();
if (@myArray) { print "yes"} else { print "no"} # ⇒ no
# -*- coding: utf-8 -*-
# perl

use strict;
# empty hash is false

my %myHash = ();
if (%myHash) { print "yes"} else { print "no"} # ⇒ no
# -*- coding: utf-8 -*-
# perl

use strict;

if (1) { print "yes"} else { print "no"}     # ⇒ yes
if ("0.0") { print "yes"} else { print "no"} # ⇒ yes
if (".0") { print "yes"} else { print "no"}  # ⇒ yes

Explicit Testing

# -*- coding: utf-8 -*-
# perl

use strict;
# examples of explicit testing

my $x = 5;
my $y;
if (defined($x)) { print "yes"} else { print "no"} # ⇒ yes
if (defined($y)) { print "yes"} else { print "no"} # ⇒ no
if ($x == 0) { print "yes"} else { print "no"} # ⇒ no
# -*- coding: utf-8 -*-
# perl

use strict;
# testing array length

my @myArray = ();
my $myArrayLength = scalar @myArray;
if ($myArrayLength == 0) { print "yes"} else { print "no"} # ⇒ yes
blog comments powered by Disqus