Server Side Programming Articles

Page 623 of 2109

What is @ operator in Python?

Jayashree
Jayashree
Updated on 24-Mar-2026 898 Views

The @ operator in Python serves multiple purposes. It's primarily used to define decorators, but it also functions as the matrix multiplication operator (introduced in Python 3.5). Let's explore both uses with practical examples. @ Operator as Decorator A decorator is a function that takes another function and extends its behavior without explicitly modifying it. The @ symbol provides clean syntax for applying decorators ? Basic Decorator Example def my_decorator(func): def wrapper(): print("Before function call") ...

Read More

How to implement Python __lt__ __gt__ custom (overloaded) operators?

Pythonista
Pythonista
Updated on 24-Mar-2026 4K+ Views

Python provides magic methods to define custom behavior for comparison operators. The operators =, == and != can be overloaded using the __lt__, __le__, __gt__, __ge__, __eq__ and __ne__ magic methods respectively. Basic Implementation Let's create a Distance class that overloads the operators to compare distances in feet and inches ? class Distance: def __init__(self, feet=5, inches=5): self.feet = feet self.inches = inches def __str__(self): ...

Read More

Is there a "not equal" operator in Python?

Pythonista
Pythonista
Updated on 24-Mar-2026 493 Views

Python provides the not equal operator to compare values and check if they are different. The != operator is the standard way to perform "not equal" comparisons in modern Python. The != Operator The != operator returns True when two values are not equal, and False when they are equal ? # Comparing numbers result1 = 5 != 3 result2 = 5 != 5 print("5 != 3:", result1) print("5 != 5:", result2) 5 != 3: True 5 != 5: False Using != with Different Data Types The not equal ...

Read More

How to save a Python Dictionary to CSV file?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 45K+ Views

In Python, to save a dictionary to a CSV file, we can use the csv module. This process depends on the structure of your dictionary. Generally, a CSV file refers to each line corresponding to a row in a table, and each value in the line is separated by a comma. CSV files are widely used because they are easy to read and write, and also easy to transfer data in the form of strings. Common Approaches There are various scenarios for saving a Python Dictionary to a CSV file. In this article, we focus on some ...

Read More

How to split Python tuples into sub-tuples?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 11K+ Views

In this article, we will show you how to split Python tuples into sub-tuples of equal size. Tuples are immutable, ordered collections in Python that can be divided into smaller chunks using various methods. What are Tuples? Tuples are immutable, ordered data structures used to store collections in Python. Unlike lists which are mutable and have variable length, tuples have a fixed length once created and cannot be modified. Method 1: Using Slicing The most straightforward approach uses slice notation with the range() function to create sub-tuples of a specific size. Example The following ...

Read More

How to zip a Python Dictionary and List together?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 13K+ Views

In this article, we will show you how to zip a Python dictionary and list together. Below are the various methods to accomplish this task ? Using zip() function Using append() function and items() function Using append() function and in operator Using zip() Function The zip() function combines two iterables by pairing their elements. When zipping a dictionary with a list, we use items() to get key-value pairs from the dictionary ? # input dictionary input_dict = {'Hello': 1, 'tutorialspoint': 2, 'python': 3, 'codes': 3} # input list input_list = [10, 20, ...

Read More

How to create Python dictionary with duplicate keys?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 7K+ Views

In Python, a dictionary doesn't allow duplicate keys. However, we can work around this limitation using defaultdict from the Collections module to store multiple values for the same key in the form of lists. Understanding defaultdict The defaultdict is a subclass of the built-in dict class that provides a default value for keys that don't exist. When you access a missing key, it automatically creates that key with a default value using a default factory function. from collections import defaultdict # Create defaultdict with list as default factory d = defaultdict(list) print(type(d)) print(d['new_key']) # ...

Read More

How to do Python math at command line?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 2K+ Views

Python can be used as a powerful calculator at the command line. When you invoke the Python interpreter, you get the (>>>) prompt where any mathematical expression can be entered and evaluated immediately upon pressing ENTER. Python Math Operators Python provides several arithmetic operators for mathematical calculations ? Operation Description a + b Addition - returns the sum of a and b a - b Subtraction - returns the difference of a and b -a Negation - changes the sign of a +a Identity - returns a ...

Read More

How to know Maximum and Minimum values for ints in Python?

Jayashree
Jayashree
Updated on 24-Mar-2026 193 Views

Python's core library provides built-in functions max() and min() to find maximum and minimum values from sequences like lists, tuples, or multiple arguments. Additionally, Python offers ways to discover the system limits for integer values. Using max() and min() Functions With Multiple Arguments You can pass multiple numbers directly to max() and min() − print(max(23, 21, 45, 43)) print(min(23, 21, 45, 43)) 45 21 With Lists and Tuples These functions also work with sequence objects like lists and tuples − numbers_list = [20, 50, 40, 30] numbers_tuple ...

Read More

How to create a Python dictionary from an object\'s fields?

Disha Verma
Disha Verma
Updated on 24-Mar-2026 542 Views

In Python, objects are instances of classes containing attributes and methods, while dictionaries are collections of key-value pairs. Converting an object's fields to a dictionary is useful for serialization, debugging, or data processing. Python provides two main approaches for this conversion. Using __dict__ Attribute Every Python object has a __dict__ attribute that stores the object's attributes and their values as a dictionary. This attribute directly exposes the internal namespace of the object ? Example class Company: def __init__(self, company_name, location): self.company_name = company_name ...

Read More
Showing 6221–6230 of 21,090 articles
« Prev 1 621 622 623 624 625 2109 Next »
Advertisements