Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

agnos.is Forums

  1. Home
  2. Programmer Humor
  3. Modern Programming

Modern Programming

Scheduled Pinned Locked Moved Programmer Humor
programmerhumor
74 Posts 46 Posters 2 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • kn0wmad1c@programming.devK [email protected]

    num % 2 isn't a boolean result in any of these languages, so I feel like it would always output "odd"

    Edit: 0 is false, everything else is true.

    K This user is from outside of this forum
    K This user is from outside of this forum
    [email protected]
    wrote on last edited by
    #17

    0 is false in C, Python, and JS. It should work

    1 Reply Last reply
    6
    • kn0wmad1c@programming.devK [email protected]

      num % 2 isn't a boolean result in any of these languages, so I feel like it would always output "odd"

      Edit: 0 is false, everything else is true.

      M This user is from outside of this forum
      M This user is from outside of this forum
      [email protected]
      wrote on last edited by
      #18

      You'd be surprised.

      But seriously, numbers can be used as booleans in an impressive number of languages. Including machine code for almost every machine out there.

      1 Reply Last reply
      1
      • entropicdrift@lemmy.sdf.orgE [email protected]
        print("odd" if num % 2 else "even")
        

        That's the native python version, for those curious

        K This user is from outside of this forum
        K This user is from outside of this forum
        [email protected]
        wrote on last edited by
        #19

        Why is the return first?

        J B 2 Replies Last reply
        7
        • moomoomoo309@programming.devM [email protected]

          All of those languages will convert numbers into booleans, 0 is false, all other numbers are true.

          kn0wmad1c@programming.devK This user is from outside of this forum
          kn0wmad1c@programming.devK This user is from outside of this forum
          [email protected]
          wrote on last edited by
          #20

          That makes sense. However, num % 2 equaling 0 would mean it's even, and not "odd" like the ternary operator is outputting, yeah?

          1 Reply Last reply
          0
          • moomoomoo309@programming.devM [email protected]

            All of those languages will convert numbers into booleans, 0 is false, all other numbers are true.

            kn0wmad1c@programming.devK This user is from outside of this forum
            kn0wmad1c@programming.devK This user is from outside of this forum
            [email protected]
            wrote on last edited by
            #21

            Ah that makes sense.

            satyrsack@lemmy.sdf.orgS 1 Reply Last reply
            2
            • kn0wmad1c@programming.devK [email protected]

              num % 2 isn't a boolean result in any of these languages, so I feel like it would always output "odd"

              Edit: 0 is false, everything else is true.

              sleeplessone@lemmy.mlS This user is from outside of this forum
              sleeplessone@lemmy.mlS This user is from outside of this forum
              [email protected]
              wrote on last edited by
              #22

              In JS at least, there's a concept of truthiness and falsiness. 0, undefined, null, and a few other non-boolean values are treated as false if used in conditionals and logical operations, while every other value is treated as true. I'm pretty sure python has something similar.

              H 1 Reply Last reply
              4
              • K [email protected]

                Why is the return first?

                J This user is from outside of this forum
                J This user is from outside of this forum
                [email protected]
                wrote on last edited by
                #23

                I think the idea is it reads more naturally, so you can read it like this return A if statement is true else return B

                N 1 Reply Last reply
                16
                • entropicdrift@lemmy.sdf.orgE [email protected]
                  print("odd" if num % 2 else "even")
                  

                  That's the native python version, for those curious

                  D This user is from outside of this forum
                  D This user is from outside of this forum
                  [email protected]
                  wrote on last edited by
                  #24

                  The ternary syntax is really my only real gripe with python design -- putting the conditional BETWEEN the true and false values feels so very messy to me.

                  rbos@lemmy.caR B idunnololz@lemmy.worldI 3 Replies Last reply
                  83
                  • K [email protected]

                    Why is the return first?

                    B This user is from outside of this forum
                    B This user is from outside of this forum
                    [email protected]
                    wrote on last edited by [email protected]
                    #25

                    Edit... I reread your comment and realized that python does it differently and that everything I typed was irrelevant... I'm still gonna leave it if anyone is interested in ternary expressions, but I suppose the answer to your question is, that's just how python does it.

                    That's how ternary operators are designed to work. In essence, if you're looking to do a single line if/then, you can directly assign a variable from the result of a ternary expression.

                    As an example, I was scripting something earlier where there may or may not be a value returned from a function, but I still had to do something with that return value later. For this thing, I was using JavaScript.

                    I ended up with:

                    return platform == "name"  ? "Option 1" : "Option 2"
                    

                    If I were to write that out in a typical if/then it would be:

                    if (platform == "name") {
                        return "option 1"
                    } else {
                        return "option 2"
                    }
                    
                    

                    A ternary starts with a boolean expression, then the if true value, else the false value. That's returned to either a variable or if in a function like my example, to the object calling the function. It's just a way to write less code that in many cases is easier to read.

                    1 Reply Last reply
                    8
                    • P [email protected]

                      Yeah... I played that "serial killer or programming language inventor" game.

                      The only one I was completely in disagreement with was the inventor of Python. He's definitely a mass-murderer

                      C This user is from outside of this forum
                      C This user is from outside of this forum
                      [email protected]
                      wrote on last edited by [email protected]
                      #26

                      Are you sure it isn't just that he's Dutch?

                      1 Reply Last reply
                      4
                      • D [email protected]

                        The ternary syntax is really my only real gripe with python design -- putting the conditional BETWEEN the true and false values feels so very messy to me.

                        rbos@lemmy.caR This user is from outside of this forum
                        rbos@lemmy.caR This user is from outside of this forum
                        [email protected]
                        wrote on last edited by
                        #27

                        It's kinda natural to me having used Perl a lot.

                        S L 2 Replies Last reply
                        12
                        • R [email protected]

                          Python does that, too.

                          https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not

                          satyrsack@lemmy.sdf.orgS This user is from outside of this forum
                          satyrsack@lemmy.sdf.orgS This user is from outside of this forum
                          [email protected]
                          wrote on last edited by
                          #28

                          Are you just referring to how Python uses the English and/or instead of the more common &&/||? I think what the user above you was talking about was Lua's strange ternary syntax using and/or.

                          lime@feddit.nuL 1 Reply Last reply
                          1
                          • kn0wmad1c@programming.devK [email protected]

                            Ah that makes sense.

                            satyrsack@lemmy.sdf.orgS This user is from outside of this forum
                            satyrsack@lemmy.sdf.orgS This user is from outside of this forum
                            [email protected]
                            wrote on last edited by
                            #29

                            It doesn't make sense. I understand it, but it doesn't make sense.

                            L 1 Reply Last reply
                            1
                            • entropicdrift@lemmy.sdf.orgE [email protected]

                              Python is kinda like that in general, unless you try to make it read like ass

                              W This user is from outside of this forum
                              W This user is from outside of this forum
                              [email protected]
                              wrote on last edited by
                              #30

                              You would not believe the number of people I’ve interviewed who excel at making Python read like ass.

                              entropicdrift@lemmy.sdf.orgE 1 Reply Last reply
                              4
                              • W [email protected]

                                You would not believe the number of people I’ve interviewed who excel at making Python read like ass.

                                entropicdrift@lemmy.sdf.orgE This user is from outside of this forum
                                entropicdrift@lemmy.sdf.orgE This user is from outside of this forum
                                [email protected]
                                wrote on last edited by
                                #31

                                I mean, it does have enough ways to write the same thing that it can really allow for some funny code golf, but some people just have no sense of readability whatsoever.

                                1 Reply Last reply
                                5
                                • rbos@lemmy.caR [email protected]

                                  It's kinda natural to me having used Perl a lot.

                                  S This user is from outside of this forum
                                  S This user is from outside of this forum
                                  [email protected]
                                  wrote on last edited by
                                  #32

                                  That's not quite the argument you might think it is

                                  rbos@lemmy.caR 1 Reply Last reply
                                  18
                                  • S [email protected]

                                    That's not quite the argument you might think it is

                                    rbos@lemmy.caR This user is from outside of this forum
                                    rbos@lemmy.caR This user is from outside of this forum
                                    [email protected]
                                    wrote on last edited by
                                    #33

                                    Argument?

                                    S 1 Reply Last reply
                                    3
                                    • maven@lemmy.zipM [email protected]
                                      This post did not contain any content.
                                      mdhughes@lemmy.sdf.orgM This user is from outside of this forum
                                      mdhughes@lemmy.sdf.orgM This user is from outside of this forum
                                      [email protected]
                                      wrote on last edited by
                                      #34

                                      print( ["even", "odd"][num % 2] )

                                      If you need to avoid evaluating the wrong branch:

                                      print( [lambda: "even", lambda: "odd"][num % 2]() )

                                      1 Reply Last reply
                                      36
                                      • maven@lemmy.zipM [email protected]
                                        This post did not contain any content.
                                        E This user is from outside of this forum
                                        E This user is from outside of this forum
                                        [email protected]
                                        wrote on last edited by
                                        #35

                                        For optimal performance, you should rewrite it in Rust:

                                        inline_python::python! {
                                            print(js2py.eval_js("(number) => number % 2 ? 'odd' : 'even'")(number))
                                        };
                                        
                                        A 1 Reply Last reply
                                        115
                                        • rbos@lemmy.caR [email protected]

                                          Argument?

                                          S This user is from outside of this forum
                                          S This user is from outside of this forum
                                          [email protected]
                                          wrote on last edited by
                                          #36

                                          You know, the stuff in @_

                                          rbos@lemmy.caR 1 Reply Last reply
                                          9
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups