About 42,100 results
Open links in new tab
  1. python - What is the correct syntax for 'else if'? - Stack Overflow

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

  2. when to use if vs elif in python - Stack Overflow

    Apr 1, 2014 · In some cases, elif is required for correct semantics. This is the case when the conditions are not mutually exclusive: if x == 0: result = 0 elif y == 0: result = None else: result = x / y In some cases it is efficient because the interpreter doesn't need to check all conditions, which is the case in your example.

  3. python - Lambda including if...elif...else - Stack Overflow

    Jul 9, 2017 · Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

  4. Python, why elif keyword? - Stack Overflow

    No, elif is the analogue of else if in C, which has precisely equivalent power (even though it's implemented with different parsing rules). You can use else if instead of case even in C. And this certainly has nothing to do with why elif is used in python rather than else if. –

  5. python - Putting an if-elif-else statement on one line ... - Stack …

    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 …

  6. python - `elif` in list comprehension conditionals - Stack Overflow

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

  7. else if vs elif in python, are they equal? - Stack Overflow

    May 12, 2020 · On the other hand by using elif you'll be able to have all the different code paths on the same level of indentation. Here is an example. if a: A() else: if b: B() else: if c: C() As you can see, this is kind of confusing. Here is the elif based example. if a: A() elif b: B() elif c: C()

  8. Does elif must followed by else in python as a common rule?

    Jan 13, 2015 · if platform == 'ios': do_something() elif platform == 'android': do_something_else() And this piece of code was strongly criticized by one of my colleague. He accused me why I don't have an else block to handle the rest part. In my opinion, all I need to do is to deal with these two situations, since I did not write the else block.

  9. python - Using in range(..) in an if-else statment - Stack Overflow

    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"

  10. Python 2.7 if / elif statement with or - Stack Overflow

    Jan 6, 2016 · i'm fairly new to python so i'm sure i'm doing something wrong. i am defining a function which accepts a string variable. i can not be sure exactly what the variable will be, but there a are a 3 va...

Refresh