0
0
Pythonprogramming~15 mins

Methods with return values in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Methods with return values
📖 Scenario: You are creating a simple calculator program that can add two numbers and return the result.
🎯 Goal: Build a method that takes two numbers, adds them, and returns the sum. Then use this method to get the result and print it.
📋 What You'll Learn
Create a method named add_numbers that takes two parameters: a and b.
The method add_numbers should return the sum of a and b.
Call the method add_numbers with two numbers and store the result in a variable named result.
Print the value of result.
💡 Why This Matters
🌍 Real World
Methods that return values are used in calculators, games, and many apps to perform tasks and give back results.
💼 Career
Understanding how to write and use methods with return values is a key skill for software developers to create reusable and organized code.
Progress0 / 4 steps
1
Create two number variables
Create two variables called num1 and num2 and set them to 5 and 7 respectively.
Python
Need a hint?

Use = to assign values to variables.

2
Define the add_numbers method
Define a method named add_numbers that takes two parameters called a and b. The method should return the sum of a and b.
Python
Need a hint?

Use def to define a method and return to send back the result.

3
Call the add_numbers method and store the result
Call the method add_numbers with num1 and num2 as arguments. Store the returned value in a variable called result.
Python
Need a hint?

Call the method by writing its name and passing the variables inside parentheses.

4
Print the result
Print the value of the variable result to display the sum of the two numbers.
Python
Need a hint?

Use print(result) to show the sum on the screen.