Programming Articles

Found 25,445 articles

C++ Program to Find GCD

Samual Sam
Samual Sam
Updated on 12-Mar-2026 15K+ Views

The Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them without leaving a remainder. For example, let's say we have two numbers 45 and 27 − 45 = 5 * 3 * 3 27 = 3 * 3 * 3 The common factors are 3 and 3, so the GCD of 45 and 27 is 9. Using Euclidean Algorithm (Modulo Method) The Euclidean algorithm finds the GCD by repeatedly replacing the larger number with the remainder of dividing the two numbers, until the remainder becomes 0. At that point, the other ...

Read More

Python program to count the elements in a list until an element is a Tuple?

AmitDiwan
AmitDiwan
Updated on 12-Mar-2026 675 Views

In this article, we will count the elements in a list until an element is a Tuple. A Python list can contain items of different types, including tuples. The goal is to iterate through the list and count how many elements appear before the first tuple is encountered. For example, given the following list − mylist = [25, 50, 75, 100, 125, (20, 175, 100, 87), 500] There are 5 elements (25, 50, 75, 100, 125) before the first tuple (20, 175, 100, 87), so the count is 5. Using isinstance() The isinstance() function checks whether an object ...

Read More

C++ Program to Find All Roots of a Quadratic Equation

karthikeya Boyini
karthikeya Boyini
Updated on 12-Mar-2026 9K+ Views

A quadratic equation is in the form ax2 + bx + c = 0. The roots of the quadratic equation are given by the following formula − x = −b ± √ b² − 4ac 2a The value b2 − 4ac is called the discriminant. It determines the nature of the roots. There are three cases − b2 > 4ac − The roots are real and different. b2 = 4ac − The roots are real and both roots are the same. b2 ...

Read More

Python program to find the number occurring odd number of times using Lambda expression and reduce function

AmitDiwan
AmitDiwan
Updated on 12-Mar-2026 6 Views

We can find the number occurring an odd number of times in a list using a Lambda expression and the reduce() function from the functools module. The approach uses the XOR (^) bitwise operator − XORing a number with itself cancels it out (returns 0), so after XORing all elements, only the number that appears an odd number of times remains. For example, given the following input − ele = [10, 20, 20, 30, 40, 20, 30, 10, 40] The output is 20, because 20 occurs three times (an odd count), while all other integers appear an even ...

Read More

Difference between attributes and properties in python?

Sarika Singh
Sarika Singh
Updated on 12-Mar-2026 8K+ Views

In Python, everything is an object, and every object has attributes and methods. Attributes are data variables that describe the object (such as name, age, or height), while properties are a special kind of attribute that use getter, setter, and deleter methods to control access to the underlying data. Attributes in Python Attributes are variables that belong to an object. They are usually defined in the __init__ method and accessed using dot notation ? Example class Dog: def __init__(self, name, age): self.name = name ...

Read More

Saving Python functions in a Sqlite table

Sarika Singh
Sarika Singh
Updated on 12-Mar-2026 351 Views

In Python, you can store a function as a text string in a SQLite database using the sqlite3 module. You cannot run the function directly from the database, but you can retrieve the code later and execute it using Python's exec() or eval() functions. This is helpful when you want to store or update function logic, like in code editors, plugins, or apps that run user-created scripts. Using sqlite3 and exec() Function The sqlite3 module is used to create and work with SQLite databases in Python. The exec() function can run Python code that is stored as a string, including ...

Read More

Why Python functions are hashable?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 175 Views

An object in Python is hashable if it has a hash value that remains the same during its lifetime. It must have a __hash__() method and be comparable to other objects via __eq__(). If two hashable objects are equal when compared, they have the same hash value. Being hashable makes an object usable as a dictionary key and a set member, since these data structures use hash values internally. All immutable built-in objects in Python are hashable. Mutable containers like lists and dictionaries are not hashable, while immutable containers like tuples are. Objects that are instances of user-defined classes are ...

Read More

The return Statement in Python

Sarika Singh
Sarika Singh
Updated on 12-Mar-2026 4K+ Views

The return statement in Python is used to send a value back from a function to its caller and exit the function. Since everything in Python is an object, the return value can be any object such as numeric types (int, float), collections (list, tuple, dict), or even other functions. Key features of the return statement − It cannot be used outside a function. Any code written after a return statement within the same block is dead code and will never be executed. If no value is specified, None is returned implicitly. Syntax def some_function(parameters): ...

Read More

What is an anonymous function in Python?

Sarika Singh
Sarika Singh
Updated on 12-Mar-2026 23K+ Views

Anonymous Function in Python An anonymous function is a function which doesn't have a name. We can return a value from an anonymous function, call it at the time of creation or, assign it to a variable. Instead of using the def keyword, which is used for regular functions, anonymous functions are defined using the lambda keyword. Hence, they are commonly referred to as lambda functions. Syntax Following is the syntax to create anonymous functions using the lambda keyword - lambda [args] : expression Although a lambda function can have only one expression, it can have any number ...

Read More

How to call Python module from command line?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 229 Views

To call a Python module from the command line, you can use the python command followed by flags like -m or -c, or run the script file directly. This article covers the most common approaches with practical examples. Running a Module with python -m The -m flag tells Python to run a module by its module name (without the .py extension). Python searches sys.path for the module and executes its __main__ block − ~$ python -m module_name For example, to run the built-in json.tool module for formatting JSON − ~$ echo '{"name":"Alice"}' | python -m json.tool { ...

Read More
Showing 1–10 of 25,445 articles
« Prev 1 2 3 4 5 2545 Next »
Advertisements