-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.py
121 lines (93 loc) · 1.48 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
n=2
f=3.14
s="string"
n1 = n + 1
s1 = "another" + s
print(n, f s)
if n > 2: print ">2"
elif n < 2: print "<2"
else: print "=2"
# creating
l = [2, 3.14, "py", n, f, s]
l2 = list("abc")
#accessing
l[1]
l[1:3]
l[-1]
l[2:]
l[:]
#adding
l.append("new")
l
l.insert(1, 44)
l
l[1:1] = 55
l
del l[1]
l
l.pop(1)
l
l.remove("new")
l
l=[7, 6, 8, 3.14]
l[0:2]=[3, 1, 2]
l
l.sort()
l
del l[3:5]
l
for e in l: print e
done = False
while not done:
cmd = raw_input("new command:")
if 'exit' in cmd:
done = True
elif 'show' in cmd:
DoShow()
elif 'assign' in cmd:
DoAssign()
d={'john': 12, 'mary': 34, 'brad':56}
d
d=dict(john=12, mary=34, brad=56)
d
d['ana']=78
d
d.update({'steve':90})
d
del d['ana']
d
d.pop('steve')
d
'mary' in d
for key in d.key():
pass
for val in d.values():
pass
for (key, val) in d.items()
pass
def mysum(l):
theSum = reduce(lambda x,y: x+y, l)
return theSum
import numpy
m = numpy.median(l)
from numpy import median
m = median(l)
import rpy2.robjects as robj
>>> pi = robjects.r['pi']
>>> pi[0]
3.14159265358979
robj.r('''
f <- function(r, verbose=FALSE) {
if (verbose) {
cat("I am calling f().\n")
}
2 * pi * r
}
f(3)
''')
>>> letters = robj.r['letters']
>>> rcode = 'paste(%s, collapse="-")' %(letters.r_repr())
>>> res = robj.r(rcode)
>>> print(res)
"a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z"