- Python Basics
- Python Home
- Python History
- Python Applications
- Python Features
- 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 & 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 & Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes & Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class Vs Static Method
- Python @property Decorator
- Python Regular Expressions
- Python CGI Programming
- Python Network Programming
- Python Send E-mail
- Python Multi-threading
- Python XML Processing
- Python MySQL Database
- Python GUI Programming
- Python Event Handling
- Python Keywords
- Python All 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 Test
- Python Online Test
- Give Online Test
- All Test List
Python return Keyword/Statement
The 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 codescracker(a, b): return a+b codes() x = cracker() print(x) x = codescracker(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 »