Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Why do Python lambdas defined in a loop with different values all return the same result?
Before understanding why Python lambdas defined in a loop with different values all return the same result, let us first learn about Lambda expressions and the concept of late binding closures. Python Lambda 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 ? ...
Read MoreWhat does the slash(/) in the parameter list of a function mean in Python?
The slash (/) in a function's parameter list marks the boundary between positional-only parameters and other parameters. Parameters before the slash can only be passed by position, not as keyword arguments. Basic Function Example Let's first understand a regular Python function with parameters ? # Creating a Function def demo(car_name): print("Car:", car_name) # Function calls demo("BMW") demo("Tesla") Car: BMW Car: Tesla Positional-Only Parameters with Slash The slash (/) denotes that parameters before it are positional-only. Arguments are mapped to parameters based solely on their position. ...
Read MoreHow do I modify a string in place in Python?
Strings in Python are immutable, meaning you cannot modify them in place. However, you can create new strings or use mutable alternatives like io.StringIO and the array module for in-place modifications. Why Strings Cannot Be Modified In Place When you try to change a string character, Python creates a new string object rather than modifying the original ? text = "Hello" print("Original:", text) print("ID:", id(text)) # This creates a new string, doesn't modify the original text = text.replace('H', 'J') print("Modified:", text) print("New ID:", id(text)) Original: Hello ID: 140712345678912 Modified: Jello New ID: ...
Read MoreHow can my code discover the name of an object in Python?
In Python, objects don't have inherent names − variable names are just labels that point to objects in memory. When multiple variables reference the same object, there's no way to determine which variable name was used to create it. Why Objects Don't Have Names Consider this example where both ob1 and ob2 reference the same object ? # Creating a Demo Class class Demo: pass # Multiple references to the same object ob1 = Demo() ob2 = ob1 print("ob1 identity:", id(ob1)) print("ob2 identity:", id(ob2)) print("Same object?", ob1 is ob2) ...
Read MoreHow do I get a list of all instances of a given class in Python?
Python provides several ways to get a list of all instances of a given class. The most common approaches use the gc module or the weakref module. The gc module is part of Python's standard library and doesn't need separate installation. Using the gc Module The gc (garbage collector) module allows you to access all objects tracked by Python's garbage collector. You can filter these to find instances of a specific class ? import gc # Create a class class Demo: pass # Create four instances ob1 = Demo() ob2 ...
Read MoreHow do I use strings to call functions/methods in Python?
Python functions are generally called using their name. However, you can also use strings to call functions dynamically. This is useful when the function name is determined at runtime or stored in variables. Using locals() and globals() The locals() function returns a dictionary of local variables, while globals() returns global variables. You can use these dictionaries to call functions by name ? def demo1(): print('Demo Function 1') def demo2(): print('Demo Function 2') # Call functions using string names locals()['demo1']() globals()['demo2']() Demo Function 1 ...
Read MoreHow do I convert a string to a number in Python?
Python provides several built-in functions to convert strings to numbers. The most common methods are int() for integers and float() for decimal numbers. Using int() for Integer Conversion The int() function converts a string containing digits to an integer ? # String to be converted my_str = "200" # Display the string and its type print("String =", my_str) print("Type =", type(my_str)) # Convert the string to integer using int() my_int = int(my_str) print("Integer =", my_int) print("Type =", type(my_int)) String = 200 Type = Integer = 200 Type = ...
Read MoreWhat are the best Python resources?
Learning Python effectively requires access to quality resources across different formats and skill levels. This guide covers the best official documentation, tutorials, and specialized learning paths to help you master Python programming. Python Official Documentation The official Python documentation remains the most authoritative and comprehensive resource for learning Python. These resources provide everything from beginner guides to advanced implementation details ? Beginner's Guide − https://wiki.python.org/moin/BeginnersGuide Developer's Guide − https://devguide.python.org/ Free Python Books − https://wiki.python.org/moin/PythonBooks Python Standard Library − https://docs.python.org/3/library/index.html Python HOWTOs − https://docs.python.org/3/howto/index.html Python Video Talks − https://pyvideo.org/ Comprehensive Tutorial Resources Beyond official ...
Read MoreWhy are there separate tuple and list data types in Python?
Python provides both tuple and list data types because they serve different purposes. The key difference is that tuples are immutable (cannot be changed after creation), while lists are mutable (can be modified). This fundamental distinction makes each suitable for different scenarios. Tuples use parentheses () and are ideal for storing data that shouldn't change, like coordinates or database records. Lists use square brackets [] and are perfect when you need to add, remove, or modify elements frequently. Creating a Tuple Tuples are created using parentheses and can store multiple data types ? # Creating ...
Read MoreHow can I find the methods or attributes of an object in Python?
To find the methods or attributes of an object in Python, you can use several built-in functions. The getattr() method retrieves attribute values, hasattr() checks if an attribute exists, and setattr() sets attribute values. Additionally, dir() lists all available attributes and methods. Using getattr() to Access Attributes Example The getattr() function retrieves the value of an object's attribute ? class Student: st_name = 'Amit' st_age = '18' st_marks = '99' def demo(self): ...
Read More