+ 1

What's the difference between a tuple and a list?

They both are set of values separated by commas.

29th Jan 2020, 7:48 PM
Punyasloka Sahoo
Punyasloka Sahoo - avatar
9 Respuestas
+ 6
A Tulpe is immutable, a list is mutable.
29th Jan 2020, 7:51 PM
Oma Falk
Oma Falk - avatar
+ 3
I'm not sure if you have fully grasped the difference between changing an object and assigning a name to an object. dice_value = (9, 2) Does not change the tuple that was formerly in there - you just say: 'The name Dice_value shall be given to this new tuple (9, 2).' This means, that the name is 'stripped off' the old tuple and sticked to the new tuple. Python now sees that the old tuple isn't needed anymore and silently discards it for you in the background without you even knowing.
29th Jan 2020, 11:03 PM
HonFu
HonFu - avatar
+ 3
Huzaifa Red Headphone, it can't be changed *at all*. That's how the type is defined.
31st Jan 2020, 2:55 PM
HonFu
HonFu - avatar
+ 1
"die_values" is not necessarily a tuple. granted you are assigning it a tuple, you can assign anything to it. In regard to tuple. mytuple = (1,2,3) print(mytuple[0]) ...,<- this is ok mytuple[0] = 10....<- this, you can't do. see here:- https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/python_tuples.asp https://www.w3schools.com/python/python_ref_tuple.asp
29th Jan 2020, 10:48 PM
rodwynnejones
rodwynnejones - avatar
+ 1
@Seb TheS myset1 = set([1, 2, 3, 5])... <- list in set. myset2 = set("hello world")...<- string in set. print(myset1) print(myset2)
29th Jan 2020, 11:07 PM
rodwynnejones
rodwynnejones - avatar
0
@Oma Falk, The following is a code. The function roll_dice returns a tuple, which is stored in dice_values. dice_values should be a tuple, but clearly I can modify it in the next line. How is that possible if it is a tuple? #Code for simulating the dice game craps import random def roll_dice(): #rolls two dice, gives the number of dots on the face facing up. die1=random.randrange(1,7) die2=random.randrange(1,7) return (die1,die2)#packs the number of dots on top face into a tuple def display_dice(dice): #prints the value of both the die die1,die2=dice print(f'Player rolled{die1}+{die2}={sum(dice)}') die_values=roll_dice() #stores the tuple returned by roll_dice() display_dice(die_values) die_values=(9,2) #trying to modify the tuple sum_of_dice=sum(die_values) if sum_of_dice in (7,11): game_status="WON" elif sum_of_dice in(2,3,12): game_status="LOSE" else: game_status="CONTINUE" my_point=sum_of_dice print("Point is",my_point) while game_status=="CONTINUE": die_values=roll_dice() display_dice(die_values) sum_of_dice=sum(die_values) if sum_of_dice==my_point: game_status="WON" elif sum_of_dice==7: game_status="LOSE" if game_status=="WON": print("Player wins the game.") elif game_status=="LOSE": print("Player loses the game.") Say if I am wrong.
29th Jan 2020, 8:34 PM
Punyasloka Sahoo
Punyasloka Sahoo - avatar
0
Tuples: -Are faster than lists (on creation), -Require less memory than lists. Unlike lists, tuples: -Can be used as dictionary keys, -Can be stored in sets.
29th Jan 2020, 10:02 PM
Seb TheS
Seb TheS - avatar
0
rodwynnejones set( [1, 2, 3, 5] ) ---> {1, 2, 3, 5} set("hello world") ---> {'h', 'e', 'l', 'o', ' ', 'w', 'r', 'd'}; { [1, 2, 3, 4] } -×-> { [1, 2, 3, 4] } #Error { (1, 2, 3, 4) } ---> { (1, 2, 3, 4) } set( [ [1, 2, 3, 4] ] ) -×-> { [1, 2, 3, 4] } #Error
30th Jan 2020, 7:42 AM
Seb TheS
Seb TheS - avatar
0
Tuple can't be changed easily
31st Jan 2020, 2:54 PM
Huzaifa Khilawala
Huzaifa Khilawala - avatar