Example In this article, we will see if you will change a list, let’s say List y will also change list x. For this, let us first see an example with two lists and try to append() and print − x = [] y = x print("Value of y = ", y) print("Value of x = ", x) y.append(25) print("After changing...") print("Value of y = ", y) print("Value of x = ", x) Output ('Value of y = ', []) ('Value of x = ', []) After changing... ('Value of y = ', [25]) ('Value of x ... Read More
The concept of arguments and parameters are part of Functions in Python. Therefore, before moving further let us learn how to create a function and parameterised function. A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Create a Function Example Let us create a basic function − # Define a function def sample(): print("Inside a Function") # Function call sample() Output Inside a Function Create a Parameterized Function Here, we are ... Read More
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 will show you how to reverse the objects/elements in a list using python. The following are the 4 different methods to accomplish this task − Using built-in reverse() method Using the builtin reversed() method Using Slicing Using For Loop, pop() and append() Functions Assume we have taken a list containing some elements. We will return a new list after reversing all the items ... Read More
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
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
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP