You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
4. You can add an optional step interval (every 2nd item, every 3rd item, etc.)
535
535
536
536
``` 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])
539
542
```
540
543
541
544
### (Optional) Why are lists indexed from 0?
@@ -568,7 +571,7 @@ print(fruits[20])
568
571
print(fruits[-3:])
569
572
```
570
573
571
-
#### Answer
574
+
#### Solution
572
575
573
576
1. You can count backwards from the end with negative integers
574
577
2. Indexing beyond the end of the collection is an error
@@ -711,16 +714,6 @@ for p in primes:
711
714
print(p, squared, cubed)
712
715
```
713
716
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
-
724
717
### Create a new collection from an existing collection
725
718
726
719
We will learn how to vectorize this when we get to Numpy and Pandas
@@ -747,22 +740,21 @@ for f in fruits:
747
740
print(total)
748
741
```
749
742
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
751
744
752
745
``` python
753
-
for number, fruit in enumerate(fruits):
754
-
print(number, ":", fruit)
746
+
for numberin range(0, 3):
747
+
print(number)
755
748
```
756
749
757
-
### (Optional) Accumulate a running total
750
+
- range() produces numbers on demand (a "generator" function)
751
+
- useful for tracking progress
758
752
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
760
754
761
755
``` 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)
766
758
```
767
759
768
760
### (Optional) How do you know if an object is iterable?
@@ -922,45 +914,6 @@ for f in random_fruits:
922
914
923
915
3. What does `'-'.join(['x', 'y', 'z'])` generate?
924
916
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
-
964
917
## (Optional) Dictionaries
965
918
966
919
### Dictionaries are sets of key/value pairs. Instead of being indexed by position, they are indexed by key.
4. You can add an optional step interval (every 2nd item, every 3rd item, etc.)
409
409
#+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])
412
415
#+END_SRC
413
416
414
417
*** (Optional) Why are lists indexed from 0?
@@ -436,7 +439,7 @@ print(fruits[20])
436
439
print(fruits[-3:])
437
440
#+END_SRC
438
441
439
-
**** Answer
442
+
**** Solution
440
443
1. You can count backwards from the end with negative integers
441
444
2. Indexing beyond the end of the collection is an error
442
445
@@ -557,15 +560,6 @@ for p in primes:
557
560
print(p, squared, cubed)
558
561
#+END_SRC
559
562
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
-
569
563
*** Create a new collection from an existing collection
570
564
We will learn how to vectorize this when we get to Numpy and Pandas
571
565
#+BEGIN_SRC python
@@ -587,19 +581,19 @@ for f in fruits:
587
581
print(total)
588
582
#+END_SRC
589
583
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
591
585
#+BEGIN_SRC python
592
-
for number, fruit in enumerate(fruits):
593
-
print(number, ":", fruit)
586
+
for numberin range(0, 3):
587
+
print(number)
594
588
#+END_SRC
595
589
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
598
594
#+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)
603
597
#+END_SRC
604
598
605
599
*** (Optional) How do you know if an object is iterable?
@@ -733,41 +727,6 @@ for f in random_fruits:
733
727
2. What does ~list('some string')~ do?
734
728
3. What does ~'-'.join(['x', 'y', 'z'])~ generate?
735
729
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
-
771
730
** (Optional) Dictionaries
772
731
*** Dictionaries are sets of key/value pairs. Instead of being indexed by position, they are indexed by key.
0 commit comments