forked from NMGRL/pychron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINFO
More file actions
158 lines (120 loc) · 3.47 KB
/
INFO
File metadata and controls
158 lines (120 loc) · 3.47 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Pychron
0.2.0
requirements
-epd7.0.2
-scikits.statsmodels
-pyserial
Python, Enthought and Pychron need to knows
Think of python as a program. what it does is takes
human readable source code (.py) and parses,compiles and executes in real time.
Documentation
*** http://docs.python.org/ ***
Best place to start *** http://docs.python.org/tutorial/index.html ***
You can run python in 2 ways. (there is actually many ways to run from the command line but
only 2 are used frequently)
Open terminal
1. with no arguments. opens the python interpreter
$ python
2. with a path to python script (absolute or relative path). executes the script
$ python hello_world.py
Using Python Interpreter
Zen of Python
>>> import this
you can use the command line as a powerful calculator
*** see http://docs.python.org/tutorial/introduction.html ***
>>> x=1
>>> y=2
>>> x+y
3
>>> x=50
Use the up and down arrows to cycle thru previous commands
>>> x+y
53
>>> y=x+y
>>> x+y
103
>>> i=0
>>> i+=1 #same as i = i+1 works for all math operators i -=1, i*=2 etc...
strings are defined using ', ", or ''' blocks
' and " are equivalent. convenient when needing to nest quotes.
>>> s='foo'
>>> b='bar'
>>> print s,b
foo bar
>>> s
'foo'
>>> b
'bar'
to make a list of items use a list or tuple
*** see http://docs.python.org/tutorial/datastructures.html ***
list
>>> l1=[1,2,3,4]
>>> l2=['foo','bar']
>>> t1=(1,2,3)
>>> t2=('foo','bar')
get the length of the sequence use builtin len
>>> len(l1)
4
to generate a list of numbers use builtin range
>>> range(10)
[0,1,2,3,4,5,6,7,8,9]
>>> range(0,10,2)
[0,2,4,6,8]
get item from list
>>> l1[0]
1
get the last item
>>> l1[-1]
9
get a sublist
>>> l1[0:2] #list[startindex:stopindex:step]
#each parameter is optional but at least needs to be set
#startindex defaults to 0
#stopindex defaults to the last index
#step defaults to 1
l1[0:2] same as l1[0:2:1] and l1[:2] (preferred)
same slicing operations work on strings. just think of them as a list of characters
>>> s= 'hello world'
>>> s[:5]
'hello'
>>> s[6:]
'world'
>>> s[-5:]
'world'
you can split and join strings easily
>>> s.split(' ') #str.list(character to split on) returns a list
['hello', 'world']
>>> ', '.join(s.split(' ')) #join_str.join(list of strings to join)
hello, world
>>> '\n'.join(['this is a good','way to write multi','line text'])
this is a good
way to write multi
line text
dictionaries are key:value containers
there are two syntaxes for creating a dictionary
>>> d=dict(name='Jake', office=316, building='MSEC')
>>> d2 = {'name':'Jake','office':316, 'building':'MSEC'} #convenient when the keys are variables as well
>>> key1='person'
>>> key2='id'
>>> val1='John'
>>> val2=10394303
>>> d3 = {key1:val1, key2:val2}
to get a value from the dictionary you specifiy a key. To get the definition of a word you find the
word (key) in are dictionary and read the associated entry
>>> d['name']
Jake
entries can be modified
>>> d['name']='Jake Ross'
>>> d['name']
Jake Ross
#string formating is awesome in python
#lets say you want to display some text with your results
>>> 'the result of {} plus {} is {}'.format(x,y,x+y)
'the result of 50 plus 53 is 103'
>>> 'the result of {1} plus {0} is {2}'.format(x,y,x+y)
'the result of 53 plus 50 is 103'
#you can use pass in a key:pairs
>>> "{name}'s office is {building} {office}".format(name='Jake',building='MSEC',office=316)
or better
>>> "{name}'s office is {building} {office}".format(**d2)
"Jake's office is MSEC 316"