Python Tuples

Python Tuples

Python tuples are ordered, immutable collections of objects. They are similar to lists in Python but have a few key differences. Here are some important characteristics of tuples:

  1. Immutable: Tuples are immutable, which means their elements cannot be modified after creation. Once a tuple is created, you cannot change, add, or remove individual elements. However, you can create a new tuple by concatenating or slicing existing tuples.
  2. Ordered: Tuples maintain the order of elements. The position of an item in a tuple can be used to access it later. This distinguishes tuples from sets, which are unordered collections.
  3. Heterogeneous elements: Tuples can contain elements of different data types, such as numbers, strings, lists, and even other tuples.
  4. Indexed and sliced: You can access individual elements in a tuple using their index. Tuple indices start at 0 for the first element. Additionally, you can use slicing to extract a subset of elements from a tuple.

Here’s an example of creating and working with tuples in Python:

python
# Creating a tuple
my_tuple = (1, 2, "three", [4, 5])

# Accessing elements
print(my_tuple[0])  # Output: 1
print(my_tuple[2])  # Output: three

# Slicing
print(my_tuple[1:3])  # Output: (2, "three")
print(my_tuple[:2])   # Output: (1, 2)
print(my_tuple[2:])   # Output: ("three", [4, 5])

# Concatenating tuples
another_tuple = (6, 7)
combined_tuple = my_tuple + another_tuple
print(combined_tuple)  # Output: (1, 2, "three", [4, 5], 6, 7)

Tuples are commonly used to represent collections of related values when immutability is desired, such as coordinates, database records, or pairs of values.

Leave a Reply

Your email address will not be published. Required fields are marked *

GLOTRU Footer
Popular Features
Popular Services/

Website Development & Design

App Development & Design

Graphic Design

Digital Marketing

SEO (Search Engine Optimization)

SMM (Social Media Marketing)

Cyber Security

Company

GLOTRU Founder & CEO : __Azam

Registared : Trade,MSME,etc

Board of Director

Team

About Us

Contact Us

Privacy Policy

Return & Refund Policy

Abuse Policy

Copyright Policy

Cookie Policy

Terms & Conditions

Universal Terms of Service

Disclaimer

Legal

Sponsorships

Investor

Press Releases

Our Investments

Brands

Newsroom

Business

...

_

Digital Millennium Copyright Act
DMCA.com Protection Status

_

Content similarity detection
Protected by Copyscape

_

***ANTI-PIRACY WARNING***

...................................................................................

Follow Us :

...................................................................................

SECURE SERVER : [Legal] [Privacy Policy] [Universal Terms of Service] [Do not sell my personal information]

SITE HOSTED : GLOTRU SECURE SERVER Asian Data Centre [You can host your site][Click Here]

SSL : Server Type : [Cloudflare] Certificate Issued By : [Let's Encrypt] Signature Algorithm : [ECDSA with SHA-384]

SITE BUILD SOFTWARE : Content Management System (CMS) Softwere

_

_

_