Python: “If” function












1















This is probably really simple but I can't figure it out.



I have a bunch of lists and I want to call certain lists if they are in the range equal to a value x and any number between x-5 and x +5. i.e. x-5,x-4,x-3,x-2,x-1,x,x+1,x+2,x+3,x+4 and x+5.



At the moment I have



if sum(listname[i])==x:

if sum(listname[i])==x-1:

if sum(listname[i])==x-2:


etc



How can I do it so that it is combined in one "if" function.



I've been thinking on the lines of something like:



if sum(listname[i])==x-5>=x>=x+5:


or



if sum(listname[i])==x or x-1 or x-2 ".. etc":


but neither work.



Can anybody shine some light on this?










share|improve this question























  • This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky. if is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)

    – timgeb
    Nov 24 '18 at 23:17








  • 1





    Try this if x-5 <= sum(listname[i]) <= x+5:. If x is a real number x-5 cannot be superior to x+5

    – Wariored
    Nov 24 '18 at 23:22


















1















This is probably really simple but I can't figure it out.



I have a bunch of lists and I want to call certain lists if they are in the range equal to a value x and any number between x-5 and x +5. i.e. x-5,x-4,x-3,x-2,x-1,x,x+1,x+2,x+3,x+4 and x+5.



At the moment I have



if sum(listname[i])==x:

if sum(listname[i])==x-1:

if sum(listname[i])==x-2:


etc



How can I do it so that it is combined in one "if" function.



I've been thinking on the lines of something like:



if sum(listname[i])==x-5>=x>=x+5:


or



if sum(listname[i])==x or x-1 or x-2 ".. etc":


but neither work.



Can anybody shine some light on this?










share|improve this question























  • This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky. if is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)

    – timgeb
    Nov 24 '18 at 23:17








  • 1





    Try this if x-5 <= sum(listname[i]) <= x+5:. If x is a real number x-5 cannot be superior to x+5

    – Wariored
    Nov 24 '18 at 23:22
















1












1








1


1






This is probably really simple but I can't figure it out.



I have a bunch of lists and I want to call certain lists if they are in the range equal to a value x and any number between x-5 and x +5. i.e. x-5,x-4,x-3,x-2,x-1,x,x+1,x+2,x+3,x+4 and x+5.



At the moment I have



if sum(listname[i])==x:

if sum(listname[i])==x-1:

if sum(listname[i])==x-2:


etc



How can I do it so that it is combined in one "if" function.



I've been thinking on the lines of something like:



if sum(listname[i])==x-5>=x>=x+5:


or



if sum(listname[i])==x or x-1 or x-2 ".. etc":


but neither work.



Can anybody shine some light on this?










share|improve this question














This is probably really simple but I can't figure it out.



I have a bunch of lists and I want to call certain lists if they are in the range equal to a value x and any number between x-5 and x +5. i.e. x-5,x-4,x-3,x-2,x-1,x,x+1,x+2,x+3,x+4 and x+5.



At the moment I have



if sum(listname[i])==x:

if sum(listname[i])==x-1:

if sum(listname[i])==x-2:


etc



How can I do it so that it is combined in one "if" function.



I've been thinking on the lines of something like:



if sum(listname[i])==x-5>=x>=x+5:


or



if sum(listname[i])==x or x-1 or x-2 ".. etc":


but neither work.



Can anybody shine some light on this?







python list if-statement range






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 23:15









P erryP erry

455




455













  • This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky. if is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)

    – timgeb
    Nov 24 '18 at 23:17








  • 1





    Try this if x-5 <= sum(listname[i]) <= x+5:. If x is a real number x-5 cannot be superior to x+5

    – Wariored
    Nov 24 '18 at 23:22





















  • This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky. if is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)

    – timgeb
    Nov 24 '18 at 23:17








  • 1





    Try this if x-5 <= sum(listname[i]) <= x+5:. If x is a real number x-5 cannot be superior to x+5

    – Wariored
    Nov 24 '18 at 23:22



















This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky. if is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)

– timgeb
Nov 24 '18 at 23:17







This is pretty theoretical, can you make a Minimal, Complete, and Verifiable example with complete sample input and output? Also, your terminology is a bit shaky. if is not a function and you can't call lists. (We should be able to understand what you want with the MCVE, though.)

– timgeb
Nov 24 '18 at 23:17






1




1





Try this if x-5 <= sum(listname[i]) <= x+5:. If x is a real number x-5 cannot be superior to x+5

– Wariored
Nov 24 '18 at 23:22







Try this if x-5 <= sum(listname[i]) <= x+5:. If x is a real number x-5 cannot be superior to x+5

– Wariored
Nov 24 '18 at 23:22














4 Answers
4






active

oldest

votes


















3














A scenario like if sum(listname[i])==x or x-1 or x-2 ".. etc": (which is not valid python) is usually solved with if value in range(start, stop, step):



So you would write:



if sum(listname[i) in range(x-2, x):
# Code for this case here...





share|improve this answer































    2














    Do you simply mean



    if x-5 <= sum(listname[i]) <= x+5:
    ...
    ...





    share|improve this answer





















    • 1





      You might want to exchange >= with <=. Otherwise good answer.

      – a_guest
      Nov 24 '18 at 23:26











    • x-5 cannot be superior to x+5

      – Wariored
      Nov 24 '18 at 23:27



















    0














    It looks like you want to check if the sum of the list is between x - 5 and x + 5. To put it in one if statement is simply:



    s = sum(listname[i])
    if s >= x - 5 and s <= x + 5:
    # do stuff





    share|improve this answer































      0














      From what I understand from your question. You what to check that whether sum(listname[i]) is between (x-5, x+5)



      You can do this in a single if assuming x is a possitive value:



      if (sum(listname[i]) >= (x - 5)) and (sum(listname[i]) <= (x+5))





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463211%2fpython-if-function%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        A scenario like if sum(listname[i])==x or x-1 or x-2 ".. etc": (which is not valid python) is usually solved with if value in range(start, stop, step):



        So you would write:



        if sum(listname[i) in range(x-2, x):
        # Code for this case here...





        share|improve this answer




























          3














          A scenario like if sum(listname[i])==x or x-1 or x-2 ".. etc": (which is not valid python) is usually solved with if value in range(start, stop, step):



          So you would write:



          if sum(listname[i) in range(x-2, x):
          # Code for this case here...





          share|improve this answer


























            3












            3








            3







            A scenario like if sum(listname[i])==x or x-1 or x-2 ".. etc": (which is not valid python) is usually solved with if value in range(start, stop, step):



            So you would write:



            if sum(listname[i) in range(x-2, x):
            # Code for this case here...





            share|improve this answer













            A scenario like if sum(listname[i])==x or x-1 or x-2 ".. etc": (which is not valid python) is usually solved with if value in range(start, stop, step):



            So you would write:



            if sum(listname[i) in range(x-2, x):
            # Code for this case here...






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 23:23









            Charles LandauCharles Landau

            2,6931216




            2,6931216

























                2














                Do you simply mean



                if x-5 <= sum(listname[i]) <= x+5:
                ...
                ...





                share|improve this answer





















                • 1





                  You might want to exchange >= with <=. Otherwise good answer.

                  – a_guest
                  Nov 24 '18 at 23:26











                • x-5 cannot be superior to x+5

                  – Wariored
                  Nov 24 '18 at 23:27
















                2














                Do you simply mean



                if x-5 <= sum(listname[i]) <= x+5:
                ...
                ...





                share|improve this answer





















                • 1





                  You might want to exchange >= with <=. Otherwise good answer.

                  – a_guest
                  Nov 24 '18 at 23:26











                • x-5 cannot be superior to x+5

                  – Wariored
                  Nov 24 '18 at 23:27














                2












                2








                2







                Do you simply mean



                if x-5 <= sum(listname[i]) <= x+5:
                ...
                ...





                share|improve this answer















                Do you simply mean



                if x-5 <= sum(listname[i]) <= x+5:
                ...
                ...






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 25 '18 at 4:44









                Wariored

                373312




                373312










                answered Nov 24 '18 at 23:22









                chrischris

                211




                211








                • 1





                  You might want to exchange >= with <=. Otherwise good answer.

                  – a_guest
                  Nov 24 '18 at 23:26











                • x-5 cannot be superior to x+5

                  – Wariored
                  Nov 24 '18 at 23:27














                • 1





                  You might want to exchange >= with <=. Otherwise good answer.

                  – a_guest
                  Nov 24 '18 at 23:26











                • x-5 cannot be superior to x+5

                  – Wariored
                  Nov 24 '18 at 23:27








                1




                1





                You might want to exchange >= with <=. Otherwise good answer.

                – a_guest
                Nov 24 '18 at 23:26





                You might want to exchange >= with <=. Otherwise good answer.

                – a_guest
                Nov 24 '18 at 23:26













                x-5 cannot be superior to x+5

                – Wariored
                Nov 24 '18 at 23:27





                x-5 cannot be superior to x+5

                – Wariored
                Nov 24 '18 at 23:27











                0














                It looks like you want to check if the sum of the list is between x - 5 and x + 5. To put it in one if statement is simply:



                s = sum(listname[i])
                if s >= x - 5 and s <= x + 5:
                # do stuff





                share|improve this answer




























                  0














                  It looks like you want to check if the sum of the list is between x - 5 and x + 5. To put it in one if statement is simply:



                  s = sum(listname[i])
                  if s >= x - 5 and s <= x + 5:
                  # do stuff





                  share|improve this answer


























                    0












                    0








                    0







                    It looks like you want to check if the sum of the list is between x - 5 and x + 5. To put it in one if statement is simply:



                    s = sum(listname[i])
                    if s >= x - 5 and s <= x + 5:
                    # do stuff





                    share|improve this answer













                    It looks like you want to check if the sum of the list is between x - 5 and x + 5. To put it in one if statement is simply:



                    s = sum(listname[i])
                    if s >= x - 5 and s <= x + 5:
                    # do stuff






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 24 '18 at 23:25









                    GniemGniem

                    914




                    914























                        0














                        From what I understand from your question. You what to check that whether sum(listname[i]) is between (x-5, x+5)



                        You can do this in a single if assuming x is a possitive value:



                        if (sum(listname[i]) >= (x - 5)) and (sum(listname[i]) <= (x+5))





                        share|improve this answer




























                          0














                          From what I understand from your question. You what to check that whether sum(listname[i]) is between (x-5, x+5)



                          You can do this in a single if assuming x is a possitive value:



                          if (sum(listname[i]) >= (x - 5)) and (sum(listname[i]) <= (x+5))





                          share|improve this answer


























                            0












                            0








                            0







                            From what I understand from your question. You what to check that whether sum(listname[i]) is between (x-5, x+5)



                            You can do this in a single if assuming x is a possitive value:



                            if (sum(listname[i]) >= (x - 5)) and (sum(listname[i]) <= (x+5))





                            share|improve this answer













                            From what I understand from your question. You what to check that whether sum(listname[i]) is between (x-5, x+5)



                            You can do this in a single if assuming x is a possitive value:



                            if (sum(listname[i]) >= (x - 5)) and (sum(listname[i]) <= (x+5))






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 24 '18 at 23:31









                            Talip Tolga SarıTalip Tolga Sarı

                            936




                            936






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463211%2fpython-if-function%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Costa Masnaga

                                Fotorealismo

                                Sidney Franklin