DEV Community

Cover image for Learn Python #8 - Let's do Tuple...🐍
Nitin Reddy
Nitin Reddy

Posted on

2 1

Learn Python #8 - Let's do Tuple...🐍

Tuples πŸ’‘

Tuple in Python is a collection that is ordered, and unchangeable.

Find an element by index πŸ’‘

You can find an element in the tuple by index

new_tuple=(1,"b",False)
print(new_tuple[0]) //1
print(new_tuple[1]) //"b"
Enter fullscreen mode Exit fullscreen mode

Ordered πŸ’‘

The tuple is an ordered collection. The order can not be changed.

Unchangeable πŸ’‘

Once defined, the tuple cannot be changed. But we can do some work around to update the tuple by leveraging list.

new_tuple = (1,"b")
updated_tuple = list(new_tuple)
updated_tuple[1] = "c"
new_tuple = tuple(updated_tuple)
print(new_tuple) // (1,"c")
Enter fullscreen mode Exit fullscreen mode

Access the elements πŸ’‘

Items in a tuple can be accessed with a negative index as well.

new_tuple=(1,2,3,"a", False)
new_tuple[-1] //False
Enter fullscreen mode Exit fullscreen mode

Conclusion πŸ“—

There are more posts on the way related to the tuples...So keep reading.

Thanks in advance for reading this article...πŸš€

I am more than happy to connect with you on

You can also find me on

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❀️