TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python Return Keyword (Return Multiple Values)

Use the return keyword to return values from methods. Return multiple values in a tuple.
Return. This is a Python keyword. We use return to signal the end of a method. We can place one or more return values in a method—they are executed when encountered.Built-ins
In Python, we find different terms for things like "void" methods. And for "no value" we have the special None value. Tuples can return multiple things at once.
No return value. Methods in Python do not need to return a value. If we use the "return" statement alone, no value is returned. This is called a void method in other languages.

Note: Clarity of code is important. Sometimes, having more symmetry in a method (where "return" is used for all paths) is a clearer design.

Python program that returns no value def printname(first, middle, last): # Validate middle initial length. if len(middle) != 1: print("Middle initial too long") return # Display. print(first + " " + middle + ". " + last) # Call method. printname("Jake", "R", "Chambers") Output Jake R. Chambers
Return none. When a method has no return value specified, and it terminates, the None value is returned. We can test for None. Here, example() returns None unless "x" is greater than zero.None
Python program that returns None def example(x): # Return a value only if argument is greater than zero. if x > 0: return x print(example(0)) print(example(1)) Output None 1
Return multiple values. A tuple is a small container for more than one value. We can return 2 or more values from a method at once with a tuple. These values can be unpacked.Tuple
Python program that returns multiple values def get_names_uppercase(a, b): # This method returns 2 strings in a tuple. return (a.upper(), b.upper()); # Get tuple from the method. result = get_names_uppercase("vidiadhar", "naipaul") print(result) # Extract first and second return values. first = result[0] second = result[1] print(first) print(second) Output ('VIDIADHAR', 'NAIPAUL') VIDIADHAR NAIPAUL
A summary. The return keyword is an essential part of Python programs. With it we direct the flow of control. We can return multiple values with a special type like a tuple.Def
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf