Turn Messy Text into Structured Insights With AI. Join Greg Live:Β Dec 4th, 2024
Leverage
Glossary

Block

Code blocks in Python

In Python, code block refers to a collection of code that is in the same block or indent. This is most commonly found in classes, functions, and loops.

You can where code blocks are because they are 1) on the same indent and 2) are uninterrupted by other lines of code not on the same indent.

Python code blocks are useful because they tell you what pieces of code will run as a part of a loop, class, or function.

def code_block():
  # Everything in this function is part of the same code block
  print (1)
  print (2)
  
for i in range(4):
  # Everyting in this loop is part of the same code block
  print (i)

Let’s take a look at a python to assign a variable code sample.

Link to code

On this page

No Headings