Articles on Trending Technologies

Technical articles with clear explanations and examples

How do I get int literal attribute instead of SyntaxError in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 460 Views

When trying to access attributes of integer literals directly in Python, you might encounter a SyntaxError: invalid decimal literal. This happens because Python's parser interprets the dot as part of a float literal. To fix this, use a space before the dot or wrap the integer in parentheses. Understanding Numeric Literals Python supports several numeric literal types: int (signed integers) − Positive or negative whole numbers with no decimal point float (floating point real values) − Real numbers with a decimal point, may use scientific notation complex (complex numbers) − Numbers of the form a + ...

Read More

Why does -22 // 10 return -3 in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 589 Views

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

How it is possible to write obfuscated oneliners in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 485 Views

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

Is there an equivalent of C's "?:" ternary operator in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 586 Views

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

My Python program is too slow. How do I speed it up?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 446 Views

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

Why did changing list 'y' also change list 'x' in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 472 Views

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

What is the difference between arguments and parameters in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

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

Why are default values shared between objects in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

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

What are the "best practices" for using import in a Python module?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

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

How do I share global variables across modules in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 6K+ Views

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
Showing 2041–2050 of 61,298 articles
« Prev 1 203 204 205 206 207 6130 Next »
Advertisements