Python Lessons

A collection of free, in-depth Python lessons for beginners on core topics.

The Python match-case Statement

The Python match-case statement conditionally executes code depending on the value of an expression. It is meant to simplify if-elif-else chains where the conditions are comparisons of the same expression to different values. First introduced in Python version 3.10, the match-case syntax is a relatively late addition to the language.

The Python while-else Clause

The Python while-else clause is an additional, optional syntax Python's while loop supports, similar to the else clause in the for loop. It enables you to execute a code block after the loop has finished normally, that is, after the while loop's condition has become false. Abnormal, premature termination, due to reasons such as a break or return statement, skips that code.

The Python pass Statement

The Python pass statement serves as a no-operation placeholder. It simply means "do nothing". However, despite its lazy nature, it is useful as a placeholder for code that you are going to fill in later. Because code blocks in Python are marked with indentations, Python statements that require code blocks underneath them need something to be indented, and the pass statement serves that purpose.

Emulating do-while Loops in Python

A do-while loop ensures code executes at least once, checking the termination condition at the end. Although Python lacks a native do-while loop, it can be emulated using a 'while True' loop with a 'break' statement when a certain condition is met.

The Python continue Statement

The Python continue statement is used to skip the remaining code in the current loop iteration and proceed to the next one. It can be used with both the for and while loops. In nested loops, continue only impacts the innermost loop.