Showing posts with label Python functions. Show all posts
Showing posts with label Python functions. Show all posts

Thursday, March 17, 2022

Difference Between Function and Method in Python

If you have started learning Python after working in Java for many years you would have wondered over one thing; while in Java you always call an enclosed group of statements doing a specific task a method, in Python you would have noticed use of both functions and methods. If you think both terms can be used interchangeably then you must know there is a subtle difference between function and method in Python and that’s what is the topic of this post.

Function Vs Method in Python

While both function and methods in Python are written the same way as given below-

def function_name(param1, param2, ..) :
 """function doc string"""
 function suite

How these two differ is that a function in Python is a group of statements that can be written individually in a Python program. You can call that function using its name and passing arguments if any.

Friday, January 15, 2021

Python Generator, Generator Expression, Yield Statement

In this article we’ll learn about generators in Python and how and why to use them.

The three things that you need to understand to master generators are-

  1. Generator functions itself.
  2. Generator expressions.
  3. Python yield statement

Python generator

Generator is a function which returns a generator iterator, where generator iterator is an object that can be iterated.

A generator function in Python looks like a normal function except that it contains yield statement rather than return (it may contain return too but yield statement is must to be qualified as a generator function).

Using yield statement generator generates a value that can be used in a for-loop or that can be retrieved one at a time with the next() function.

Thursday, January 14, 2021

Keyword Arguments in Python

Generally if you have a function with parameters, while calling the function you pass the arguments in the same positional order. There is also an option of keyword arguments in Python where arguments are passed as key, value pair, since the parameters are identified by their names so keyword arguments are also known as named arguments.

For example here is a function where the positional arguments are passed.

def display_amount(message, amount):
  print(message, '%.2f ' % amount)

display_amount("Total Amount is", 50.56)

Same function with keyword arguments can be written as-

def display_amount(message, amount):
  print(message, '%.2f ' % amount)

display_amount(message="Total Amount is", amount=50.56)

Default Arguments in Python

In Python, function arguments can have default value. If a function, that already has a default value, is called without the argument then the default value is used otherwise the passed value overrides the default value.

Default arguments in Python function example

In the function display_message() one of the argument has a default value. While calling the function you can chose not to pass that argument, in that case default value is used.

def display_message(name, msg='Hi '):
  print(msg + name)

display_message("Tom")

Output

Hi Tom

Wednesday, January 13, 2021

Functions in Python With Examples

In this post we’ll see what are functions, what are the advantages of creating functions, how to define functions in Python and how to execute functions in Python and return result, if any.

What are functions

A function is a block of code that is usually written to perform a specific functionality. A function is executed only when you call it and you can pass data to a function (known as function parameters) and return a result from a function.

Advantages of using functions

  1. Functions help you in writing modular code as you can write different functions for different functionality. So. Functions help you in dividing application into smaller chunks.
  2. Functions help in making your code reusable as you can call functions when required and use them in different applications too.
  3. Functions also avoid repetition of code as the functionality which is required at several places can be written as a separate function and that function can be called as and when required.
  4. Functions make your code easy to maintain as any extra functionality can be written as an extra function without disturbing the existing code base.
  5. Functions make it easy to debug code, since different functions are written for different tasks so it easy to pin-point which function has a problem.

Variable Length Arguments (*args), Keyword Varargs (**kwargs) in Python

In this article we’ll see what are variable length arguments (*args) in Python and what are keyword variable length arguments (**kwargs) in Python.


Variable length arguments in Python

A variable length argument as the name suggests is an argument that can accept variable number of values. To indicate that the function can take variable number of argument you write a variable argument using a ‘*’, for example *args.

Monday, January 11, 2021

Python Functions: Returning Multiple Values

In Python a function can return multiple values, that is one of the difference you will find in a Python function and function (or method) in a language like C or Java.

Returning multiple values in Python

If you want to return multiple values from a function you can use return statement along with comma separated values. For example-

return x, y, z

When multiple values are returned like this, function in Python returns them as a tuple. When assigning these returned values to a variable you can assign them to variables in sequential order or in a tuple. Let’s try to clarify it with examples.