(Day 10) Task : Functions with Output and Calculator Project :-


We've seen functions with only an execution body, functions with inputs that allow for variation in execution of the function body and now we'll see the final form of functions. Functions that can have outputs.
TODO 1 :
Create a function called format_name() that takes two inputs: f_name and `l_name'.
def format_name(fname, I_name):
print(fname)
print(I_name)
format_name("adItya" , "ShaRma") #Output :adItya ShaRma
TODO 2 :
Use the title() function to modify the f_name and l_name parameters into Title Case.
In Python, the term "title" refers to a string method that converts the first character of each word in a string to uppercase and the remaining characters to lowercase. This is often referred to as "title case." Non-alphanumeric characters are ignored.
def format_name(f_name,l_name):
print(f_name.title())
print(l_name.title())
format_name("adItya rAm" , "ShaRma") #Output : Aditya Ram Sharma
Print (Display) vs. Output (Return) :-
The return statement is used to give back a value from a function, which can be used later vs the print statement is used to display a value to the console only for the programmer to see.
return
: Sending Data Back to the Caller
The return
statement is used within a function to send the result of the function's computation back to the caller. This returned value can then be stored, manipulated, or used in further computations.
def add(a, b):
return a + b
result = add(3, 4)
print(result) # Outputs: 7
print
: Displaying Information to the Console
The print
function outputs information to the console. It's primarily used for displaying messages or debugging purposes. Unlike return
, print
does not send data back to the caller; it merely displays it.
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # Outputs: Hello, Alice!
Syntax :-
You can create a function with a body, input and output like this:
def function_name(input_parameter):
<body of function that uses input_argument>
return output
Function_name()
Multiple Return Value :-
Functions terminate at the return keyword. If you write code below the return statement that code will not be executed. However, you can have multiple return statements in one function. So how does that work?
Conditional Returns :
Conditional Returns allow a function to return different values or take different execution paths based on conditional checks.
When we have control flow, as in the code will behave differently (go down different execution paths) depending on certain conditional checks, we can end up with multiple endings (returns).
def canBuyAlcohol(age):
if age >= 18:
return True
else:
return False
Empty Returns :
An Empty Return is a return statement without any value, used to exit a function without returning anything. You can also write return without anything afterwards, and this just tells the function to exit.
def canBuyAlcohol(age):
if type(age) != int: #If the data type of the age input is not a int, then exit.
return
if age >= 18:
return True
else:
return False
Docstrings :-
You can use docstrings to write multiline comments that document your code. Just enclose your text inside a pair of three double quotes.
Documenting Functions :
it involves using docstrings just below a function definition to describe its purpose , which can be displayed when hover the function call.A neat feature of docstrings is you can use it just below the definition of a function and that text will be displayed when you hover over a function call. It's a good way to remind yourself what a self-created function does.
def my_function(num):
"""Multiplies a number by itself."""
return num * num
my_function
Calculator Project :-
The goal is to build a calculator program. Demo https://appbrewery.github.io/python-day10-demo/
Import art :-
import art
Write out 4 functions - addition, subtract, multiply and divide :-
def add(n1, n2): return n1 + n2 #TODO: Write out the other 3 functions - subtract, multiply and divide. def multiply(n1 , n2): return n1 * n2 def divide(n1 , n2): return n1/n2 def subtract(n1 , n2): return n1 - n2
Add these 4 functions into a dictionary as the values. Keys = "+", "-", "*", "/" :-
operations = {"+": add , "*" : multiply , "/" : divide , "-" : subtract}
Other Functionalities :-
Function Definition and Logo Display :
def calculator(): print(art.logo)
Defines the
calculator
function.Displays a logo or banner from the
art
module.
Initialization :
should_accumulate = True num1 = float(input("What is the first number: "))
Sets a flag
should_accumulate
to control the loop for continuous calculations.Prompts the user to input the first number and converts it to a float.
Main Calculation Loop :
while should_accumulate: for symbol in operations: print(symbol)
Enters a loop that continues as long as
should_accumulate
isTrue
.Iterates over the keys in the
operations
dictionary (e.g., '+', '-', '*', '/') and prints them.
User Input for Operation and Second Number :
operational_symbol = input("Pick an operator: ") num2 = float(input("What is your next number: "))
Prompts the user to select an operator.
Prompts the user to input the next number and converts it to a float.
Perform Calculation :
answer = operations[operational_symbol](num1, num2)
- Retrieves the function corresponding to the selected operator from the
operations
dictionary and applies it tonum1
andnum2
.
- Retrieves the function corresponding to the selected operator from the
Display Result :
print(f"{num1} {operational_symbol} {num2} = {answer}")
- Displays the calculation and result in a formatted string.
Prompt for Continuation :
choice = input(f"Type 'y' to continue calculating with {answer}, or type 'n' to start a new calculation: ")
- Asks the user whether to continue with the current result or start anew.
Handle User Choice
if choice == "y": num1 = answer else: should_accumulate = False print("\n" * 20) calculator()
If the user chooses to continue,
num1
is updated to the currentanswer
.If not, sets
should_accumulate
toFalse
, prints multiple newlines to clear the screen, and recursively callscalculator()
to start over.
Initial Function Call :
calculator()
- Initiates the calculator by calling the function.
Code :-
Subscribe to my newsletter
Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Aditya Sharma
Aditya Sharma
DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS