avatarLiu Zuo Lin

Summary

The web content provides an explanation of the various uses of the else keyword in Python, including its application in if-else, for-else, while-else, and try-else blocks, and concludes with a call to support the creator.

Abstract

The article "Every Way To Use ‘Else’ in Python" delves into the multifaceted nature of the else keyword beyond the conventional if-else construct. It outlines the usage of else in loop constructs such as for-else and while-else, where the else block executes only if a break statement is not encountered within the loop. Additionally, it discusses the try-else structure, where the else block runs only if no exceptions are raised in the try block. The author, Liu Zuo Lin, provides code examples to illustrate each case and concludes by inviting readers to support his work through various means, including clapping for the story, commenting, highlighting, and subscribing to his content across different platforms.

Opinions

  • The author believes that understanding the different applications of the else keyword can be helpful to Python programmers.
  • The use of else in loops is presented as a lesser-known feature that can be beneficial in certain programming scenarios.
  • The try-else construct is highlighted as a useful tool for executing code that should only run if the try block is successful.
  • The article encourages reader interaction and support for the author's work, suggesting that such engagement is valuable and appreciated.
  • The author promotes an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus, indicating a recommendation based on perceived value and performance.

Every Way To Use ‘Else’ in Python

Other than the classic if-else block, we can use the else keyword in 3 other ways in Python — for-else, while-else & try-else

1) If Else — the classic

fruit = 'orange'

if fruit == 'apple':
  print('apple juice')
else:
  print('not apple')
  • stuff inside the if block runs if the condition evaluates to True
  • stuff inside the else block runs if the condition evaluates to False

2) For Else

for i in range(5):
  # do stuff
else:
  print('this runs if break DOES NOT happens in above for loop')

In a for-else block, stuff in the else block runs only if the break statement DOES NOT run inside the for loop.

for i in range(5):
  print(i)
  if i == 2:
    break
else:
  print('else block runs')  

# 0 1 2

^ here, range(5) generates 0 1 2 3 4, and break happens when i==2.

Since break happens inside the for loop, the else block does not run.

for i in range(5):
  print(i)
  if i == 100:
    break
else:
  print('else block runs')

# 0 1 2 3 4
# else block runs

^ here, i==100 does not happen, and break does not run.

Since break does not run, else block runs.

3) While Else

while condition:
    # do stuff
else:
    print('else block runs if break DOES NOT run in while loop')

This functions the same way as the for-else block.

  • If break happens, else does not happen
  • If break does not happen, else happens

4) Try Else

try:
    # stuff
except Exception as e:
    print(e)
else:
    print('else block runs if except block DOES NOT run')
  • if except happens, else does not happen
  • if except does not happen, else happens
try:
  x = 1/0
except Exception as e:
  print('except block runs ->', e)
else:
  print('else block runs')

# except block runs -> division by zero

^ here, 1/0 forces the ZeroDivisionError, which forces the except block to run. As the except block runs, the else block does not run.

try:
  x = 1/1
except Exception as e:
  print('except block runs ->', e)
else:
  print('else block runs')

# else block runs

^ here, 1/1 does not cause an Exception. The except block does not run, so the else block runs.

Conclusion

I hope this was helpful in some way

If You Wish To Support Me As A Creator

  1. Clap 50 times for this story
  2. Leave a comment telling me your thoughts
  3. Highlight your favourite part of the story

Thank you! These tiny actions go a long way, and I really appreciate it!

YouTube: https://www.youtube.com/@zlliu246

LinkedIn: https://www.linkedin.com/in/zlliu/

My Ebooks: https://zlliu.co/ebooks

Python
Python Programming
Programming
Coding
Python3
Recommended from ReadMedium