Pass Optional or Keyword Parameters in Python Functions

AmitDiwan
Updated on 19-Sep-2022 12:29:10

1K+ Views

To pass optional or keyword parameters from one function to another, collect the arguments using the * and ** specifiers in the function’s parameter list But, at first, do know what are *args and **args in Python. Let us understand them − Variable-length/ Arbitrary arguments in Python (*args) Example When you don’t know in advance about the number of arguments to be passed, the arguments are variable-length. Include an asterisk i.e. * before the parameter name while defining the function. Let us see an example: def demo(*car): print("Car 1 = ", car[0]) print("Car 2 ... Read More

Why Default Values are Shared Between Objects in Python

AmitDiwan
Updated on 19-Sep-2022 12:24:28

2K+ Views

The default values concept in Python are based on using mutable or immutable objects. It is good programming practice to not use mutable objects as default values. Instead, use None as the default value to avoid issues. Immutable objects such as numbers, strings, tuples, and None, are safe from change. Changes to mutable objects such as dictionaries, lists, and class instances can lead to confusion. Let’s see the example of a Dictionary in a function and the issues with it and how to fix it. Problem We have a function. In that we have a Dictionary as a parameter with ... Read More

Best Practices for Using Import in a Python Module

AmitDiwan
Updated on 19-Sep-2022 12:19:22

2K+ Views

The import statement, just like any other statement or keyword in Python should be used and added to the code properly following the best practices. Let’s see them one by on − Multiple Imports Multiple Imports should usually be on separate lines. For example − import numpy import pandas import matplotlib Always on the Top Imports are always put at the top of the file i.e. After any module comments and docstrings Before module globals and constants. For example − # import the numpy module import numpy Import Modules in an Order A good practice is ... Read More

Share Global Variables Across Modules in Python

AmitDiwan
Updated on 19-Sep-2022 12:18:29

5K+ Views

To share global variable across module in Python, let us first understand what are global variables and its scope. Global Variable ExampleIf a variable is accessible from anywhere i.e. inside and even outside the function, it is called a Global Scope. Let’s see an example − # Variable i = 10 # Function def example(): print(i) print(i) # The same variable accessible outside the function # Calling the example() function example() # The same variable accessible outside print(i) Output 10 10 10 Share Information Across Modules Now, to share information across modules ... Read More

Python Lambdas in Loop Return Same Result

AmitDiwan
Updated on 19-Sep-2022 12:17:36

106 Views

Before understanding why Python lambdas defined in a loop with different values all return the same result, let us first learn about Lambda. Python Lambda The Lambda expressions allow defining anonymous functions. A lambda function is an anonymous function i.e. a function without a name. Let us see the syntax − lambda arguments: expressions The keyword lambda defines a lambda function. A lambda expression contains one or more arguments, but it can have only one expression. Example Let us see an example myStr = "Thisisit!" (lambda myStr : print(myStr))(myStr) Output Thisisit! Python lambdas defined in a loop ... Read More

Slash in Function Parameter List in Python

AmitDiwan
Updated on 19-Sep-2022 12:03:45

5K+ Views

A slash in the argument list of a function denotes that the parameters prior to it are positional-only. Let us first see a function in Python with a parameter − Function in Python Example Here, we are creating a basic function in Python with a parameter myStr − # Creating a Function def demo(myStr): print("Car =: ", myStr) # function call demo("BMW") demo("Tesla") Output Car =: BMW Car =: Tesla Slash in the parameter list of a function As said above, a slash in the argument list of a function denotes that the parameters ... Read More

Memory Management in Python

AmitDiwan
Updated on 19-Sep-2022 11:58:17

867 Views

Writing a memory-efficient and a code that executes quickly is what every developer wants while working on any programming language. In Python, memory allocation and deallocation is not manual, since Python has a Garbage Collector. Now, what is a Garbage Collector. Garbage Collector Garbage Collection is how the memory is freed when not use and how it can be made available for other objects. Python deletes the objects that are no longer in use. This is what we call Garbage Collection. The garbage collector initiates its execution with the program and is activated if the reference count drops to zero. ... Read More

Remove Index List from Another List in Python

Vikram Chiluka
Updated on 19-Sep-2022 11:03:49

3K+ Views

In this article, we will show you how to remove the index list elements from the original list using python. Now we see 2 methods to accomplish this task − Using pop() method Using del keyword Assume we have taken a list containing some elements. We will remove the index list elements from the main list using different methods as specified above. Note We must sort the indices list in descending order because removing elements from the beginning will change the indices of other elements, and removing another element will result in an incorrect result due to misplaced ... Read More

Join List of Lists in Python

Vikram Chiluka
Updated on 19-Sep-2022 10:38:44

29K+ Views

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In this article, we will show you how to join the list of lists(nested lists) using python. Now we see 4 methods to accomplish this task − Using nested for loop Using List comprehension Using sum() function Using NumPy module Assume we have taken a list of lists containing some elements. We will join those list of lists and return the result using different methods as specified above. Method 1: Using nested for loop Algorithm (Steps) Create a ... Read More

Append List to Second List in Python

Vikram Chiluka
Updated on 19-Sep-2022 09:30:17

2K+ Views

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. In this article, we are going to append the list to another list(Concatenate Lists) in Python. The following are the different methods to accomplish this task − Using Concatenation(+) Operator Using the append method of the list object Using extend() Method Using itertools.chain() Method Obtain a list without duplicates Assume we have taken a list containing some elements. We will return the concatenated list of the given ... Read More

Advertisements