Python for R Users
Daniel Chen
Instructor
[3, 2.718, True, 'hello']
)l <- list(1, "2", TRUE)
l
[[1]]
[1] 1
[[2]]
[1] "2"
[[3]]
[1] TRUE
l = [1, '2', True]
l
[1, '2', True]
Python is a 0-indexed language
Get the 1st element from R list
l[[1]] # use 1
1
Get the 1st element from Python list
l[0] # use 0
1
Negative indices start counting from the end
l <- list(1, "2", TRUE)
l[-1]
[[1]]
[1] "2"
[[2]]
[1] TRUE
l = [1, "2", True]
l[-1]
True
Left inclusive, right exclusive
index 0 1 2 3 4 5
| | | | | |
| element 0 | element 1 | element 2 | element 3 | element 4 |
| | | | | |
l = [0, 1, 2, 3, 4]
l[0:3]
[0, 1, 2]
l[0:5:2]
[0, 2, 4]
l[:3]
[0, 1, 2]
l[1:]
[1, 2, 3, 4]
l[::2]
[0, 2, 4]
l[0:3]
[0, 1, 2]
l[1:5]
[1, 2, 3, 4]
l[0:5:2]
[0, 2, 4]
d = {'int_value':3, 'bool_value':False, 'str_value':'hello'}
d
{'int_value': 3, 'bool_value': False, 'str_value': 'hello'}
d['str_value']
'hello'
l = [0, 1, 2, 3, 4]
d = {'int_value':3, 'bool_value':False, 'str_value':'hello'}
len(l)
5
len(d)
3
Python for R Users