Skip to main content

Command Palette

Search for a command to run...

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

Python

Updated
β€’1 min read
Learn Python #8 - Let's do Tuple...🐍
N

Seasoned JavaScript Developer who loves to explore other programming languages like Python. A problem solver, tech lover by heart. Loves reading booking, and cooking. πŸ€©πŸš€πŸ”¬

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"

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")

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

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

More from this blog

N

nitinreddy3

43 posts

Seasoned JavaScript Developer who loves to explore other programming languages like Python. A problem solver, tech lover by heart. Loves reading booking, and cooking. πŸ€©πŸš€πŸ”¬