In Python, -22 // 10 returns -3 because of how floor division works with negative numbers. The // operator always rounds down toward negative infinity, not toward zero. Understanding Floor Division Floor division (//) returns the largest integer less than or equal to the division result. For positive numbers, this behaves as expected, but with negative numbers, it rounds away from zero toward negative infinity. Syntax a // b Where a and b are the dividend and divisor respectively. Floor Division with Positive Numbers a = 22 b = 10 ... Read More
Python allows writing obfuscated one-liners using lambda functions, functional programming constructs, and advanced techniques. These create compact but hard-to-read code that can perform complex operations in a single line. Understanding Lambda Functions Lambda expressions define anonymous functions without a name. The syntax is ? lambda arguments: expression A lambda can have multiple arguments but only one expression. Simple Lambda Example message = "Hello World!" (lambda text: print(text))(message) Hello World! Using functools.reduce for Complex Operations The reduce() function from functools applies a function cumulatively to items ... Read More
Yes, Python has an equivalent to C's ternary operator ?:. Python's conditional expression provides a concise way to choose between two values based on a condition. C Language Ternary Operator First, let's see how C's ternary operator works − #include int main() { int x = 10; int y; y = (x == 1) ? 20 : 30; printf("Value of y = %d", y); y = (x == 10) ? 20 : 30; printf("Value ... Read More
When your Python program runs slowly, several optimization techniques can significantly improve performance. Here are the most effective approaches to speed up your code. Avoid Excessive Abstraction Minimize unnecessary abstraction layers, especially tiny functions or methods. Each abstraction creates indirection that forces the interpreter to work harder. When indirection overhead exceeds useful work, your program slows down. Reduce Looping Overhead For simple loop bodies, the interpreter overhead of the loop itself can be substantial. The map() function often performs better, but requires the loop body to be a function call. Traditional Loop Example Here's ... Read More
In Python, when you assign one list variable to another using y = x, both variables point to the same object in memory. This means modifying one list will affect the other, which often surprises beginners. Example: Lists Share the Same Reference Let's see what happens when we assign one list to another and then modify it − 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) Value of y = [] ... Read More
The concepts of arguments and parameters are fundamental parts of functions in Python. Understanding their difference is crucial for writing effective Python code. A parameter is a variable listed inside the parentheses in the function definition. An argument is the actual value passed to the function when it is called. Basic Function Example Let's start with a simple function to understand the basics ? # Define a function def greet(): print("Hello, World!") # Function call greet() Hello, World! Function with Parameters Here's a function ... Read More
Default values in Python are created only once when a function is defined, not each time the function is called. This behavior can cause unexpected issues when using mutable objects (like lists or dictionaries) as default parameters. Understanding this concept is crucial for writing reliable Python code. The Problem with Mutable Defaults When you use a mutable object as a default parameter, all function calls share the same object. Here's a demonstration of the problem: def add_item(item, target_list=[]): target_list.append(item) return target_list # First call result1 = add_item("apple") ... Read More
The import statement is fundamental to Python programming, but following best practices ensures clean, maintainable code. Here are the essential guidelines for using imports effectively in your Python modules. Multiple Imports on Separate Lines Each module should be imported on its own line for better readability ? # Good practice import numpy import pandas import matplotlib.pyplot print("Modules imported successfully") Modules imported successfully However, importing multiple items from the same module on one line is acceptable ? from os import path, getcwd, listdir print("Current directory:", getcwd()) ... Read More
To share global variables across modules in Python, you need to understand global variable scope and create a shared configuration module. This approach allows multiple modules to access and modify the same variables. Global Variable Scope A global variable is accessible from anywhere in the program − both inside and outside functions. Here's how global variables work ? # Global variable counter = 10 # Function accessing global variable def display_counter(): print(counter) # Accessing global variable outside function print(counter) # Calling the function display_counter() # Accessing global variable ... 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 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 More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance