Python Articles

Page 830 of 855

How to call Python module from command line?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 265 Views

To call a Python module from the command line, you can use the python command followed by flags like -m or -c, or run the script file directly. This article covers the most common approaches with practical examples. Running a Module with python -m The -m flag tells Python to run a module by its module name (without the .py extension). Python searches sys.path for the module and executes its __main__ block − ~$ python -m module_name For example, to run the built-in json.tool module for formatting JSON − ~$ echo '{"name":"Alice"}' | python -m json.tool { ...

Read More

How to run Python functions from command line?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 2K+ Views

To run Python functions from the command line, you save the function in a .py file and then invoke it using the Python interpreter. There are several approaches − using sys.argv, the -c flag, or the argparse module. Using sys.argv with __name__ Guard The most common approach is to use sys.argv to read command-line arguments and the if __name__ == "__main__" guard to call your function when the script is executed directly. The first item in sys.argv is the script name, and subsequent items are the arguments passed. Note that all command-line arguments are received as strings, so you must ...

Read More

Are Python functions objects?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 443 Views

Yes, Python functions are full objects. Python creates function objects when you use a def statement or a lambda expression. Like any other object, functions can be assigned to variables, passed as arguments, returned from other functions, and even have custom attributes. Functions Have a Type and Support Attributes Since functions are objects, they have a type (function) and you can assign custom attributes to them ? Example def foo(): pass foo.score = 20 print(type(foo)) print(foo.score) print(type(lambda x: x)) The output of the above code is ? 20 ...

Read More

How can we define a Python function at runtime?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 130 Views

We can define a python function and execute it at runtime by importing the types module and using its function types.FunctionType() as follows This code works at the python prompt as shown. First we import the types module. Then we run the command dynf=…; then we call the function dynf() to get the output as shown import types dynf = types.FunctionType(compile('print("Really Works")', 'dyn.py', 'exec'), {}) dynf() Output Really Works Defining a Python function at runtime can be useful in a variety of situations, such as when you need to ...

Read More

How can we create recursive functions in Python?

Sarika Singh
Sarika Singh
Updated on 12-Mar-2026 4K+ Views

Recursion is a process where a function calls itself repeatedly with new input values, slowly moving toward a condition where it stops. This stopping condition is called the base case. It prevents the function from running endlessly and allows it to return a final result. In Python, we can create recursive functions by simply defining a function that calls itself. Every recursive function has two main components − Recursive Case: This is when the function calls itself to solve a smaller part of the problem. It keeps the process going. Base Case: This is the stopping point. It tells ...

Read More

How to get a list of parameter names inside Python function?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 3K+ Views

To get a list of parameter names inside a Python function, you can use the inspect module. This module provides several functions that let you examine the properties of Python objects, including function signatures, parameter names, and default values. Using inspect.getfullargspec() The inspect.getfullargspec() function returns a named tuple containing information about a function's parameters, including argument names, variable args, keyword args, and default values ? Example import inspect def aMethod(arg1, arg2): pass print(inspect.getfullargspec(aMethod)) def foo(a, b, c=4, *arglist, **keywords): pass print(inspect.getfullargspec(foo)) The output of the above ...

Read More

How to wrap python object in C/C++?

Gireesha Devara
Gireesha Devara
Updated on 12-Mar-2026 705 Views

To wrap existing C or C++ functionality in Python, there are number of options available, which are: Manual wrapping using PyMethodDef and Py_InitModule, SWIG, Pyrex, ctypes, SIP, Boost.Python, and pybind1. Using the SWIG Module Let’s take a C function and then tune it to python using SWIG. The SWIG stands for “Simple Wrapper Interface Generator”, and it is capable of wrapping C in a large variety of languages like python, PHP, TCL etc. Example Consider simple factorial function fact() in example.c file. /* File : example.c */ #include // calculate factorial int fact(int n) { ...

Read More

Swap Even Index Elements And Odd Index Elements in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 3K+ Views

Suppose we have a list of numbers called nums, we will exchange each consecutive even indexes with each other, and also exchange each consecutive odd index with each other.So, if the input is like [1,2,3,4,5,6,7,8,9], then the output will be [3, 4, 1, 2, 7, 8, 5, 6, 9]To solve this, we will follow these steps −length := size of numsfor i in range 0 to length, increase by 4, doif i+2

Read More

Python Program for Counting Sort

Pavitra
Pavitra
Updated on 11-Mar-2026 273 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement− We are given an array, we need to sort the array using the concept of counting sort.Counting sort is a technique in which we work on keys between a specific range. It involves counting the number of objects which have distinct key & values. Finally, we do arithmetic calculations to obtain the position of each object and display the output.Now let’s observe the solution in the implementation below −Exampledef countSort(arr):    # The output character array that will have sorted arr    output = ...

Read More

Python Program for Cycle Sort

Pavitra
Pavitra
Updated on 11-Mar-2026 351 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of cycle sort.It is an in-place algorithm and swapping takes place by the formation of cycles.Now let’s observe the solution in the implementation below −Exampledef cycleSort(array):    writes = 0    # cycles to be rotated    for cycleStart in range(0, len(array) - 1):       item = array[cycleStart]       #position to place the item       pos = cycleStart       for i in range(cycleStart ...

Read More
Showing 8291–8300 of 8,546 articles
« Prev 1 828 829 830 831 832 855 Next »
Advertisements