Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Latest commit

 

History

History
29 lines (21 loc) · 1.18 KB

File metadata and controls

29 lines (21 loc) · 1.18 KB
title Python Loops
titleTemplate Snippet Collection | massCode
description Collection of Python Loop snippets

Python Loops

Loops

Genarally speaking python only has 2 types of loops, even though if...else comes in conditional statements I have included it in this section. Loops are one of basic in python however it comes handy when you want to perform operation again repeatedly. for and while loops are mostly used more.

you can also insert "pass" key word instead of statements which you don't want to execute thus it skips executing that block of code in loops

however if you comes from other language background such as C++ or java, Python does not have do-while loop. but i'm sure our brilliant minds will make your self some crazy idea just for do-while loop. :)

While Loop

Tip :- you can use else with while loops thus you can create do-while loop however it acts as postcondition loop such that condition is tested first before any statements in while true or else block statements are executed.

Syntax of while loop

while conditional_test:
  statement_to_execute_when_conditinal_test_is_true
else:
  else_statement_when_conditinal_test_is_false