DEV Community

Cover image for Learn Python: Tuples
Rishi
Rishi

Posted on

4 2

Learn Python: Tuples

Tuple shares some similarities to list.
Just like a list, a tuple variable can hold multiple values.

However:

  • There are no square brackets [ ].
  • The values are separated by a , .
  • It is good practice to use brackets ( ) around the values, but it is not required. However, where python can't understand whether to evaluate the values as a tuple or a list, brackets will be required.

Creating new tuple.

a_tuple = "Rishi", "Abee"
another_tuple = (1, 2)
Enter fullscreen mode Exit fullscreen mode

⚠️ NOTE: Leaving a trailing , at the end of a value will cause python to evaluate the variable as a tuple instead.

a_string = "Rishi"
a_short_string_tuple = "Rishi",
a_short_number_tuple = 2,
Enter fullscreen mode Exit fullscreen mode

Tuples can be added to a list.
💡 To help python better understand whether to evaluate the values as a tuple, brackets ( ) are required.

a_list = ["Orange", "Yellow"]
a_tuple_inside_a_list= [('🍏', '🍎'), another_tuple, 'Hello']
Enter fullscreen mode Exit fullscreen mode

A Tuple is Immutable

Unlike a list, a tuple cannot be modified by appending values to it.
If we need to add values to a tuple, we will have to re-assign the same tuple a new value.

# Adding to a tuple.
a_tuple = "Rishi", "Abee"
a_tuple = a_tuple + ("Python",)
print(a_tuple )
Enter fullscreen mode Exit fullscreen mode



Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (2)

Collapse
 
i96k profile image
Isak

Really liked the flow of this tutorial. Good work! 👍

Collapse
 
rishiabee profile image
Rishi

More to come every week. Keep watching for updates.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay