Skip to content

Commit 1dd143b

Browse files
committed
Minor copyedits
1 parent f7c5969 commit 1dd143b

2 files changed

Lines changed: 29 additions & 117 deletions

File tree

README.md

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,11 @@ print("Fifth item:", fruits[4])
534534
4. You can add an optional step interval (every 2nd item, every 3rd item, etc.)
535535
536536
``` python
537-
print("First 5 items, every other item:", fruits[0:5:2])
538-
print("Every third item:", fruits[::3])
537+
# First 5 items, every other item
538+
print(fruits[0:5:2])
539+
540+
# Every third item
541+
print(fruits[::3])
539542
```
540543
541544
### (Optional) Why are lists indexed from 0?
@@ -568,7 +571,7 @@ print(fruits[20])
568571
print(fruits[-3:])
569572
```
570573
571-
#### Answer
574+
#### Solution
572575
573576
1. You can count backwards from the end with negative integers
574577
2. Indexing beyond the end of the collection is an error
@@ -711,16 +714,6 @@ for p in primes:
711714
print(p, squared, cubed)
712715
```
713716
714-
### (Optional) Use `range()` to iterate over a sequence of numbers
715-
716-
``` python
717-
for number in range(0, 3):
718-
print(number)
719-
```
720-
721-
- range() produces numbers on demand (a "generator" function)
722-
- useful for tracking progress
723-
724717
### Create a new collection from an existing collection
725718
726719
We will learn how to vectorize this when we get to Numpy and Pandas
@@ -747,22 +740,21 @@ for f in fruits:
747740
print(total)
748741
```
749742
750-
### (Optional) Use `enumerate()` to iterate over a sequence of items and their positions
743+
### (Optional) Use `range()` to iterate over a sequence of numbers
751744
752745
``` python
753-
for number, fruit in enumerate(fruits):
754-
print(number, ":", fruit)
746+
for number in range(0, 3):
747+
print(number)
755748
```
756749
757-
### (Optional) Accumulate a running total
750+
- range() produces numbers on demand (a "generator" function)
751+
- useful for tracking progress
758752
759-
Initialize an accumulator variable to zero, the empty string, or the empty list; then iteratively update the variable with values from a collection.
753+
### (Optional) Use `enumerate()` to iterate over a sequence of items and their positions
760754
761755
``` python
762-
total = 0
763-
for number in range(7):
764-
total = total + number
765-
print(total)
756+
for number, fruit in enumerate(fruits):
757+
print(number, ":", fruit)
766758
```
767759
768760
### (Optional) How do you know if an object is iterable?
@@ -922,45 +914,6 @@ for f in random_fruits:
922914
923915
3. What does `'-'.join(['x', 'y', 'z'])` generate?
924916
925-
### **(Optional) Challenge**: Locating the right module
926-
927-
You want to select a random character from a string:
928-
929-
``` python
930-
bases = 'ACTTGCTTGAC'
931-
```
932-
933-
1. Which standard library module could help you? <https://docs.python.org/3/library/>
934-
2. Which function would you select from that module? Are there alternatives?
935-
3. Try to write a program that uses the function.
936-
937-
#### Solutions:
938-
939-
1. You could try the `random` module. The string has 11 characters, each having a positional index from 0 to 10. You could use either `random.randrange` or `random.randint` functions to get a random integer between 0 and 10, and then pick out the character at that position:
940-
941-
``` python
942-
from random import randrange
943-
944-
random_index = randrange(len(bases))
945-
print(bases[random_index])
946-
```
947-
948-
…or more compactly:
949-
950-
``` python
951-
from random import randrange
952-
953-
print(bases[randrange(len(bases))])
954-
```
955-
956-
2. Perhaps you found the `random.sample()` function. It allows for slightly less typing:
957-
958-
``` python
959-
from random import sample
960-
961-
print(sample(bases, 1)[0])
962-
```
963-
964917
## (Optional) Dictionaries
965918
966919
### Dictionaries are sets of key/value pairs. Instead of being indexed by position, they are indexed by key.

README.org

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,11 @@ print("Fifth item:", fruits[4])
407407

408408
4. You can add an optional step interval (every 2nd item, every 3rd item, etc.)
409409
#+BEGIN_SRC python
410-
print("First 5 items, every other item:", fruits[0:5:2])
411-
print("Every third item:", fruits[::3])
410+
# First 5 items, every other item
411+
print(fruits[0:5:2])
412+
413+
# Every third item
414+
print(fruits[::3])
412415
#+END_SRC
413416

414417
*** (Optional) Why are lists indexed from 0?
@@ -436,7 +439,7 @@ print(fruits[20])
436439
print(fruits[-3:])
437440
#+END_SRC
438441

439-
**** Answer
442+
**** Solution
440443
1. You can count backwards from the end with negative integers
441444
2. Indexing beyond the end of the collection is an error
442445

@@ -557,15 +560,6 @@ for p in primes:
557560
print(p, squared, cubed)
558561
#+END_SRC
559562

560-
*** (Optional) Use ~range()~ to iterate over a sequence of numbers
561-
#+BEGIN_SRC python
562-
for number in range(0, 3):
563-
print(number)
564-
#+END_SRC
565-
566-
- range() produces numbers on demand (a "generator" function)
567-
- useful for tracking progress
568-
569563
*** Create a new collection from an existing collection
570564
We will learn how to vectorize this when we get to Numpy and Pandas
571565
#+BEGIN_SRC python
@@ -587,19 +581,19 @@ for f in fruits:
587581
print(total)
588582
#+END_SRC
589583

590-
*** (Optional) Use ~enumerate()~ to iterate over a sequence of items and their positions
584+
*** (Optional) Use ~range()~ to iterate over a sequence of numbers
591585
#+BEGIN_SRC python
592-
for number, fruit in enumerate(fruits):
593-
print(number, ":", fruit)
586+
for number in range(0, 3):
587+
print(number)
594588
#+END_SRC
595589

596-
*** (Optional) Accumulate a running total
597-
Initialize an accumulator variable to zero, the empty string, or the empty list; then iteratively update the variable with values from a collection.
590+
- range() produces numbers on demand (a "generator" function)
591+
- useful for tracking progress
592+
593+
*** (Optional) Use ~enumerate()~ to iterate over a sequence of items and their positions
598594
#+BEGIN_SRC python
599-
total = 0
600-
for number in range(7):
601-
total = total + number
602-
print(total)
595+
for number, fruit in enumerate(fruits):
596+
print(number, ":", fruit)
603597
#+END_SRC
604598

605599
*** (Optional) How do you know if an object is iterable?
@@ -733,41 +727,6 @@ for f in random_fruits:
733727
2. What does ~list('some string')~ do?
734728
3. What does ~'-'.join(['x', 'y', 'z'])~ generate?
735729

736-
*** *(Optional) Challenge*: Locating the right module
737-
You want to select a random character from a string:
738-
#+BEGIN_SRC python
739-
bases = 'ACTTGCTTGAC'
740-
#+END_SRC
741-
742-
1. Which standard library module could help you? https://docs.python.org/3/library/
743-
2. Which function would you select from that module? Are there alternatives?
744-
3. Try to write a program that uses the function.
745-
746-
**** Solutions:
747-
1. You could try the ~random~ module. The string has 11 characters, each having a positional index from 0 to 10. You could use either ~random.randrange~ or ~random.randint~ functions to get a random integer between 0 and 10, and then pick out the character at that position:
748-
749-
#+BEGIN_SRC python
750-
from random import randrange
751-
752-
random_index = randrange(len(bases))
753-
print(bases[random_index])
754-
#+END_SRC
755-
756-
...or more compactly:
757-
758-
#+BEGIN_SRC python
759-
from random import randrange
760-
761-
print(bases[randrange(len(bases))])
762-
#+END_SRC
763-
764-
2. Perhaps you found the ~random.sample()~ function. It allows for slightly less typing:
765-
#+BEGIN_SRC python
766-
from random import sample
767-
768-
print(sample(bases, 1)[0])
769-
#+END_SRC
770-
771730
** (Optional) Dictionaries
772731
*** Dictionaries are sets of key/value pairs. Instead of being indexed by position, they are indexed by key.
773732
#+BEGIN_SRC python

0 commit comments

Comments
 (0)