Parentheses in lists [PY] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Parentheses in lists [PY]

So I noticed in Py lists in 1st lecture they used [] for lists: words=["hello", "world", "!"] So I changed parentheses for words to basic () and it worked. And then for range they used (): List(range[4]) So I changed parentheses here to [] and it didn't work. Why are different parenthses used for lists?

26th Jun 2022, 1:52 PM
Bojan Jovanovic
Bojan Jovanovic - avatar
3 Answers
+ 4
You can create a list by 2 ways basically : 1st: [ ] braces like l = [ < values > ] Ex: [ 1,2,3,4,5 ] Or like 2nd: l = list( iterator ) ex: list( range(10) ) # range() is function, which returns an iterator. hope it helps.. edit: ( 1,2,3,4,5 ) creates a tuple, not list
26th Jun 2022, 2:06 PM
Jayakrishna 🇮🇳
+ 5
Bojan Jovanovic , using this: words=["hello", "world", "!"] creates a list using this: words=("hello", "world", "!") creates a tuple
26th Jun 2022, 2:58 PM
Lothar
Lothar - avatar
+ 4
Bojan Jovanovic list(range(4)) here list is a function if you print this then you get result same as above means [] print(list(range(4)) will give [0, 1, 2, 3] Which is like words = ['hello', 'world', '!'] remeber a function has parenthesis () so list() and range() are functions.
26th Jun 2022, 2:04 PM
A͢J
A͢J - avatar