Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Function Body | What Is a Function in Python?
Python Functions Tutorial

bookFunction Body

The function body is the block of code inside a function definition. It contains the instructions that define what the function does.

Previously, you used mathematical formulas and the print() function as the body. However, a function body can include loops, if-else statements, keywords, or other logic.

Use one level of indentation to define the function body. Indentation in Python determines the structure of the code and specifies which statements belong to the function.

Example: Cat's Health Level

Write a function that determines a cat's health level based on the amount of calories it consumes daily. Consider the following conditions:

  • If the cat consumes less than 200 calories per day, the health level is "low";
  • If the cat consumes between 200 and 400 calories per day, the health level is "average";
  • If the cat consumes more than 400 calories per day, the health level is "high".
12345678910111213141516
def health_level_for_cat(calories_per_day): # Use one indentation to create function body if calories_per_day < 200: health_level = 'Low' elif 200 <= calories_per_day <= 400: health_level = 'Average' else: health_level = 'High' message = f"The cat's health level based on calorie intake is {health_level}." return message # Example usage of the function print("Cat Felix:", health_level_for_cat(187)) print("Cat Tom:", health_level_for_cat(301)) print("Cat Oggy:", health_level_for_cat(404))
copy

The function's body begins with the function definition. Inside, an if-else statement determines the cat's health level based on daily calorie intake. After evaluating the condition, the function constructs a message reflecting the health level and returns it as the function's output.

In this example, the function assesses the cat's health level based on calorie consumption and generates a corresponding message, which is then printed as the function's output.

question mark

What is the purpose of the function body in Python?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 4.35

bookFunction Body

Swipe to show menu

The function body is the block of code inside a function definition. It contains the instructions that define what the function does.

Previously, you used mathematical formulas and the print() function as the body. However, a function body can include loops, if-else statements, keywords, or other logic.

Use one level of indentation to define the function body. Indentation in Python determines the structure of the code and specifies which statements belong to the function.

Example: Cat's Health Level

Write a function that determines a cat's health level based on the amount of calories it consumes daily. Consider the following conditions:

  • If the cat consumes less than 200 calories per day, the health level is "low";
  • If the cat consumes between 200 and 400 calories per day, the health level is "average";
  • If the cat consumes more than 400 calories per day, the health level is "high".
12345678910111213141516
def health_level_for_cat(calories_per_day): # Use one indentation to create function body if calories_per_day < 200: health_level = 'Low' elif 200 <= calories_per_day <= 400: health_level = 'Average' else: health_level = 'High' message = f"The cat's health level based on calorie intake is {health_level}." return message # Example usage of the function print("Cat Felix:", health_level_for_cat(187)) print("Cat Tom:", health_level_for_cat(301)) print("Cat Oggy:", health_level_for_cat(404))
copy

The function's body begins with the function definition. Inside, an if-else statement determines the cat's health level based on daily calorie intake. After evaluating the condition, the function constructs a message reflecting the health level and returns it as the function's output.

In this example, the function assesses the cat's health level based on calorie consumption and generates a corresponding message, which is then printed as the function's output.

question mark

What is the purpose of the function body in Python?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5
some-alt