0
Hey anyone can tell what is the difference between () or {} and [] and when I should use each one?
7 Réponses
+ 3
イブライム
[ Additional Answer ] to Jayakrishna🇮🇳
( ) is also commonly used to enclose tuples.
Tuples are like list but immutable or the elemenets cannot be modifed or changed.
Examples of tuples:
🔹(1, 2, 3, 4)
🔹("hi", True, 42, 3.14)
🔹(2,)
tuples can also be written without parentheses (as long as it has comma at the end or between each element)
🔹1, 2, 3
🔹"Hello",
Use list [ ] if you want to perform different modification and methods to your elements.
🔸e.g. if you know your elements will get change constantly.
Use tuples ( ) if you don't want to change your values inside it.
🔸e.g. if you know you will not add nor remove nor change any element.
+ 3
[ Additional Answer 2 ]
{ } or curly bracket is not only used by dictionary, but also in sets too.
Sets are mutable and group of elements in which elemets are unique (no duplicates). Use sets if you want your elements to be unique to each other.
Example of Sets:
🔹{1, 2, 3, 4}
Dictionary is a built-in data structure which has key:value pair(s). Dictionary is mutable and useful when you want to have key-value pairs.
Example of Dictionary:
🔹{1 : "one", 2 : "two"}
But { } is an empty dictionary (not empty set).
🔸empty_dict = { }
If you want to declare an empty set, use set function followed by a pair of parentheses.
🔸empty_set = set()
+ 1
Data = { } #Data is a dictionary type..
Data = [ ] #Data is a list now..
( ) parenthesis opertors to combine compound statements just like other languages.... (a<b and b<c)
+ 1
Jayakrishna🇮🇳 yes I tagged python
+ 1
Eve 👌👌👍
I forgot those..tq...
0
Jayakrishna🇮🇳 is it python?