What is a block of code?

Question

What makes up a block of code?

Answer

Any time you hear something referred to as a block or block of code it typically means the same thing. At least in programming.

There’s no better way to explain it than to show you visually! Don’t worry about understanding the code below, but understand that the chunks of code written at the same indentation level in Python make up a block of code. Notice the small gray arrow on line 4? That indicates a block of code belonging to whatever is on the line with the arrow - a function in this case.

20 Likes

Hello, how can I call that block code inside my_function and how can I name my_function?
Thank you

3 Likes

the general syntax for a function:

def function_name():

def is short for define. So here you define the function, then you name the function anything you like, then to call the function do:

function_name()
9 Likes

A block of code is composed of several statements that are intended to execute when certain condition is met. In Python, a block of code is implemented using indentation.

An example,

     if(true)
           print "this is the first line of the block"
           print "this is the second  line of the block"
           print "this is the third  of the block"
    else
           print "this is the first line of another block"
           print "this is the second  line of another block"
           print "this is the third  of another block"
3 Likes

A post was split to a new topic: Learning python