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?
3 ответов
+ 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
+ 5
Bojan Jovanovic ,
using this: words=["hello", "world", "!"] creates a list
using this: words=("hello", "world", "!") creates a tuple
+ 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.