Skip to content

Commit 2fd2005

Browse files
committed
changes
1 parent 4d88e68 commit 2fd2005

File tree

1 file changed

+87
-88
lines changed

1 file changed

+87
-88
lines changed

foundations/basic-python.md

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -122,147 +122,146 @@ In building your first Python script we will set up our workspace and read a `.t
122122

123123
5. And create a data directory:
124124

125-
```
126-
mkdir data
127-
```
125+
```
126+
mkdir data
127+
```
128128

129129
6. Go into the data directory:
130-
`
131130

132-
```
133-
$ cd data
134-
```
131+
```
132+
$ cd data
133+
```
135134

136135
7. Download sample data from the CU Boulder weather station:
137136

138-
```
139-
$ curl -kO https://sundowner.colorado.edu/weather/atoc8/wxobs20170821.txt
140-
```
137+
```
138+
$ curl -kO https://sundowner.colorado.edu/weather/atoc8/wxobs20170821.txt
139+
```
141140

142-
This weather station is a Davis Instruments wireless Vantage Pro2 located on the CU-Boulder east campus at the SEEC building (40.01 N, 05.24 W, 5250 ft elevation). The station is monitored by the Atmospheric and Oceanic Sciences (ATOC) department and is part of the larger University of Colorado ATOC Weather Network.
141+
This weather station is a Davis Instruments wireless Vantage Pro2 located on the CU-Boulder east campus at the SEEC building (40.01 N, 05.24 W, 5250 ft elevation). The station is monitored by the Atmospheric and Oceanic Sciences (ATOC) department and is part of the larger University of Colorado ATOC Weather Network.
143142

144-
7. Go back to the top-level directory:
143+
8. Go back to the top-level directory:
145144

146-
```
147-
$ cd ..
148-
```
145+
```
146+
$ cd ..
147+
```
149148

150-
8. And now that you've set up our workspace, create a blank Python script, called `mysci.py`:
149+
9. And now that you've set up our workspace, create a blank Python script, called `mysci.py`:
151150

152-
```
153-
$ touch mysci.py
154-
```
151+
```
152+
$ touch mysci.py
153+
```
155154

156-
If you are working on a Windows machine it is possible that `touch` will not be recognized as an internal or external command. If this is the case, run `conda install m2-base` to enable unix commands such as `touch`.
155+
If you are working on a Windows machine it is possible that `touch` will not be recognized as an internal or external command. If this is the case, run `conda install m2-base` to enable unix commands such as `touch`.
157156

158-
9. Edit the `mysci.py` file using nano, vim, or your favorite text editor to include the classic first command - printing, "Hello, world!".
157+
10. Edit the `mysci.py` file using nano, vim, or your favorite text editor to include the classic first command - printing, "Hello, world!".
159158

160-
```python
161-
print("Hello, world!")
162-
```
159+
```python
160+
print("Hello, world!")
161+
```
163162

164-
On a Windows machine, it is possible `nano` or `vim` are not recognized as text editors within your terminal. In this case simply try to run `mysci.py` to open a notepad editor.
163+
On a Windows machine, it is possible `nano` or `vim` are not recognized as text editors within your terminal. In this case simply try to run `mysci.py` to open a notepad editor.
165164

166-
10. To run a Python script from the command line type, "python" and then the name of your script:
165+
11. To run a Python script from the command line type, "python" and then the name of your script:
167166

168-
```
169-
$ python mysci.py
170-
```
167+
```
168+
$ python mysci.py
169+
```
171170

172-
11. You probably won't need to run your Hello World script again, so delete the `print("Hello, world!")` line and start over with something more useful - we'll read the first 4 lines from our datafile.
171+
12. You probably won't need to run your Hello World script again, so delete the `print("Hello, world!")` line and start over with something more useful - we'll read the first 4 lines from our datafile.
173172

174-
Change the `mysci.py` script to read:
173+
Change the `mysci.py` script to read:
175174

176-
```python
177-
# Read the data file
178-
filename = "data/wxobs20170821.txt"
179-
datafile = open(filename, 'r')
175+
```python
176+
# Read the data file
177+
filename = "data/wxobs20170821.txt"
178+
datafile = open(filename, 'r')
180179

181-
print(datafile.readline())
182-
print(datafile.readline())
183-
print(datafile.readline())
184-
print(datafile.readline())
180+
print(datafile.readline())
181+
print(datafile.readline())
182+
print(datafile.readline())
183+
print(datafile.readline())
185184

186-
datafile.close()
185+
datafile.close()
187186

188-
```
187+
```
189188

190-
First create a variable for your datafile name, which is a string - this can be in single or double quotes.
189+
First create a variable for your datafile name, which is a string - this can be in single or double quotes.
191190

192-
Then create a variable associated with the opened file, here it is called `datafile`.
191+
Then create a variable associated with the opened file, here it is called `datafile`.
193192

194-
The `'r'` argument in the open command indicates that we are opening the file for reading capabilities. Other input arguments for open include `'w'`, for example, if you wanted to write to the file.
193+
The `'r'` argument in the open command indicates that we are opening the file for reading capabilities. Other input arguments for open include `'w'`, for example, if you wanted to write to the file.
195194

196-
The readline command moves through the open file, always reading the next line.
195+
The readline command moves through the open file, always reading the next line.
197196

198-
And remember to close your datafile.
197+
And remember to close your datafile.
199198

200-
Comments in Python are indicated with a hash, as you can see in the first line `# Read the data file`. Comments are ignored by the interpreter.
199+
Comments in Python are indicated with a hash, as you can see in the first line `# Read the data file`. Comments are ignored by the interpreter.
201200

202-
12. Run your script by typing:
201+
13. Run your script by typing:
203202

204-
```
205-
$ python mysci.py
206-
```
203+
```
204+
$ python mysci.py
205+
```
207206

208-
Testing of your script with `python mysci.py` should be done every time you wish to execute the script. This will no longer be specified as a unique step in between every change to our script.
207+
Testing of your script with `python mysci.py` should be done every time you wish to execute the script. This will no longer be specified as a unique step in between every change to our script.
209208

210-
13. Change the `mysci.py` script to read your whole data file:
209+
14. Change the `mysci.py` script to read your whole data file:
211210

212-
```python
213-
# Read the data file
214-
filename = "data/wxobs20170821.txt"
215-
datafile = open(filename, 'r')
211+
```python
212+
# Read the data file
213+
filename = "data/wxobs20170821.txt"
214+
datafile = open(filename, 'r')
216215

217-
data = datafile.read()
216+
data = datafile.read()
218217

219-
datafile.close()
218+
datafile.close()
220219

221-
# DEBUG
222-
print(data)
223-
print('data')
224-
```
220+
# DEBUG
221+
print(data)
222+
print('data')
223+
```
225224

226-
Our code is similar as before, but now we've read the entire file. To test that this worked. We'll `print(data)`. Print statements in python require parenthesis around the object you wish to print, in this scenario the data object.
225+
Our code is similar as before, but now we've read the entire file. To test that this worked. We'll `print(data)`. Print statements in python require parenthesis around the object you wish to print, in this scenario the data object.
227226

228-
Try `print('data')` as well. Now Python will print the string `data`, as it did for the hello world function, instead of the information stored in the variable data.
227+
Try `print('data')` as well. Now Python will print the string `data`, as it did for the hello world function, instead of the information stored in the variable data.
229228

230-
Don't forget to execute with :bash:`python mysci.py`.
229+
Don't forget to execute with :bash:`python mysci.py`.
231230

232-
14. Change the `mysci.py` script to read your whole data file using a context
231+
15. Change the `mysci.py` script to read your whole data file using a context
233232
manager with:
234233

235-
```python
236-
# Read the data file
237-
filename = "data/wxobs20170821.txt"
238-
with open(filename, 'r') as datafile:
239-
data = datafile.read()
234+
```python
235+
# Read the data file
236+
filename = "data/wxobs20170821.txt"
237+
with open(filename, 'r') as datafile:
238+
data = datafile.read()
240239

241-
# DEBUG
242-
print(data)
243-
```
240+
# DEBUG
241+
print(data)
242+
```
244243

245-
Again this is a similar method of opening the datafile, but we now use `with open`. The `with` statement is a context manager that provides clean-up and assures that the file is automatically closed after you've read it.
244+
Again this is a similar method of opening the datafile, but we now use `with open`. The `with` statement is a context manager that provides clean-up and assures that the file is automatically closed after you've read it.
246245

247-
The indendation of the line `data = datafile.read()` is very important. Python is sensitive to white space and will not work if you mix spaces and tabs (Python does not know your tab width). It is best practice to use four spaces as opposed to tabs (tab width is not consistent between editors).
246+
The indendation of the line `data = datafile.read()` is very important. Python is sensitive to white space and will not work if you mix spaces and tabs (Python does not know your tab width). It is best practice to use four spaces as opposed to tabs (tab width is not consistent between editors).
248247

249-
Combined these two lines mean: with the datafile opened, I'd like to read it.
248+
Combined these two lines mean: with the datafile opened, I'd like to read it.
250249

251-
And execute with `python mysci.py`.
250+
And execute with `python mysci.py`.
252251

253-
15. What did we just see? What is the data object? What type is data? How do we find out?
252+
16. What did we just see? What is the data object? What type is data? How do we find out?
254253

255-
Change the DEBUG section of our script to:
254+
Change the DEBUG section of our script to:
256255

257-
```python
258-
# DEBUG
259-
print(type(data))
260-
```
256+
```python
257+
# DEBUG
258+
print(type(data))
259+
```
261260

262-
And execute with `python mysci.py`.
261+
And execute with `python mysci.py`.
263262

264-
Object types refer to `float`, `integer`, `string` or other types that you can create.
263+
Object types refer to `float`, `integer`, `string` or other types that you can create.
265264

266-
Python is a dynamically typed language, which means you don't have to explicitly specify the datatype when you name a variable, Python will automatically figure it out by the nature of the data.
265+
Python is a dynamically typed language, which means you don't have to explicitly specify the datatype when you name a variable, Python will automatically figure it out by the nature of the data.
267266

268267
In this section you set up a workspace by creating your directory and activating your conda environment. You downloaded a .txt file and read it using the Python commands of `open()`, `readline()`, `read()`, `close()`, and `print()`, as well as the context manager `with`. You should be familiar with the `str` datatype.

0 commit comments

Comments
 (0)