Found 10805 Articles for Python

How to expand tabs in string to multiple spaces in Python?

Sarika Singh
Updated on 23-Nov-2022 07:49:30

2K+ Views

The handling of white spaces between strings is managed effectively in Python. In this article, we'll examine the methods to provide a space in the string when we are unsure of how much space is necessary. Using string expandtabs() method A built-in method in Python called String expandtabs() is used to handle spaces in strings. The expandtabs() method in Python creates a copy of the string that has all tab characters '\t' up to the next multiple tabsize parameters replaced with whitespace characters. There may be instances when it is necessary to define how much space should be left in ... Read More

Which is more fundamental between Python functions and Python object-methods?

Manogna
Updated on 13-Feb-2020 06:40:52

83 Views

A function is a callable object in Python, i.e. can be called using the call operator. However other objects can also emulate a function by implementing __call__method.  exampledef a(): pass # a() is an example of function print a print type(a)OutputC:/Users/TutorialsPoint/~.py A method is a special class of function, one that can be bound or unbound.Exampleclass C:       def c(self): pass print C.c   # example of unbound method print type(C.c) print C().c  # example of bound method print type(C().c) print C.c()Of course, an unbound method cannot be called without passing as argument.Output ... Read More

Why would you use the return statement in Python?

Rajendra Dharmkar
Updated on 02-May-2023 14:00:23

3K+ Views

The return statement in Python is used to return a value or a set of values from a function. When a return statement is encountered in a function, the function execution is stopped and the value specified in the return statement is returned to the caller. The main purpose of the return statement is to pass data from a function back to the caller. The caller can then use this data for further processing. Why use the return statement? The return statement is useful in many situations. Some common use cases include − Passing data back to the caller: As ... Read More

How variable scope works in Python function?

Sarika Singh
Updated on 19-Dec-2022 12:01:30

209 Views

In this article we will discuss about how a variable scope works in Python function. The scope of a name is the area of a program in which you can clearly access a name, such as variables. Hence, a variable scope is the region in which we can access a variable as shown in the following example - def multiplication(): product = 7*9 Here, the “product” variable is created inside the function, therefore it can only be accessed within it. In general, the python scope concept is presented using the rule called as the ... Read More

How to use *args and **kwargs in Python?

Sarika Singh
Updated on 19-Dec-2022 11:57:47

1K+ Views

In this article we will discuss about *args and **kwargs properties and their usage in Python. To pass unspecified number of arguments to a function usually *args and **kwargs properties are used. The *args property is used to send a non-keyword variable length argument to a function call whereas **kwargs keyword is used to pass a keyword length of arguments to a function. Keyword arguments (or arguments) are the values that is preceded by an identifier “=” in a function call for e.g. “name=”. The primary benefits of using *args and **kwargs are readability and convenience, but ... Read More

What is an anonymous function in Python?

Sarika Singh
Updated on 19-Dec-2022 12:35:50

18K+ Views

An anonymous function in Python is one that has no name when it is defined. In Python, the lambda keyword is used to define anonymous functions rather than the def keyword, which is used for normal functions. As a result, lambda functions are another name for anonymous functions. Syntax Following is the syntax of the lambda function - lambda [args] : expression Although a lambda function can have only one expression, it can have any number of arguments. A lambda can also be immediately invoked and is written in a single line of code. Example: Calling a lambda function ... Read More

How to use Lambda Function in Python?

Pranathi M
Updated on 16-Sep-2022 07:24:05

2K+ Views

The anonymous nature of Python Lambda Functions indicates that they lack a name. As we already know, a standard Python function is defined using the def keyword. Similar to this, the lambda keyword is utilized in Python to declare an anonymous function. Syntax The syntax of the lambda expression in python is as follows − lambda arguments: expression There is only one expression that is evaluated and returned in this function, regardless of the number of parameters. Lambda functions can be used anywhere that function objects are required. The fact that lambda functions are syntactically limited to a single ... Read More

Can Python functions run in html as javascript does?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20

160 Views

It is not possible to run Python in any modern browser because none contain a python interpreter. Javascript is the only language which runs in a browser without plugins like Flash or ActiveX.One way to write Python code that will run in the browser is to use a "transpiler". This is a tool that will compile the python code into Javascript. So the browser is ultimately running the language it knows, but you're writing Python. There are already a number of languages like CoffeeScript, TypeScript and even React JSX templates that compile down to raw javascript.An example of a Python ... Read More

How to write recursive Python Function to find factorial?

Rajendra Dharmkar
Updated on 02-May-2023 13:49:46

428 Views

Factorial of a number is the product of all the positive integers from 1 to that number. For example, the factorial of 4 is 4*3*2*1 = 24. To find the factorial of a number using recursive Python function, we can define a function that calls itself with a smaller input until it reaches the base case, which is the factorial of 1, which is 1. Here is the code to find the factorial of a number using recursive Python function − def factorial(n): if n == 1: return 1 ... Read More

How to pass Python function as a function argument?

Rajendra Dharmkar
Updated on 02-May-2023 12:32:13

18K+ Views

In Python, you can pass a function as an argument to another function. This is known as a higher-order function. In other words, a function is called a higher-order function if it contains other functions as parameters or returns functions. Python supports the usage of higher-order functions as it is a very versatile programming language. Few examples of higher-order functions in Python are map(), filter(), sorted(), and reduce(). For example the function map() applies a function to all the items in an input list and returns a new list containing all the function call results and similarly other higher order ... Read More

Advertisements