Search results
Dec 25, 2012 · if expression1: statement1 elif expression2: statement2 else: statement3 Or a real-world example: if i > 100: x = 2 elif i < 100: x = 1 else: x = 0 I just feel if the example above could be written the following way, it could look like more concise. x = 2 if i>100 elif i<100 1 else 0 # [WRONG]
Apr 1, 2014 · elif is a bit more efficient, and it's quite logical: with ifs the program has to evaluate each logical expression every time. In elif s though, it's not always so. However, in your example, this improvement would be very, very small, probably unnoticeable, as evaluating x > 0 is one of the cheapest operations.
Mar 5, 2013 · "Elif" seems to have originated with the C preprocessor, which used #elif long before Python AFAICT. Obviously, in that context having a single-token directive is valuable, since parsing #else if <code> vs. #else <code that could theoretically even be an if statement> would've complicated a syntax that was intended to be bog-simple.
Jul 9, 2017 · Now available on Stack Overflow for Teams! AI features where you work: search, IDE, and chat. Learn more Explore Teams
Mar 21, 2010 · Note that this is pseudo-code not Python code. In Python you cannot create functions called and or or because these are keywords. Also you should never use "evaluate" or if bool(...). Customizing the behavior of your own classes. This implicit bool call can be used to customize how your classes behave with and, or and not.
Aug 27, 2012 · python 2.7. Is it possible to do this: print "Enter a number between 1 and 10:" number = raw_input("> ") if number in range(1, 5): print "You entered a number in the range of 1 to 5" elif number in range(6, 10): print "You entered a number in the range of 6 to 10" else: print "Your number wasn't in the correct range"
@jdi Though conditional-expressions may not be to your taste, they were specifically designed to handle if-elif-elif-else chains, just as the OP requested. They aren't hard to learn and can gracefully handle situations that aren't as amenable to dictionary lookup logic: 'A' if grade>=90 else 'B' if grade>=80 else 'C' if grade>=70 else 'F' .
Feb 14, 2012 · The 'elif' caused a dependency with the 'if' so that if the original 'if' was satisfied the 'elif' will not initiate even if the 'elif' logic satisfied the condition as well. Let's change the second 'elif' to an 'if' instead.
elif and else must immediately follow the end of the if block, or Python will assume that the block has closed without them. if 1: pass <--- this line must be indented at the same level as the `pass` else: pass In your code, the interpreter finishes the if block when the indentation, so the elif and the else aren't associated with it. They are ...
Here's a Python 2 version: #!python2 while True: prompt1=raw_input('Can I make this stupid thing work?').lower() if prompt1 == 'yes': print 'Hooray, I can!' elif prompt1 == 'no': print 'Well I did anyway!' else: print 'Huh?' #an answer that wouldn't be yes or no raw_input is used instead of input.