# Python does not have if statements

**URL:** https://discourse.julialang.org/t/python-does-not-have-if-statements/52765
**Category:** Offtopic
**Created:** [January 3, 2021, 8:34am UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765 "2021-01-03T08:34:38Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)
#### Post date: [January 3, 2021, 8:34am UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/1 "2021-01-03T08:34:38Z")

</div>

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?  
[https://docs.julialang.org/en/v1/base/numbers/#Core.Bool](https://docs.julialang.org/en/v1/base/numbers/#Core.Bool)  
I guess not - or can you use Bool as an index?

> <https://twitter.com/ctrlshifti/status/1345487495681568768>

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 3, 2021, 8:50am UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/2 "2021-01-03T08:50:52Z")

</div>

> [@johnh](#):
>
> 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?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 3, 2021, 9:33am UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/3 "2021-01-03T09:33:55Z")

</div>

> [@johnh](#):
>
> Bool takes up 8 bits - I guess that does not really matter on modern computer systems with lots of memory

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.

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [January 3, 2021, 10:34am UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/4 "2021-01-03T10:34:56Z")

</div>

Memory addresses are referring to bytes, not to single bits (see [Memory address - Wikipedia](https://en.m.wikipedia.org/wiki/Memory_address#Unit_of_address_resolution)).  
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.

---

<div class="post-metadata">

### Author: ![galihaktan](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@galihaktan](https://discourse.julialang.org/u/galihaktan)
#### Post date: [January 3, 2021, 1:08pm UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/5 "2021-01-03T13:08:14Z")

</div>

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

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

```

And there is also a ternary operator in Python.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [January 3, 2021, 2:00pm UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/6 "2021-01-03T14:00:02Z")

</div>

You can index a `Dict` with no issues.

```julia
julia> num = 100
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"

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 3, 2021, 2:26pm UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/7 "2021-01-03T14:26:35Z")

</div>

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

```julia
julia> n = 101
101

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

```

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

```julia
julia> function (x::Int)(n)
         if x > n
           true
         else
           false
         end
       end

julia> x = 100
100

julia> x(99)
true

julia> x(101)
false

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 3, 2021, 2:34pm UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/8 "2021-01-03T14:34:43Z")

</div>

> [@johnh](#):
>
> Would the same ‘trick’ as in this tweet work in Julia though?

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

```julia
julia> Bool <: Integer
true

```

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

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

```

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [January 3, 2021, 2:52pm UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/9 "2021-01-03T14:52:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cce/32/460_2.png) [@cce](https://discourse.julialang.org/u/cce)
#### Post date: [January 3, 2021, 4:54pm UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/10 "2021-01-03T16:54:20Z")

</div>

> [@Tamas\_Papp](#):
>
> In fact, working with “bits” directly is either impossible or expensive on modern architectures.

@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.

---

<div class="post-metadata">

### Author: ![cce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cce/32/460_2.png) [@cce](https://discourse.julialang.org/u/cce)
#### Post date: [January 3, 2021, 4:58pm UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/11 "2021-01-03T16:58:15Z")

</div>

> [@lungben](#):
>
> But it is bad style, both in Python and Juila.

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.

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [January 3, 2021, 6:33pm UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/12 "2021-01-03T18:33:11Z")

</div>

This is fine, I was rather referring to write

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

```

instead of (in Python) e.g.

```julia
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).

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [January 3, 2021, 6:48pm UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/13 "2021-01-03T18:48:11Z")

</div>

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

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

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

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

```

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

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 4, 2021, 6:20am UTC](https://discourse.julialang.org/t/python-does-not-have-if-statements/52765/14 "2021-01-04T06:20:34Z")

</div>

> [@cce](#):
>
> I’m unaware of any impossibilities or expensive outcomes when using bitwise operators in modern architectures.

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.
