Found 10784 Articles for Python

Namespaces and Scoping in Python

Mohd Mohtashim
Updated on 30-Jan-2020 06:28:56

416 Views

Variables are names (identifiers) that map to objects. A namespace is a dictionary of variable names (keys) and their corresponding objects (values).A Python statement can access variables in a local namespace and in the global namespace. If a local and a global variable have the same name, the local variable shadows the global variable.Each function has its own local namespace. Class methods follow the same scoping rule as ordinary functions.Python makes educated guesses on whether variables are local or global. It assumes that any variable assigned a value in a function is local.Therefore, in order to assign a value to a global variable within a ... Read More

Locating Modules in Python

Mohd Mohtashim
Updated on 30-Jan-2020 06:28:14

1K+ Views

When you import a module, the Python interpreter searches for the module in the following sequences −The current directory.If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/.The module search path is stored in the system module sys as the sys.path variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation-dependent default.The PYTHONPATH VariableThe PYTHONPATH is an environment variable, consisting of a list of directories. The syntax of PYTHONPATH is the same as that of the shell variable PATH.Here is a ... Read More

The import Statements in Python

Mohd Mohtashim
Updated on 30-Jan-2020 06:36:06

3K+ Views

You can use any Python source file as a module by executing an import statement in some other Python source file.SyntaxThe import has the following syntax −import module1[, module2[, ... moduleN]When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module support.py, you need to put the following command at the top of the script −#!/usr/bin/python # Import module support import support # Now you can call defined function that module ... Read More

The return Statement in Python

Sarika Singh
Updated on 19-Dec-2022 12:09:39

3K+ Views

The return statement in python is an extremely useful statement used to return the flow of program from the function to the function caller. The keyword return is used to write the return statement. Since everything in python is an object the return value can be any object such as – numeric (int, float, double) or collections (list, tuple, dictionary) or user defined functions and classes or packages. The return statement has the following features - Return statement cannot be used outside the function. Any code written after return statement is called dead code as it will never ... Read More

The Anonymous Functions in Python

Mohd Mohtashim
Updated on 30-Jan-2020 06:20:22

2K+ Views

These functions are called anonymous because they are not declared in the standard manner by using the def keyword. You can use the lambda keyword to create small anonymous functions.Lambda forms can take any number of arguments but return just one value in the form of an expression. They cannot contain commands or multiple expressions.An anonymous function cannot be a direct call to print because lambda requires an expressionLambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace.Although it appears that lambda's are a one-line version of a function, they ... Read More

Variable-length arguments in Python

Mohd Mohtashim
Updated on 30-Jan-2020 06:19:40

6K+ Views

You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments.SyntaxSyntax for a function with non-keyword variable arguments is this −def functionname([formal_args, ] *var_args_tuple ): "function_docstring" function_suite return [expression]An asterisk (*) is placed before the variable name that holds the values of all nonkeyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call.Example Live Demo#!/usr/bin/python # Function definition is here def printinfo( arg1, *vartuple ): "This prints a variable passed arguments" ... Read More

Required arguments in Python

Mohd Mohtashim
Updated on 30-Jan-2020 06:14:12

6K+ Views

Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition.To call the function printme(), you definitely need to pass one argument, otherwise it gives a syntax error as follows −Example Live Demo#!/usr/bin/python # Function definition is here def printme( str ): "This prints a passed string into this function" print str return; # Now you can call printme function printme()OutputWhen the above code is executed, it produces the following result −Traceback (most recent call last): File "test.py", line 11, in printme(); TypeError: ... Read More

Pass by reference vs value in Python

Mohd Mohtashim
Updated on 29-Jan-2020 11:11:15

7K+ Views

All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.Example Live Demo#!/usr/bin/python # Function definition is here def changeme( mylist ): "This changes a passed list into this function" mylist.append([1, 2, 3, 4]); print "Values inside the function: ", mylist return # Now you can call changeme function mylist = [10, 20, 30]; changeme( mylist ); print "Values outside the function: ", mylistOutputHere, we are maintaining reference of the passed object and appending values in the same object. ... Read More

Calling a Function in Python

Mohd Mohtashim
Updated on 29-Jan-2020 11:10:26

289 Views

Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code.Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is the example to call printme() function − Live Demo#!/usr/bin/python # Function definition is here def printme( str ): "This prints a passed string into this function" print str return; # Now you can call printme function printme("I'm first call to user defined function!") printme("Again second call to the same function")OutputWhen ... Read More

The calendar Module in Python

Mohd Mohtashim
Updated on 29-Jan-2020 11:09:25

465 Views

The calendar module supplies calendar-related functions, including functions to print a text calendar for a given month or year.By default, calendar takes Monday as the first day of the week and Sunday as the last one. To change this, call calendar.setfirstweekday() function.Here is a list of functions available with the calendar module −Sr.NoFunction with Description1calendar.calendar(year, w=2, l=1, c=6)Returns a multiline string with a calendar for year year formatted into three columns separated by c spaces. w is the width in characters of each date; each line has length 21*w+18+2*c. l is the number of lines for each week.2calendar.firstweekday( )Returns the current setting ... Read More

Advertisements