Skip to content

Commit c533c15

Browse files
committed
headers
1 parent a7826db commit c533c15

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

appendix/howto.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# How To Do Common Operations in Python
22

3-
## Get positional command-line arguments
3+
# Get positional command-line arguments
44

55
You can get the command-line arguments using `sys.argv` (argument vector), but it's annoying that the name of the Python program itself is in the first position (`sys.argv[0]`). To skip over this, take a slice of the argument vector starting at the second position (index `1`) which will succeed even if there are no arguments -- you'll get an empty list, which is safe.
66

@@ -22,7 +22,7 @@ There are 1 arg
2222
$ ./args.py foo bar
2323
There are 2 args
2424
````
25-
## Put positional arguments into named variables
25+
# Put positional arguments into named variables
2626

2727
If you use `sys.argv[1]` and `sys.argv[2]` throughout your program, it degrades readability. It's better to copy the values into variables that have meaningful names like "file" or "num_lines".
2828

@@ -51,7 +51,7 @@ $ ./name_args.py nobody.txt 10
5151
FILE is "nobody.txt", NUM is "10"
5252
````
5353

54-
## Set defaults for optional arguments
54+
# Set defaults for optional arguments
5555

5656
````
5757
$ cat -n default_arg.py
@@ -79,7 +79,7 @@ $ ./default_arg.py nobody.txt 5
7979
FILE is "nobody.txt", NUM is "5"
8080
````
8181

82-
## Test argument is file and read
82+
# Test argument is file and read
8383

8484
This program takes an argument, tests that it is a file, and then reads it. It's basically `cat`.
8585

@@ -121,7 +121,7 @@ To an admiring Bog!
121121
Emily Dickinson
122122
````
123123

124-
## Write data to a file
124+
# Write data to a file
125125

126126
To write a file, you need to `open` some filename with a second argument of the "mode" where
127127

@@ -164,7 +164,7 @@ bar
164164
baz
165165
````
166166

167-
## Accept/use a random seed argument
167+
# Accept/use a random seed argument
168168

169169
Here is how I would define and use a random seed argument using `argparse`. If the `--seed` is defined with `default=None`, then you can pass it directly to `random.seed`. When it is defined by the user, it will be used; otherwise it is ignored.
170170

@@ -221,7 +221,7 @@ $ ./random_seed.py -s 1
221221
Random number is "18"
222222
````
223223

224-
## Test if an argument is a directory and list the contents
224+
# Test if an argument is a directory and list the contents
225225

226226
````
227227
$ cat -n list_dir.py
@@ -266,7 +266,7 @@ codons.py
266266
default_arg.py
267267
````
268268

269-
## Loop N Times
269+
# Loop N Times
270270

271271
If you want to go through a loop some defined number of times, maybe combine `for` and `range`:
272272

@@ -311,7 +311,7 @@ $ ./looping_n_times.py 3
311311
3 times
312312
````
313313

314-
## Skip an iteration of a loop
314+
# Skip an iteration of a loop
315315

316316
Sometimes in a loop (`for` or `while`) you want to skip immediately to the top of the loop. You can use `continue` to do this. In this example, we skip the even-numbered lines by using the modulus `%` operator to find those line numbers which have a remainder of 0 after dividing by 2. We can use the `enumerate` function to provide both the array index and value of any list.
317317

@@ -351,7 +351,7 @@ $ ./skip_loop.py nobody.txt
351351
11 Emily Dickinson
352352
````
353353

354-
## Create a directory if it does not exist
354+
# Create a directory if it does not exist
355355

356356
This program takes a directory name and looks to see if it already exists or needs to be created.
357357

@@ -476,7 +476,7 @@ $ ./unpack_dict2.py
476476
1982 Signals
477477
````
478478

479-
## Sort a dictionary by keys
479+
# Sort a dictionary by keys
480480

481481
To sort a dictionary by the keys, you have to understand that the `.sort()` method of an list mutates the list *in-place*. We get the keys of a dictionary with the `.keys()` method which does not support the `.sort()` method:
482482

@@ -596,7 +596,7 @@ Power Windows 1985
596596
Signals 1982
597597
````
598598

599-
## Sort a dictionary by values
599+
# Sort a dictionary by values
600600

601601
To sort a dictionary by the values rather than the keys, we need to reverse the tuples which is what happens on line 24. Notice that in years when two albums were released, the `sorted` first sorts by the first tuple member (the year) and then the second (album name):
602602

0 commit comments

Comments
 (0)