- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Python return Keyword/Statement
return values in python 50% 590 return value in python 50% 590 return python 50% 5400 return function python 50% 3600 python tutorial pdf 10% 590 python return function 50% 3600 python return 48% 5400 python programming pdf 10% 720 python programming cheat sheet pdf 25% 20 python practice 20% 2900 python pdf 10% 1600 python how to program pdf 10% 720 python functions return 50% 3600 python functions practice problems 28% 20 python functions practice 30% 90The return keyword in Python is used when we need to exit from a function with or without a returning value. For example:
def codes(): print("First 'print()' inside 'codes()' function") return print("Second 'print()' inside 'codes()' function") def cracker(): return 10 def fresherearth(a, b): return a+b codes() x = cracker() print(x) x = fresherearth(10, 50) print(x)
The output is:
First 'print()' inside 'codes()' function 10 60
In above program, the following statement:
print("Second 'print()' inside 'codes()' function")
is not executed, because just before this statement, a return statement or keyword is used. Therefore the program flow exists from this function.
Note - After the return statement, remaining statement(s) will be not executed, if available, inside the same function.
Python return Keyword or Statement Example
The program given below is an example of return statement:
def add(x, y): return x+y def square(x): return x*x def great(x, y): if x>y: return x else: return y print("1. Add Two Numbers") print("2. Find Square of a Number") print("3. Find Greatest of Two Numbers") print("\nEnter Your Choice (1-3): ", end="") choice = int(input()) if choice == 1 or choice == 3: print("\nEnter any two numbers: ", end="") a = int(input()) b = int(input()) if choice == 1: print("\nResult =", add(a, b)) elif choice == 2: print("\nEnter a Number: ", end="") num = int(input()) print("\nResult =", square(num)) elif choice == 3: print("\nGreatest =", great(a, b)) else: print("\nInvalid Choice!")
The snapshot given below shows the sample run, with user input 3 as choice, 10 and 20 as two numbers:
« Previous Tutorial Next Tutorial »