Hey anyone can tell what is the difference between () or {} and [] and when I should use each one? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hey anyone can tell what is the difference between () or {} and [] and when I should use each one?

21st Mar 2021, 2:33 PM
Ibrahim Elixoder
Ibrahim Elixoder - avatar
7 Answers
+ 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.
21st Mar 2021, 4:38 PM
noteve
noteve - avatar
+ 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()
21st Mar 2021, 4:44 PM
noteve
noteve - avatar
+ 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)
21st Mar 2021, 2:38 PM
Jayakrishna 🇮🇳
+ 1
21st Mar 2021, 3:04 PM
Ibrahim Elixoder
Ibrahim Elixoder - avatar
+ 1
Eve 👌👌👍 I forgot those..tq...
22nd Mar 2021, 6:29 AM
Jayakrishna 🇮🇳
21st Mar 2021, 2:47 PM
Ibrahim Elixoder
Ibrahim Elixoder - avatar
0
イブライム yes... You tagged Python!
21st Mar 2021, 3:01 PM
Jayakrishna 🇮🇳