Conditional statements syntax within python


Ok, I have a question for setting up conditional statements. Lets say I want to test an input from the user against the following conditions (hypothetical):

if input is >= 10
return or print this
elif input is < 10 and >= 5
return or print this
elif input is < 5
return or print this

I get the >= and < and how to set those up. However, it is the setting up the conditions in between the them that I want to set up. I tried to do "<10 and >=5" and it wouldn't function because of how the and function works. Do I actually have to do like one of the lessons and set it up as follows:

if input is >= 10
" "
elif input is >= 5
" "
elif input is < 5
" "

or can it be set up differently? Whenever I think about conditional statements, I generally think of the first example, at least that is how it is set up in other subjects. I am new to the python language and is just a little confusing.


if you want to do it that way, you would have to do
elif input < 10 and input >= 5:
but there's an easier way.
You can just say
elif input >= 5:
because if it's NOT less than ten, then it will fall into the if statements condition. It can only match one of the conditions, (if, elif, else, etc), so whichever one it matches first is what it will act on.
I hope this makes some kind of sense.
Also, the statements shouldn't have the word "is" in them.

Personally, how I would go about writing this is:

if input >= 10:
    #do something
elif input >= 5:
    #do something
    #do something

The reason I would say use else in this case rather than another elif is because input being less than 5 is the only other option, so else covers it.
Also remember that else cannot have a condition, because I know that I've forgotten that and I've seen other people forget as well.

Some more notes you may find interesting:
This isn't exactly an answer to your question, but it involves taking the input, and returning things based on the range of it.
I wrote a tiny, quite unimpressive program, just for the purpose of this to explain using the range function in an if or elif condition.

input = raw_input("enter a number")
if int(input) in range(9):
    print "between 0 and 9"
elif int(input) in range(15):
    print "between 9 and 15"

I'll walk through it for you.
The first line is taking the user input, using the function raw_input.
Next, the if statement is introduced. (remember that when you take user input and you got a number, you have to convert it to an integer or float using the function int() or else it's dealt with like a string.) What the condition is testing for is if the number is in the range of 0 to 9. If you just give one number to the function, it will assume that you're incrementing by one, and that you started at zero. You'll learn more about range later, and how you can give up to three arguments.
Basically, that's all there is to this program, but you can also note that in the second one, if the user input is above nine and less than 15, it will fall into that category, even though the only visible condition is that it needs to be less than 15. The reason for this is because it doesn't pass the first if check, so it moves on and realizes that it can pass the elif condition.


Hey kyleaw,

Thank you for your response. Yeah I been thinking about that and you are right. Makes better sense. However I do have a question from your response. You made the above quote and I guess I am getting a little confused because from what I have written so far, I have not used the word "is". Was this a typo or what did you mean by that?



you used the words is, but i was trying to say that you shouldnt


Ah ok, I understand. Thank you for your help.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.