Programming Articles

Page 14 of 2547

How can I remove items out of a Python tuple?

Sindhura Repala
Sindhura Repala
Updated on 24-Mar-2026 2K+ Views

A tuple in Python is an ordered, immutable collection that holds multiple items, supports mixed data types, and allows indexing. Since tuples cannot be modified directly, we need special techniques to remove items from them. Here are three main approaches to remove items from a Python tuple: Converting to List and Back Using Tuple Comprehension ...

Read More

How can I create a Python tuple of Unicode strings?

Sindhura Repala
Sindhura Repala
Updated on 24-Mar-2026 467 Views

A tuple in Python is an ordered, immutable collection that stores multiple items. It supports mixed data types, allows indexing, and is commonly used for fixed data structures. In Python 3, all strings are Unicode by default, making it straightforward to create tuples containing Unicode strings. Creating Basic Unicode String Tuples You can create a tuple with Unicode strings by simply placing Unicode text inside tuple parentheses. Python 3 handles Unicode automatically ? # Basic Unicode tuple unicode_tuple = ('café', 'naïve', '世界', '😊') print(unicode_tuple) print(type(unicode_tuple)) ('café', 'naïve', '世界', '😊') Using ...

Read More

How can I create a non-literal python tuple?

Sindhura Repala
Sindhura Repala
Updated on 24-Mar-2026 451 Views

A tuple in Python is an ordered, immutable collection that stores multiple items. Tuples can be created in two ways: directly with fixed values (literal) or dynamically using code (non-literal). Literal Tuple − Created directly with parentheses: (1, 2, 3) Non-literal Tuple − Created dynamically using functions or expressions What is a Non-Literal Tuple? A non-literal tuple is created dynamically using code instead of being written directly with parentheses and values. It's formed through functions, expressions, or data transformations. Method 1: Using tuple() Constructor Convert existing data structures into tuples ? ...

Read More

How can I represent immutable vectors in Python?

Sindhura Repala
Sindhura Repala
Updated on 24-Mar-2026 417 Views

An immutable vector in Python is a fixed, ordered collection of numerical values that cannot be changed after creation. These are implemented using tuples or libraries like NumPy with write protection, ensuring data consistency and preventing modifications during computations. Methods to Represent Immutable Vectors Python provides several approaches to create immutable vectors ? Using Tuples Using NumPy Arrays with Write Protection Using Named Tuples Using Tuples Tuples are inherently immutable in Python, making them ideal for representing fixed vectors ? ...

Read More

How do we compare two tuples in Python?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 24-Mar-2026 6K+ Views

Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are not equal, this is the result of the comparison, else the second item is considered, then the third and so on. Lexicographic Comparison Python compares tuples element by element from left to right using lexicographic ordering ? a = (1, 2, 3) b = (1, 2, 5) print(a < b) print(a == b) print(a > b) True False False In this example, the first two ...

Read More

How can I convert Python tuple to C array?

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

In this article, we will show you how to convert a Python tuple to a C array. Python does not have a built-in array data type like other programming languages, but you can create an array using a library like NumPy. Installation If you haven't already installed NumPy on your system, run the following command to do so ? pip install numpy Methods for Tuple to Array Conversion The Python NumPy library provides various methods to create, manipulate and modify arrays in Python. Following are the two important methods that help us convert ...

Read More

Why python returns tuple in list instead of list in list?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 630 Views

In Python, many built-in functions and operations return lists of tuples instead of lists of lists. This design choice is intentional and serves important purposes in data integrity and performance. The primary reason is that tuples are immutable − once created, they cannot be modified. This makes them ideal for representing fixed data like database records, coordinate pairs, or grouped values that should remain unchanged. In contrast, lists are mutable and can be accidentally modified, which could lead to data corruption. The enumerate() Function The enumerate() function adds a counter to an iterable and returns an enumerate ...

Read More

How can I preserve Python tuples with JSON?

Samual Sam
Samual Sam
Updated on 24-Mar-2026 478 Views

JSON format doesn't have a built-in tuple type, so Python's json module converts tuples to JSON arrays (lists). This means the immutability of tuples is lost during serialization. However, you can preserve tuples using custom encoders and decoders or alternative serialization methods. Problem: Default JSON Conversion By default, Python tuples are converted to JSON arrays ? import json data = { "coordinates": (10, 20), "colors": ["red", "blue"], "dimensions": (1920, 1080, 32) } json_string = json.dumps(data) print("JSON string:", json_string) # When loaded ...

Read More

How can I convert bytes to a Python string?

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

In Python, bytes are sequences of 8-bit values, while strings are sequences of Unicode characters. Converting bytes to strings is a common task when working with file I/O, network data, or encoded text. Python provides several built-in methods to perform this conversion efficiently. Converting Bytes to Strings in Python The following methods can be used to convert bytes to Python strings ? Using decode() method Using str() function Using codecs.decode() function Using pandas library Using decode() Method ...

Read More

Where are operators mapped to magic methods in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 574 Views

In this article, we will explain where operators are mapped to magic methods in Python and how they enable operator overloading. Python Magic methods are special methods that begin and end with double underscores. They are also known as dunder methods. Magic methods are not intended to be invoked directly by you, but rather invocation occurs by the class on a specific action. When you use the + operator to add two numbers, the __add__() method is called internally. Many magic methods in Python are defined by built-in classes. To get the number of magic methods inherited by ...

Read More
Showing 131–140 of 25,467 articles
« Prev 1 12 13 14 15 16 2547 Next »
Advertisements