Python does not have if statements

Yes of course i does. Please look at the interesting tweet below.
Julia has the Bool type of course. But when I did a little digging I find that Bool takes up 8 bits - I guess that does not really matter on modern computer systems with lots of memory.
Would the same ‘trick’ as in this tweet work in Julia though?
I guess not - or can you use Bool as an index?

You sure can but we start indexing with one. So this will work in Julia if you add 1 to the Bool (or use OffsetArrays). But… why?

1 Like

In fact, working with “bits” directly is either impossible or expensive on modern architectures. You would not be “saving” 7 bits. Cf performance if BitArray vs Array{Bool}.

Also, the Bool in a condition usually does not end up in RAM.


Memory addresses are referring to bytes, not to single bits (see Memory address - Wikipedia).
Thus, you would not even save memory when you do not need to store 8 or more booleans because a partly occupied byte is not usable for other purposes.
In BitArrays, each byte of memory is used to store 8 booleans, but this has a performance cost because the byte content has to be converted to single booleans.
This is at least my understanding, please correct me if I am wrong.


i have looked the tweet. i think you can get the same outputs in Julia using the ternary operator like

num = 100; numberGreaterThan10 = num > 10 ? true : false

And there is also a ternary operator in Python.

You can index a Dict with no issues.

julia> num = 100

julia> Dict([false,true] .=> ["Number is less than 10", "Number is greater than 10"])[>(10)(num)]
"Number is greater than 10"

julia> Dict([false,true] .=> ["Number is less than 50", "Number is greater than 50"])[>(50)(num)]
"Number is greater than 50"

julia> Dict([false,true] .=> ["Number is less than 100", "Number is greater than 100"])[>(100)(num)]
"Number is less than 100"

I did not understand exactly what the question is, but this syntax of course also works:

julia> n = 101

julia> isgt100 = if n > 100 true else false end

And another interesting syntax (not sure if useful at all, but maybe), is:

julia> function (x::Int)(n)
         if x > n

julia> x = 100

julia> x(99)

julia> x(101)

1 Like

More or less. Booleans in Julia are a type of integer:

julia> Bool <: Integer

and you can use them to index arrays by adding 1 (since the standard array type in Julia is 1-based):

julia> println(["100 is < 10", "100 is > 10"][(100 > 10)+1])
100 is > 10
1 Like

But it is bad style, both in Python and Juila.

1 Like

@Tamas_Papp – do you mind providing a citation for this? I’m unaware of any impossibilities or expensive outcomes when using bitwise operators in modern architectures.

Use of 0 for false and 1 for true in data science is useful and pretty much ubiquitious. For example, you could sum an array of boolean values to count the number of true outcomes.

This is fine, I was rather referring to write

foo = ["ifFalse","ifTrue"][booleanCondition]

instead of (in Python) e.g.

foo = "ifTrue" if booleanCondition else "ifFalse"

or e.g. a ternary operator in Julia.

The latter versions are both more readable (at least in my oppinion) and should be faster (no creation of an intermediate list/array).


In julia you can commit some misdemeanor type piracy to make this work:

julia> Base.to_index(b::Bool) = b + 1

julia> ["ifFalse", "ifTrue"][true]

julia> ["ifFalse", "ifTrue"][false]

Disclaimer: Please please please don’t actually do this in real code


I think you misunderstand — bitwise operators are fine, but using individual bits as first-class objects (eg for the purposes of a conditional) is not a thing. Bits are just not the unit modern CPUs or RAM work with directly.

1 Like