i
Python functionality and evolution
Installing Python (windows and Ubuntu)
IDLE (Integrated Development and Learning Environment)
Write the first python program and execute it
Keywords in Python
Identifiers in Python
Indentation in Python
Comments in Python
Multi-line Comments in Python
Getting user input
Python Function
Calling a function
Arguments in Function
Scope of Variables
Modules in Python
The PYTHON PATH-Variable
Python File Open
Python File Opening Modes
The file-Object-Attributes
Python File Close() Method
Python File Read/Write
Python File Position
Renaming and Deleting Files in Python
Python Directory Methods
File/Directory Methods
Exceptions in Python
The try-finally Clause
The argument of Exception
Raising Exceptions
Python Built-in Exceptions
Python OOPs Concepts
Python Classes/Objects
Creating Instance Objects
Accessing Attributes
Built-In Class Attributes
Garbage Collection
Python Inheritance
Overriding Methods
Method overriding
Data Hiding
Regular Expression
The match function
The search function
Match Object Methods & Description
Matching or Searching
Search or Replace
Regular Expression Modifiers / Option Flags
Grouping with Parentheses
Python Socket Programming
Python Socket Server
Python Socket Client
Send Data Between Clients
Python Socket or Server
Python Socket Clients
Points to ponder
Calling function by using following formal arguments:
• Required-arguments
• Keyword-arguments
• Default-arguments
• Variable length arguments
Required-arguments
Required-arguments are the arguments passed to function in corrected positional order. Number of arguments in the function should match the exact function definitions.
Calling function printm(), you definitely should pass one argument and it gives syntax error shown as below:
Live Demo
Output of above code is as follows:
Error traceback(most of recent call last):
file "test.py" line 10 in
printm();
TypeError: printm() takes exactly 1 argument (0 given)
Keyword-arguments
Keyword-arguments are related to function calls. Using keyword-arguments in function call, caller identifies arguments by parameter name.
Allows you to skip argument or place them out of order to the python interpreter is able to use the keywords to match values in parameter. You can make keyword calls also to the printm() function in following ways:
Live Demo
Output of the above code:
My string
Below example shows order of parameters does not matter.
Live Demo
Output:
EName: miki
eage 50
Default-arguments
Default-argument is an argument that assumes a default value if value is not provided in the function call for that argument. Please check below example where you get idea of default arguments:
Live Demo
Output:
eName: miki
eAge 50
eName: miki
eAge 35
Variable-length-arguments
You need to process the function for more arguments than you specified defining the function. This type of arguments are called variable-length-arguments and are not named in the function definition.
Below is the syntax of non-keyword-variable-arguments:
Asterisk (*) is placed before the variable name which holds the values of all non-keyword variable arguments. This type of tuple remains empty if no additional arguments are specified during the function call. Below is the example:
Live Demo
Output:
10
70
60
50
The Anonymous-Functions
This type of functions are called anonymous because they are not declared in the standard manner by def keyword. For that we can use the lambda keyword to create small anonymous-functions.
• The Lambda-function may take any number of arguments but return just one value in the form of expression. There cannot be any commands or multiple expression.
• An anonymous-function cannot be a direct-call to print because lambda-function requires an expression
• The Lambda-functions generally have their own local namespace and cannot access variables other than those in parameter list and in the global namespace.
• The Lambda are a one-line version of a function, they are not equivalent to inline statements in C or C++.
Syntax
Below is the syntax of the Lambda-functions contain only a single string:
lambda[argu1 [,argu2,.....argu]]:expression
Live Demo
Output:
total : 30
total : 40
The return-Statement
The return-statement return [expression] exits function, optionally passing back an expression to the caller. Return-statement with no arguments is the same as return-None.
Live Demo
Output:
In the function : 30
Out the function : 30
Don't miss out!