When to use tuples in python, what even are they? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grátis
0

When to use tuples in python, what even are they?

What's a tuple? When should it be used in python? I'm trying to figure out errbot, a chat box program written in python, and it states that sometimes a "tuple" is required and it refers to putting a trailing "," after a list when inside parenthesis. Like so: thing = ("things thing one","things thing two",) I do not know what it is or why, pls help :)

29th Aug 2021, 12:01 AM
⊹≒tɧε8шσσɖɕนttεг≓⊹
⊹≒tɧε8шσσɖɕนttεг≓⊹ - avatar
6 Respostas
+ 3
Tuples are just unchangeable lists, you use it when you want a data structure that can't be changed :) for example maybe you want to have birth records, you don't want to change something like that.
29th Aug 2021, 12:26 AM
Chloe
Chloe - avatar
+ 3
⊹≒tɧε8шσσɖɕนttεг≓⊹ also, when defining a tuple with one element, it's mandatory to add a trailing comma so python knows it's not just redundant parentheses :)
4th Sep 2021, 12:26 AM
Chloe
Chloe - avatar
+ 2
Tuples are similar to lists, but they are immutable. See Python Data Structures 16.1 and 17.1.
29th Aug 2021, 12:22 AM
Simon Sauter
Simon Sauter - avatar
+ 2
"In some code samples you might see a comma after the last item in the list. It's not mandatory, but perfectly valid." in python core 23.1 it sez this aha!
3rd Sep 2021, 11:35 PM
⊹≒tɧε8шσσɖɕนttεг≓⊹
⊹≒tɧε8шσσɖɕนttεг≓⊹ - avatar
+ 1
Calvin [Inactive] ooooooh so then if you instantiate a variable with say var = (), you should use the tuple thing afterwards? (Tuple i mean comma). Thats like a "hey im not reduntant!" stop from when it was started that way.
5th Sep 2021, 12:13 AM
⊹≒tɧε8шσσɖɕนttεг≓⊹
⊹≒tɧε8шσσɖɕนttεг≓⊹ - avatar
+ 1
Or its to assert that the () ARE redundant when passed through? Giving the data without parenthesis
5th Sep 2021, 12:14 AM
⊹≒tɧε8шσσɖɕนttεг≓⊹
⊹≒tɧε8шσσɖɕนttεг≓⊹ - avatar