# Chapter 13 While and for Loops

#### Two main looping constructs:

while-----'while' provides a way to code general loops

for------'for' is designed for stepping through the items in a sequence object

'while' and 'for' are the primary syntax for coding repeated actions
#### some statements used within loops:

break

continue

#### some loop generator:

range
zip
map

'range', 'zip' and 'map' are additional looping operations

#### Chapter 14 will talk about Python's iteration protocol and list comprehensions

I don't know what the iteration protocol is

#### Later chapters will explore some generators like filter and reduce.

```python
# While loops

# while True:
    # print("") # Press 'Ctrl-C to stop this iteration'

i = 123456789

while i%2 == 0:
    print("lbwnb")
```

```python
i
```

    123456789

```python
i%2 == 0
```

    False

```python
# Print the even numbers from 0 to 100:
i = 0

while i <=1000:
    if i%2==0:
        print(i,end=' ')
    i += 1

print('\n')

for i in range(0,1001):
    if i%2 ==0:
        print(i, end = ' ')
```

    0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 280 282 284 286 288 290 292 294 296 298 300 302 304 306 308 310 312 314 316 318 320 322 324 326 328 330 332 334 336 338 340 342 344 346 348 350 352 354 356 358 360 362 364 366 368 370 372 374 376 378 380 382 384 386 388 390 392 394 396 398 400 402 404 406 408 410 412 414 416 418 420 422 424 426 428 430 432 434 436 438 440 442 444 446 448 450 452 454 456 458 460 462 464 466 468 470 472 474 476 478 480 482 484 486 488 490 492 494 496 498 500 502 504 506 508 510 512 514 516 518 520 522 524 526 528 530 532 534 536 538 540 542 544 546 548 550 552 554 556 558 560 562 564 566 568 570 572 574 576 578 580 582 584 586 588 590 592 594 596 598 600 602 604 606 608 610 612 614 616 618 620 622 624 626 628 630 632 634 636 638 640 642 644 646 648 650 652 654 656 658 660 662 664 666 668 670 672 674 676 678 680 682 684 686 688 690 692 694 696 698 700 702 704 706 708 710 712 714 716 718 720 722 724 726 728 730 732 734 736 738 740 742 744 746 748 750 752 754 756 758 760 762 764 766 768 770 772 774 776 778 780 782 784 786 788 790 792 794 796 798 800 802 804 806 808 810 812 814 816 818 820 822 824 826 828 830 832 834 836 838 840 842 844 846 848 850 852 854 856 858 860 862 864 866 868 870 872 874 876 878 880 882 884 886 888 890 892 894 896 898 900 902 904 906 908 910 912 914 916 918 920 922 924 926 928 930 932 934 936 938 940 942 944 946 948 950 952 954 956 958 960 962 964 966 968 970 972 974 976 978 980 982 984 986 988 990 992 994 996 998 1000

    0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 280 282 284 286 288 290 292 294 296 298 300 302 304 306 308 310 312 314 316 318 320 322 324 326 328 330 332 334 336 338 340 342 344 346 348 350 352 354 356 358 360 362 364 366 368 370 372 374 376 378 380 382 384 386 388 390 392 394 396 398 400 402 404 406 408 410 412 414 416 418 420 422 424 426 428 430 432 434 436 438 440 442 444 446 448 450 452 454 456 458 460 462 464 466 468 470 472 474 476 478 480 482 484 486 488 490 492 494 496 498 500 502 504 506 508 510 512 514 516 518 520 522 524 526 528 530 532 534 536 538 540 542 544 546 548 550 552 554 556 558 560 562 564 566 568 570 572 574 576 578 580 582 584 586 588 590 592 594 596 598 600 602 604 606 608 610 612 614 616 618 620 622 624 626 628 630 632 634 636 638 640 642 644 646 648 650 652 654 656 658 660 662 664 666 668 670 672 674 676 678 680 682 684 686 688 690 692 694 696 698 700 702 704 706 708 710 712 714 716 718 720 722 724 726 728 730 732 734 736 738 740 742 744 746 748 750 752 754 756 758 760 762 764 766 768 770 772 774 776 778 780 782 784 786 788 790 792 794 796 798 800 802 804 806 808 810 812 814 816 818 820 822 824 826 828 830 832 834 836 838 840 842 844 846 848 850 852 854 856 858 860 862 864 866 868 870 872 874 876 878 880 882 884 886 888 890 892 894 896 898 900 902 904 906 908 910 912 914 916 918 920 922 924 926 928 930 932 934 936 938 940 942 944 946 948 950 952 954 956 958 960 962 964 966 968 970 972 974 976 978 980 982 984 986 988 990 992 994 996 998 1000

```python
i =0
while i <=100: # A header line  with a test expression
    if i%2==0: # body, indented statements
        print('%d is the even number'%i,end =', ')
    i += 1
```

    0 is the even number, 2 is the even number, 4 is the even number, 6 is the even number, 8 is the even number, 10 is the even number, 12 is the even number, 14 is the even number, 16 is the even number, 18 is the even number, 20 is the even number, 22 is the even number, 24 is the even number, 26 is the even number, 28 is the even number, 30 is the even number, 32 is the even number, 34 is the even number, 36 is the even number, 38 is the even number, 40 is the even number, 42 is the even number, 44 is the even number, 46 is the even number, 48 is the even number, 50 is the even number, 52 is the even number, 54 is the even number, 56 is the even number, 58 is the even number, 60 is the even number, 62 is the even number, 64 is the even number, 66 is the even number, 68 is the even number, 70 is the even number, 72 is the even number, 74 is the even number, 76 is the even number, 78 is the even number, 80 is the even number, 82 is the even number, 84 is the even number, 86 is the even number, 88 is the even number, 90 is the even number, 92 is the even number, 94 is the even number, 96 is the even number, 98 is the even number, 100 is the even number,

```python
i = 0
while i <=100:
    print(i, end =' ')
    i += 1
else: # Optional. Function: else will run statements below it, if and only if the text expression doesn't stand alone
    print('i is %d, bigger than 100. Thus, the loop will end here!'%i)
```

    0 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 i is 101, bigger than 100. Thus, the loop will end here!

```python
# Examples

1 == True # True is just a custom version of the integer 1 and always stands for a Boolean true value
1 is True
```

    False

```python
type(1), type (True) # '1' and 'True' are different things.
```

    (int, bool)

```python
# infinite loop
# While True:
#   print('Type Ctrl-C to stop me')

bool('')
type('')
len('')
```

    str

```python
x = 'spam'
while x:
    print(x,sep=',')
    x = x[1:]
```

    spam
    pam
    am
    m

```python
x = 'xswl'
print(x[0] +'=>x[0]',x[1] +'=>x[1]',x[2] +'=>x[2]',x[len(x)-1] +'=>x[3]',sep='     ')
x = x[1:]
print(x,x[0] +'=>x[0]',x[1] +'=>x[1]',x[2] +'=>x[2]',x[len(x)-1] +'=>x[2]',sep='         ')
```

    x=>x[0]     s=>x[1]     w=>x[2]     l=>x[3]
    swl         s=>x[0]         w=>x[1]         l=>x[2]         l=>x[2]

```python
a = 0; b = 10
while a < b:
    print(a, end = ' ')
    a += 1
```

    0 1 2 3 4 5 6 7 8 9

### Break, continue, pass, and the Loop else

#### break: jumps out of the closet enclosing loop (past the entire loop statement)

#### continue: jumps to the top of the closet enclosing loop (to the loop's header line)

#### pass: do nothing at all; placeholder

#### else: optional; be executed if and only if control exits the loop without hitting the break

```python
## General Loop Format

i = 1

while i <=100: # i = 1, pass  # i = 2, pass

    print(i)   # do this statement # execute this statement

    if i%2 == 0: # false # True when i = 2

        break    # Jumps out of the closet enclosing loop; Since it is false when i = 1, this operation will be executed

                 # The loop will end here when i = 2
    if i%3 == 0: # false

        continue # Won't be executed when i = 1

    i += 1 # i = i + 1, i =2

else:

    print("i is %d and the loop will end here!"%i)
```

    1
    2

```python
print(1%3==0, 2%3==0, 3%3==0)
```

    False False True

# Pass

```python
s = """Simple things first: the pass statement is a no-operation placeholder that is used when
the syntax requires a statement, but you have nothing useful to say. It is often used to
code an empty body for a compound statement. For instance, if you want to code an
infinite loop that does nothing each time through, do it with a pass"""
```

```python
print(s)
```

    Simple things first: the pass statement is a no-operation placeholder that is used when
    the syntax requires a statement, but you have nothing useful to say. It is often used to
    code an empty body for a compound statement. For instance, if you want to code an
    infinite loop that does nothing each time through, do it with a pass

```python
for i in s:
    print(i, end = ' ')
```

    S i m p l e   t h i n g s   f i r s t :   t h e   p a s s   s t a t e m e n t   i s   a   n o - o p e r a t i o n   p l a c e h o l d e r   t h a t   i s   u s e d   w h e n
     t h e   s y n t a x   r e q u i r e s   a   s t a t e m e n t ,   b u t   y o u   h a v e   n o t h i n g   u s e f u l   t o   s a y .   I t   i s   o f t e n   u s e d   t o
     c o d e   a n   e m p t y   b o d y   f o r   a   c o m p o u n d   s t a t e m e n t .   F o r   i n s t a n c e ,   i f   y o u   w a n t   t o   c o d e   a n
     i n f i n i t e   l o o p   t h a t   d o e s   n o t h i n g   e a c h   t i m e   t h r o u g h ,   d o   i t   w i t h   a   p a s s

```python
I = iter(s)
```

```python
I.__next__()
```

    'S'

```python
I.__next__()
```

    'i'

```python
from sympy import *
```

```python
init_session()
```

    IPython console for SymPy 1.4 (Python 3.7.4-64-bit) (ground types: python)

    These commands were executed:
    >>> from __future__ import division
    >>> from sympy import *
    >>> x, y, z, t = symbols('x y z t')
    >>> k, m, n = symbols('k m n', integer=True)
    >>> f, g, h = symbols('f g h', cls=Function)
    >>> init_printing()

    Documentation can be found at https://docs.sympy.org/1.4/

```python
solveset(11*x**2-56*x + 42,x)
```

```python
while (28/11-sqrt(322)/11) <= 0:
    pass
else:
    print('(28/11-sqrt(322)/11) is bigger than 0!')
```

    (28/11-sqrt(322)/11) is bigger than 0!

```python
## One of functions 'pass' has is 'to be filled in later'

def To_be_filled_in():
    pass
def To_be_filled_in_later():
    pass
```

```python
To_be_filled_in() # Won't give an error
```

```python
def To_be_filled_in_later():
    ...   # ... is ellipses; Three consecutive dots

To_be_filled_in_later() # won't give an error when this function is called
```

```python
x = ...
```

```python
x
```

    Ellipsis

### x = pass: Wrong, invalid syntax

# Continue

```python
x = 1234
while x:
    x -=1
    if x % 2 !=0: continue # if x%2 ！=0 (that is, x is an odd number), skip print
    print(x, end = ' ')
```

    1232 1230 1228 1226 1224 1222 1220 1218 1216 1214 1212 1210 1208 1206 1204 1202 1200 1198 1196 1194 1192 1190 1188 1186 1184 1182 1180 1178 1176 1174 1172 1170 1168 1166 1164 1162 1160 1158 1156 1154 1152 1150 1148 1146 1144 1142 1140 1138 1136 1134 1132 1130 1128 1126 1124 1122 1120 1118 1116 1114 1112 1110 1108 1106 1104 1102 1100 1098 1096 1094 1092 1090 1088 1086 1084 1082 1080 1078 1076 1074 1072 1070 1068 1066 1064 1062 1060 1058 1056 1054 1052 1050 1048 1046 1044 1042 1040 1038 1036 1034 1032 1030 1028 1026 1024 1022 1020 1018 1016 1014 1012 1010 1008 1006 1004 1002 1000 998 996 994 992 990 988 986 984 982 980 978 976 974 972 970 968 966 964 962 960 958 956 954 952 950 948 946 944 942 940 938 936 934 932 930 928 926 924 922 920 918 916 914 912 910 908 906 904 902 900 898 896 894 892 890 888 886 884 882 880 878 876 874 872 870 868 866 864 862 860 858 856 854 852 850 848 846 844 842 840 838 836 834 832 830 828 826 824 822 820 818 816 814 812 810 808 806 804 802 800 798 796 794 792 790 788 786 784 782 780 778 776 774 772 770 768 766 764 762 760 758 756 754 752 750 748 746 744 742 740 738 736 734 732 730 728 726 724 722 720 718 716 714 712 710 708 706 704 702 700 698 696 694 692 690 688 686 684 682 680 678 676 674 672 670 668 666 664 662 660 658 656 654 652 650 648 646 644 642 640 638 636 634 632 630 628 626 624 622 620 618 616 614 612 610 608 606 604 602 600 598 596 594 592 590 588 586 584 582 580 578 576 574 572 570 568 566 564 562 560 558 556 554 552 550 548 546 544 542 540 538 536 534 532 530 528 526 524 522 520 518 516 514 512 510 508 506 504 502 500 498 496 494 492 490 488 486 484 482 480 478 476 474 472 470 468 466 464 462 460 458 456 454 452 450 448 446 444 442 440 438 436 434 432 430 428 426 424 422 420 418 416 414 412 410 408 406 404 402 400 398 396 394 392 390 388 386 384 382 380 378 376 374 372 370 368 366 364 362 360 358 356 354 352 350 348 346 344 342 340 338 336 334 332 330 328 326 324 322 320 318 316 314 312 310 308 306 304 302 300 298 296 294 292 290 288 286 284 282 280 278 276 274 272 270 268 266 264 262 260 258 256 254 252 250 248 246 244 242 240 238 236 234 232 230 228 226 224 222 220 218 216 214 212 210 208 206 204 202 200 198 196 194 192 190 188 186 184 182 180 178 176 174 172 170 168 166 164 162 160 158 156 154 152 150 148 146 144 142 140 138 136 134 132 130 128 126 124 122 120 118 116 114 112 110 108 106 104 102 100 98 96 94 92 90 88 86 84 82 80 78 76 74 72 70 68 66 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 0

```python
x = 10
while x:
    x -= 1
    if x%2 == 0:
        print(x, end=' ')
```

    8 6 4 2 0

```python
# Break

while True:
    name = input('Enter name:')
    if name == 'stop': break
    age = input('Enter age: ')
    print("Hello", name, '=>', int(age))
```

    Enter name: Kingslayer
    Enter age:  25

    Hello Kingslayer => 25

    Enter name: Arya
    Enter age:  15

    Hello Arya => 15

    Enter name: Bran
    Enter age:  14

    Hello Bran => 14

    Enter name: John snow
    Enter age:  20

    Hello John snow => 20

    Enter name: stop

```python
while True:
    name = input('请输入名字：')
    if name == 'Ned Stark':
        break
        print('Ned Stark is dead after Season 3') # Useless
    age = input("请输入年龄：")
    print('哈喽，', name, '=>', age)
```

    请输入名字： Ned Stark

# Loop else

```python
import random
def numerate(k):
    return random.randint(1,k)

y = numerate(100)
x = y //2
while x > 1:
    if y % x == 0:
        print(y, 'has factor', x)
      # break
    x -= 1
else:
    print(y, 'is prime')
```

    71 is prime

```python
bool(y%21166==0)
```

    False

```python
int(42332/21166)
```

    2

```python
# More on the loop else
# The codes here are just notion

found = False

while x and not found:
    if match(x[0]):
        print('Ni')
        found = True
    else:
        x = x[1:]
if not found:
    print('not found')
```

    Ni

```python
x = """Because the loop else clause is unique to Python, it tends to perplex some newcomers.
In general terms, the loop else provides explicit syntax for a common coding scenario—
it is a coding structure that lets us catch the “other” way out of a loop, without setting
and checking flags or conditions."""
```

```python
L = []
for i in x:
    L.append(i)
```

```python
print(L)
```

    ['B', 'e', 'c', 'a', 'u', 's', 'e', ' ', 't', 'h', 'e', ' ', 'l', 'o', 'o', 'p', ' ', 'e', 'l', 's', 'e', ' ', 'c', 'l', 'a', 'u', 's', 'e', ' ', 'i', 's', ' ', 'u', 'n', 'i', 'q', 'u', 'e', ' ', 't', 'o', ' ', 'P', 'y', 't', 'h', 'o', 'n', ',', ' ', 'i', 't', ' ', 't', 'e', 'n', 'd', 's', ' ', 't', 'o', ' ', 'p', 'e', 'r', 'p', 'l', 'e', 'x', ' ', 's', 'o', 'm', 'e', ' ', 'n', 'e', 'w', 'c', 'o', 'm', 'e', 'r', 's', '.', '\n', 'I', 'n', ' ', 'g', 'e', 'n', 'e', 'r', 'a', 'l', ' ', 't', 'e', 'r', 'm', 's', ',', ' ', 't', 'h', 'e', ' ', 'l', 'o', 'o', 'p', ' ', 'e', 'l', 's', 'e', ' ', 'p', 'r', 'o', 'v', 'i', 'd', 'e', 's', ' ', 'e', 'x', 'p', 'l', 'i', 'c', 'i', 't', ' ', 's', 'y', 'n', 't', 'a', 'x', ' ', 'f', 'o', 'r', ' ', 'a', ' ', 'c', 'o', 'm', 'm', 'o', 'n', ' ', 'c', 'o', 'd', 'i', 'n', 'g', ' ', 's', 'c', 'e', 'n', 'a', 'r', 'i', 'o', '—', '\n', 'i', 't', ' ', 'i', 's', ' ', 'a', ' ', 'c', 'o', 'd', 'i', 'n', 'g', ' ', 's', 't', 'r', 'u', 'c', 't', 'u', 'r', 'e', ' ', 't', 'h', 'a', 't', ' ', 'l', 'e', 't', 's', ' ', 'u', 's', ' ', 'c', 'a', 't', 'c', 'h', ' ', 't', 'h', 'e', ' ', '“', 'o', 't', 'h', 'e', 'r', '”', ' ', 'w', 'a', 'y', ' ', 'o', 'u', 't', ' ', 'o', 'f', ' ', 'a', ' ', 'l', 'o', 'o', 'p', ',', ' ', 'w', 'i', 't', 'h', 'o', 'u', 't', ' ', 's', 'e', 't', 't', 'i', 'n', 'g', '\n', 'a', 'n', 'd', ' ', 'c', 'h', 'e', 'c', 'k', 'i', 'n', 'g', ' ', 'f', 'l', 'a', 'g', 's', ' ', 'o', 'r', ' ', 'c', 'o', 'n', 'd', 'i', 't', 'i', 'o', 'n', 's', '.']

```python
x = L
```

```python
def match(x):
    if x == 't':
        return True
    else:
        return False
```

```python
# Equivalent code:

while x:
    if match(x[0]):
        print('Ni')
        break
    x = x[1:]
else:
    print('Not found')
```

    Ni

```python
S1 = 'abcdefghijklmnopqrstuvwxyz'
L2 = []
for j in S1:
    L2.append(j)
```

```python
print(L2)
```

    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

```python
for i in L2:
    if i in x:
        print(i in x, end = ', ')
    else:
        print('%s is not in x'%i, end =', ')
```

    True, b is not in x, True, True, True, True, True, True, True, j is not in x, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, z is not in x,

```python
def match(x):
    if x == 'b':
        return True
    else:
        return False

while x:
    if match(x[0]):
        print('Ni')
        break
    x = x[1:]
else:
    print('Not found')
```

    Not found

```python
def match(x):
    if x == 'j':
        return True
    else:
        return False

while x:
    if match(x[0]):
        print('Ni')
        break
    x = x[1:]
else:
    print('Not found')
```

    Not found

```python
def match(x):
    if x == 'z':
        return True
    else:
        return False

while x:
    if match(x[0]):
        print('Ni')
        break
    x = x[1:]
else:
    print('Not found')
```

    Not found

```python
next?
```

    Docstring:
    next(iterator[, default])

    Return the next item from the iterator. If default is given and the iterator
    is exhausted, it is returned instead of raising StopIteration.
    Type:      builtin_function_or_method

```python
print(L)
```

    ['B', 'e', 'c', 'a', 'u', 's', 'e', ' ', 't', 'h', 'e', ' ', 'l', 'o', 'o', 'p', ' ', 'e', 'l', 's', 'e', ' ', 'c', 'l', 'a', 'u', 's', 'e', ' ', 'i', 's', ' ', 'u', 'n', 'i', 'q', 'u', 'e', ' ', 't', 'o', ' ', 'P', 'y', 't', 'h', 'o', 'n', ',', ' ', 'i', 't', ' ', 't', 'e', 'n', 'd', 's', ' ', 't', 'o', ' ', 'p', 'e', 'r', 'p', 'l', 'e', 'x', ' ', 's', 'o', 'm', 'e', ' ', 'n', 'e', 'w', 'c', 'o', 'm', 'e', 'r', 's', '.', '\n', 'I', 'n', ' ', 'g', 'e', 'n', 'e', 'r', 'a', 'l', ' ', 't', 'e', 'r', 'm', 's', ',', ' ', 't', 'h', 'e', ' ', 'l', 'o', 'o', 'p', ' ', 'e', 'l', 's', 'e', ' ', 'p', 'r', 'o', 'v', 'i', 'd', 'e', 's', ' ', 'e', 'x', 'p', 'l', 'i', 'c', 'i', 't', ' ', 's', 'y', 'n', 't', 'a', 'x', ' ', 'f', 'o', 'r', ' ', 'a', ' ', 'c', 'o', 'm', 'm', 'o', 'n', ' ', 'c', 'o', 'd', 'i', 'n', 'g', ' ', 's', 'c', 'e', 'n', 'a', 'r', 'i', 'o', '—', '\n', 'i', 't', ' ', 'i', 's', ' ', 'a', ' ', 'c', 'o', 'd', 'i', 'n', 'g', ' ', 's', 't', 'r', 'u', 'c', 't', 'u', 'r', 'e', ' ', 't', 'h', 'a', 't', ' ', 'l', 'e', 't', 's', ' ', 'u', 's', ' ', 'c', 'a', 't', 'c', 'h', ' ', 't', 'h', 'e', ' ', '“', 'o', 't', 'h', 'e', 'r', '”', ' ', 'w', 'a', 'y', ' ', 'o', 'u', 't', ' ', 'o', 'f', ' ', 'a', ' ', 'l', 'o', 'o', 'p', ',', ' ', 'w', 'i', 't', 'h', 'o', 'u', 't', ' ', 's', 'e', 't', 't', 'i', 'n', 'g', '\n', 'a', 'n', 'd', ' ', 'c', 'h', 'e', 'c', 'k', 'i', 'n', 'g', ' ', 'f', 'l', 'a', 'g', 's', ' ', 'o', 'r', ' ', 'c', 'o', 'n', 'd', 'i', 't', 'i', 'o', 'n', 's', '.']

```python
''.join(L)
```

    'Because the loop else clause is unique to Python, it tends to perplex some newcomers.\nIn general terms, the loop else provides explicit syntax for a common coding scenario—\nit is a coding structure that lets us catch the “other” way out of a loop, without setting\nand checking flags or conditions.'

```python
iterator1 = iter(L)
```

```python
next(iterator1)
```

    'B'

# For loops

```python
S = 'The for loop is a generic sequence iterator in Python: it can step through the items in any ordered sequence object. '
```

```python
S1 = "The for loop is a generic sequence iterator in Python: it can step through the items in any ordered sequence object. "
```

```python
S2 = """The for loop is a generic sequence iterator in Python: it can step through the items in any ordered sequence object. """
```

```python
S4 = "The for loop is 'a generic sequence'"
```

```python
L = [] # empty list
for i in S:
    L.append(i)
```

```python
print(L)
```

    ['T', 'h', 'e', ' ', 'f', 'o', 'r', ' ', 'l', 'o', 'o', 'p', ' ', 'i', 's', ' ', 'a', ' ', 'g', 'e', 'n', 'e', 'r', 'i', 'c', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'i', 't', 'e', 'r', 'a', 't', 'o', 'r', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n', ':', ' ', 'i', 't', ' ', 'c', 'a', 'n', ' ', 's', 't', 'e', 'p', ' ', 't', 'h', 'r', 'o', 'u', 'g', 'h', ' ', 't', 'h', 'e', ' ', 'i', 't', 'e', 'm', 's', ' ', 'i', 'n', ' ', 'a', 'n', 'y', ' ', 'o', 'r', 'd', 'e', 'r', 'e', 'd', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'o', 'b', 'j', 'e', 'c', 't', '.', ' ']

```python
L2 = []
for i in L:
    L2.append(L)
```

```python
''.join(L)
```

    'The for loop is a generic sequence iterator in Python: it can step through the items in any ordered sequence object. '

```python
T = ('1','2','3')
type(T)
```

    tuple

```python
print(tuple(L))
```

    ('T', 'h', 'e', ' ', 'f', 'o', 'r', ' ', 'l', 'o', 'o', 'p', ' ', 'i', 's', ' ', 'a', ' ', 'g', 'e', 'n', 'e', 'r', 'i', 'c', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'i', 't', 'e', 'r', 'a', 't', 'o', 'r', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n', ':', ' ', 'i', 't', ' ', 'c', 'a', 'n', ' ', 's', 't', 'e', 'p', ' ', 't', 'h', 'r', 'o', 'u', 'g', 'h', ' ', 't', 'h', 'e', ' ', 'i', 't', 'e', 'm', 's', ' ', 'i', 'n', ' ', 'a', 'n', 'y', ' ', 'o', 'r', 'd', 'e', 'r', 'e', 'd', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'o', 'b', 'j', 'e', 'c', 't', '.', ' ')

```python
S
```

    'The for loop is a generic sequence iterator in Python: it can step through the items in any ordered sequence object. '

```python
print(list(S))
```

    ['T', 'h', 'e', ' ', 'f', 'o', 'r', ' ', 'l', 'o', 'o', 'p', ' ', 'i', 's', ' ', 'a', ' ', 'g', 'e', 'n', 'e', 'r', 'i', 'c', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'i', 't', 'e', 'r', 'a', 't', 'o', 'r', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n', ':', ' ', 'i', 't', ' ', 'c', 'a', 'n', ' ', 's', 't', 'e', 'p', ' ', 't', 'h', 'r', 'o', 'u', 'g', 'h', ' ', 't', 'h', 'e', ' ', 'i', 't', 'e', 'm', 's', ' ', 'i', 'n', ' ', 'a', 'n', 'y', ' ', 'o', 'r', 'd', 'e', 'r', 'e', 'd', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'o', 'b', 'j', 'e', 'c', 't', '.', ' ']

```python
str(L)
```

    "['T', 'h', 'e', ' ', 'f', 'o', 'r', ' ', 'l', 'o', 'o', 'p', ' ', 'i', 's', ' ', 'a', ' ', 'g', 'e', 'n', 'e', 'r', 'i', 'c', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'i', 't', 'e', 'r', 'a', 't', 'o', 'r', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n', ':', ' ', 'i', 't', ' ', 'c', 'a', 'n', ' ', 's', 't', 'e', 'p', ' ', 't', 'h', 'r', 'o', 'u', 'g', 'h', ' ', 't', 'h', 'e', ' ', 'i', 't', 'e', 'm', 's', ' ', 'i', 'n', ' ', 'a', 'n', 'y', ' ', 'o', 'r', 'd', 'e', 'r', 'e', 'd', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'o', 'b', 'j', 'e', 'c', 't', '.', ' ']"

```python
print(tuple(S))
```

    ('T', 'h', 'e', ' ', 'f', 'o', 'r', ' ', 'l', 'o', 'o', 'p', ' ', 'i', 's', ' ', 'a', ' ', 'g', 'e', 'n', 'e', 'r', 'i', 'c', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'i', 't', 'e', 'r', 'a', 't', 'o', 'r', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n', ':', ' ', 'i', 't', ' ', 'c', 'a', 'n', ' ', 's', 't', 'e', 'p', ' ', 't', 'h', 'r', 'o', 'u', 'g', 'h', ' ', 't', 'h', 'e', ' ', 'i', 't', 'e', 'm', 's', ' ', 'i', 'n', ' ', 'a', 'n', 'y', ' ', 'o', 'r', 'd', 'e', 'r', 'e', 'd', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'o', 'b', 'j', 'e', 'c', 't', '.', ' ')

```python
T = tuple(S)
```

```python
print(list(T))
```

    ['T', 'h', 'e', ' ', 'f', 'o', 'r', ' ', 'l', 'o', 'o', 'p', ' ', 'i', 's', ' ', 'a', ' ', 'g', 'e', 'n', 'e', 'r', 'i', 'c', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'i', 't', 'e', 'r', 'a', 't', 'o', 'r', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n', ':', ' ', 'i', 't', ' ', 'c', 'a', 'n', ' ', 's', 't', 'e', 'p', ' ', 't', 'h', 'r', 'o', 'u', 'g', 'h', ' ', 't', 'h', 'e', ' ', 'i', 't', 'e', 'm', 's', ' ', 'i', 'n', ' ', 'a', 'n', 'y', ' ', 'o', 'r', 'd', 'e', 'r', 'e', 'd', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'o', 'b', 'j', 'e', 'c', 't', '.', ' ']

strings => lists => tuples;
lists !=> strings => tuples;
tuples => strings;

```python
str(T)
```

    "('T', 'h', 'e', ' ', 'f', 'o', 'r', ' ', 'l', 'o', 'o', 'p', ' ', 'i', 's', ' ', 'a', ' ', 'g', 'e', 'n', 'e', 'r', 'i', 'c', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'i', 't', 'e', 'r', 'a', 't', 'o', 'r', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n', ':', ' ', 'i', 't', ' ', 'c', 'a', 'n', ' ', 's', 't', 'e', 'p', ' ', 't', 'h', 'r', 'o', 'u', 'g', 'h', ' ', 't', 'h', 'e', ' ', 'i', 't', 'e', 'm', 's', ' ', 'i', 'n', ' ', 'a', 'n', 'y', ' ', 'o', 'r', 'd', 'e', 'r', 'e', 'd', ' ', 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', ' ', 'o', 'b', 'j', 'e', 'c', 't', '.', ' ')"

```python
# The other built-in iterables: file
```

```python
for i in [0,1,2,3,4,5,6,7,8,9,10]:
    # i = i + 1
    if i >= 6: break
    print(i,end=' ')
else:
    print("Raise an error!")
```

    0 1 2 3 4 5

```python
i == 6
```

    True

```python
from sympy import *
```

```python
init_session()
```

    IPython console for SymPy 1.4 (Python 3.7.4-64-bit) (ground types: python)

    These commands were executed:
    >>> from __future__ import division
    >>> from sympy import *
    >>> x, y, z, t = symbols('x y z t')
    >>> k, m, n = symbols('k m n', integer=True)
    >>> f, g, h = symbols('f g h', cls=Function)
    >>> init_printing()

    Documentation can be found at https://docs.sympy.org/1.4/

```python
a, b, c = symbols('a b c')
```

```python
solveset(a*x**2+ b*x + c, x)
```

# Examples

```python
for x in ["spam","eggs","ham"]:
    print(x, end = ' ')
```

    spam eggs ham

```python
L = [1,2,3,4]
sum(L)
```

```python
sum = 0
for x in [1,2,3,4]:
    sum = sum + x
```

```python
sum
```

```python
init_session()
```

    IPython console for SymPy 1.4 (Python 3.7.4-64-bit) (ground types: python)

    These commands were executed:
    >>> from __future__ import division
    >>> from sympy import *
    >>> x, y, z, t = symbols('x y z t')
    >>> k, m, n = symbols('k m n', integer=True)
    >>> f, g, h = symbols('f g h', cls=Function)
    >>> init_printing()

    Documentation can be found at https://docs.sympy.org/1.4/

```python
del sum
```

```python
sum(L)
```

```python
symbols = 0
```

```python
for i in range(10):
    symbols +=i
```

```python
print(symbols)
```

    45

```python
type(symbols)
```

    int

```python
from sympy import *
x, y, z, t = symbols('x y z t')
```

```python
x, y, z, t
```

```python
summation = 0
for i in range(101):
    summation += i
print(summation)
```

    5050

```python
prod = 1
for item in [1,2,3,4]:
    prod *= item
```

```python
prod
```

```python
from sympy import *
i = symbols('i')
```

```python
product(i,(i,1,4))
```

```python
type((1,2))
```

    tuple

```python
type([1,2,3,4,5,6])
```

    list

A list of tuples

```python
T = [(1,2),(3,4),(5,6)]
```

```python
for (a,b) in T:
    print(a,b)
```

    1 2
    3 4
    5 6

```python
zip?
```

    Init signature: zip(self, /, *args, **kwargs)
    Docstring:
    zip(iter1 [,iter2 [...]]) --> zip object

    Return a zip object whose .__next__() method returns a tuple where
    the i-th element comes from the i-th iterable argument.  The .__next__()
    method continues until the shortest iterable in the argument sequence
    is exhausted and then it raises StopIteration.
    Type:           type
    Subclasses:

```python
Tu = zip([1,2],[3,4],[5,6])
```

```python
for i in Tu:
    print(i)
```

    (1, 3, 5)
    (2, 4, 6)

```python
map?
```

    Init signature: map(self, /, *args, **kwargs)
    Docstring:
    map(func, *iterables) --> map object

    Make an iterator that computes the function using arguments from
    each of the iterables.  Stops when the shortest iterable is exhausted.
    Type:           type
    Subclasses:

```python
D = {'a': 1, 'b': 2, 'c': 3}
```

```python
for key in D:
    print(key, '=>', D[key])
```

    a => 1
    b => 2
    c => 3

```python
D
```

    {'a': 1, 'b': 2, 'c': 3}

```python
list(D)
```

    ['a', 'b', 'c']

```python
list(D.items())
```

    [('a', 1), ('b', 2), ('c', 3)]

```python
D.items()
```

    dict_items([('a', 1), ('b', 2), ('c', 3)])

```python
D['a'] = '1','2'
```

```python
D['b'] = '3','4'
D['c'] = '5', '6'
```

```python
for (key,value) in D.items():
    print(key, '=>', value)
```

    a => ('1', '2')
    b => ('3', '4')
    c => ('5', '6')

```python
from turtle import *

# 设置色彩模式是RGB:
colormode(255)
lt(90)
lv = 14
l = 120
s = 45
width(lv)
# 初始化RGB颜色:
r = 0
g = 0
b = 0
pencolor(r, g, b)
penup()
bk(l)
pendown()
fd(l)
def draw_tree(l, level):
    global r, g, b
    # save the current pen width
    w = width()

    # narrow the pen width
    width(w * 3.0 / 4.0)
    # set color:
    r = r + 1
    g = g + 2
    b = b + 3
    pencolor(r % 200, g % 200, b % 200)
    l = 3.0 / 4.0 * l
    lt(s)
    fd(l)
    if level < lv:
        draw_tree(l, level + 1)
    bk(l)
    rt(2 * s)
    fd(l)

    if level < lv:
        draw_tree(l, level + 1)
    bk(l)
    lt(s)

    # restore the previous pen width
    width(w)

speed("fastest")

draw_tree(l, 4)

done()
```

```python
#KocheDraw1
import turtle
def koch(size,n):
  if n==1:
    turtle.fd(size)
  else:
    for i in [0,60,-120,60]:
      turtle.left(i)
      koch(size/3,n-1)
def main():
  turtle.setup(600,600)
  turtle.penup()
  turtle.speed(10)
  turtle.hideturtle()
  turtle.pensize(2)
  turtle.goto(-200,100)
  turtle.pendown()
  level=4
  koch(400,level)
  turtle.right(120)
  koch(400, level)
  turtle.right(120)
  koch(400, level)
  turtle.penup()
  turtle.done()
main()
```

```python
import turtle as t

t.speed(10)
t.pensize(8)
t.hideturtle()
t.screensize(500, 500, bg='white')

# 猫脸
t.fillcolor('#00A1E8')
t.begin_fill()
t.circle(120)
t.end_fill()

t.pensize(3)
t.fillcolor('white')
t.begin_fill()
t.circle(100)
t.end_fill()

t.pu()
t.home()
t.goto(0, 134)
t.pd()
t.pensize(4)
t.fillcolor("#EA0014")
t.begin_fill()
t.circle(18)
t.end_fill()

t.pu()
t.goto(7, 155)
t.pensize(2)
t.color('white', 'white')
t.pd()
t.begin_fill()
t.circle(4)
t.end_fill()

t.pu()
t.goto(-30, 160)
t.pensize(4)
t.pd()
t.color('black', 'white')
t.begin_fill()
a = 0.4
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a+0.08
        t.lt(3) #向左转3度
        t.fd(a) #向前走a的步长
    else:
        a = a-0.08
        t.lt(3)
        t.fd(a)
t.end_fill()

t.pu()
t.goto(30, 160)
t.pensize(4)
t.pd()
t.color('black', 'white')
t.begin_fill()
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a+0.08
        t.lt(3)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a-0.08
        t.lt(3)
        t.fd(a)
t.end_fill()

t.pu()
t.goto(-38,190)
t.pensize(8)
t.pd()
t.right(-30)
t.forward(15)
t.right(70)
t.forward(15)

t.pu()
t.goto(15, 185)
t.pensize(4)
t.pd()
t.color('black', 'black')
t.begin_fill()
t.circle(13)
t.end_fill()

t.pu()
t.goto(13, 190)
t.pensize(2)
t.pd()
t.color('white', 'white')
t.begin_fill()
t.circle(5)
t.end_fill()

t.pu()
t.home()
t.goto(0, 134)
t.pensize(4)
t.pencolor('black')
t.pd()
t.right(90)
t.forward(40)

t.pu()
t.home()
t.goto(0, 124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(10)
t.forward(80)

t.pu()
t.home()
t.goto(0, 114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(6)
t.forward(80)

t.pu()
t.home()
t.goto(0,104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(0)
t.forward(80)

# 左边的胡子
t.pu()
t.home()
t.goto(0,124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(170)
t.forward(80)

t.pu()
t.home()
t.goto(0, 114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(174)
t.forward(80)

t.pu()
t.home()
t.goto(0, 104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(180)
t.forward(80)

t.pu()
t.goto(-70, 70)
t.pd()
t.color('black', 'red')
t.pensize(6)
t.seth(-60)
t.begin_fill()
t.circle(80,40)
t.circle(80,80)
t.end_fill()

t.pu()
t.home()
t.goto(-80,70)
t.pd()
t.forward(160)

t.pu()
t.home()
t.goto(-50,50)
t.pd()
t.pensize(1)
t.fillcolor("#eb6e1a")
t.seth(40)
t.begin_fill()
t.circle(-40, 40)
t.circle(-40, 40)
t.seth(40)
t.circle(-40, 40)
t.circle(-40, 40)
t.seth(220)
t.circle(-80, 40)
t.circle(-80, 40)
t.end_fill()

# 领带
t.pu()
t.goto(-70, 12)
t.pensize(14)
t.pencolor('red')
t.pd()
t.seth(-20)
t.circle(200, 30)
t.circle(200, 10)

# 铃铛
t.pu()
t.goto(0, -46)
t.pd()
t.pensize(3)
t.color("black", '#f8d102')
t.begin_fill()
t.circle(25)
t.end_fill()

t.pu()
t.goto(-5, -40)
t.pd()
t.pensize(2)
t.color("black", '#79675d')
t.begin_fill()
t.circle(5)
t.end_fill()

t.pensize(3)
t.right(115)
t.forward(7)

t.mainloop()
```

```python
# coding: utf-8
import turtle as t
t.pensize(4)
t.hideturtle()
t.colormode(255)
t.setup(840,500)
t.speed(5)

# Nose
t.color((255,155,192),"pink")
t.pencolor(255,155,192)
t.pu()
t.goto(-100,100)
t.pd()
t.seth(-30)
t.begin_fill()
a=0.4
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a + 0.08
        t.lt(3) # 向左转3度
        t.fd(a) # 向前走a的步长
    else:
        a = a -0.08
        t.lt(3)
        t.fd(a)
t.end_fill()

t.pu()
t.seth(90)
t.fd(25)
t.seth(10)
t.fd(10)
t.pd()
t.pencolor(255,155,192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160,82,45)
t.end_fill()

t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255,155,192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160,82,45)
t.end_fill()

# 头
t.color((255,155,192),"pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300,-30)
t.circle(100,-60)
t.circle(80,-100)
t.circle(150,-20)
t.circle(60,-95)
t.seth(161)
t.circle(-300,15)
t.pu()
t.goto(-100,100)
t.pd()
t.seth(-30)
a = 0.4
for i in range(60):
    if 0 <= i < 30 or 60 <= i <90:
        a = a + 0.08
        t.lt(3) # 向左转3度
        t.fd(a) # 向前走a的步长
    else:
        a = a - 0.08
        t.lt(3)
        t.fd(a)
t.end_fill()

# 耳朵
t.color((255,155,192),"pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,54)
t.end_fill()

t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,56)
t.end_fill()

# 眼睛
t.color((255,155,192),"white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()

t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()

t.color((255,155,192),"white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()

t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()

# 腮
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()

# 嘴
t.color(239,69,19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30,40)
t.circle(40,80)

#身体
t.color("red",(255,99,71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100,10)
t.circle(300,30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300,30)
t.circle(100,3)
t.color((255,155,192),(255,100,100))
t.seth(-135)
t.circle(-80,63)
t.circle(-150,24)
t.end_fill()

# 手
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300,15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20,90)

t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300,15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20,90)

# 脚
t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)

t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)

# 尾巴
t.pensize(4)
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70,20)
t.circle(10,330)
t.circle(70,30)
t.done()
```

```python
from sympy import *
```

```python
init_session()
```

    IPython console for SymPy 1.4 (Python 3.7.4-64-bit) (ground types: python)

    These commands were executed:
    >>> from __future__ import division
    >>> from sympy import *
    >>> x, y, z, t = symbols('x y z t')
    >>> k, m, n = symbols('k m n', integer=True)
    >>> f, g, h = symbols('f g h', cls=Function)
    >>> init_printing()

    Documentation can be found at https://docs.sympy.org/1.4/

```python
from sympy import tan
```

```python
A = symbols('A')
```

```python
a = solve((1 + tan(A))/(1-tan(A))-2005,A)
```

```python
from sympy import sec
```

```python
from sympy import substitution
```

```python
type(a)
```

    list

```python
a[0]
```

```python
float(sec(2*a[0]) + tan(2*a[0]))
```

求证：〖sin〗^3  a/3+3*〖sin〗^3  a/3^2 +⋯+3^(n-1)*〖sin〗^3  a/3^n =1/4(3^n*sin a/3^n -sina)

```python
from sympy import *
init_session()
```

    IPython console for SymPy 1.4 (Python 3.7.4-64-bit) (ground types: python)

    These commands were executed:
    >>> from __future__ import division
    >>> from sympy import *
    >>> x, y, z, t = symbols('x y z t')
    >>> k, m, n = symbols('k m n', integer=True)
    >>> f, g, h = symbols('f g h', cls=Function)
    >>> init_printing()

    Documentation can be found at https://docs.sympy.org/1.4/

```python
a = symbols('a')
from sympy import sin
```

```python
# 当 n = 1
simplify(Sum(3**(n-1)*(sin(a/(3**n)))**3,(n,1,2)).doit()-1/4*(3**2*sin(a/(3**2))-sin(a)))
```

```python
# 当 n = 2
simplify(Sum(3**(n-1)*(sin(a/(3**n)))**3,(n,1,2)).doit()-1/4*(3**2*sin(a/(3**2))-sin(a)))
```

```python
# 当 n = 5
simplify(Sum(3**(n-1)*(sin(a/(3**n)))**3,(n,1,5)).doit()-1/4*(3**5*sin(a/(3**5))-sin(a)))
```

```python
Sum(3**(n-1)*(sin(a/(3**n)))**3,(n,1,n))
left = Sum(3**(n-1)*(sin(a/(3**n)))**3,(n,1,n))
```

```python
1/4*(3**n*sin(a/(3**n))-sin(a))
right = 1/4*(3**n*sin(a/(3**n))-sin(a))
```

```python
from sympy import substitution
```

```python
from sympy import *
```

```python
init_session()
```

    IPython console for SymPy 1.4 (Python 3.7.4-64-bit) (ground types: python)

    These commands were executed:
    >>> from __future__ import division
    >>> from sympy import *
    >>> x, y, z, t = symbols('x y z t')
    >>> k, m, n = symbols('k m n', integer=True)
    >>> f, g, h = symbols('f g h', cls=Function)
    >>> init_printing()

    Documentation can be found at https://docs.sympy.org/1.4/

```python
i = symbols('i', integer = True)
```

```python
type(i)
```

    sympy.core.symbol.Symbol

```python
Sum(i,(i,1,100)).doit()
```

```python
a = symbols('a')
```

```python
Sum(3**(n-1)*(sin(a/(3**n))**3),(n,1,k))
```

```python
left = Sum(3**(n-1)*(sin(a/(3**n))**3),(n,1,k))
```

```python
factor(1/4*(3**n*sin(a/(3**n))-sin(a)))
```

```python
right = factor(1/4*(3**n*sin(a/(3**n))-sin(a)))
```

```python
simplify(left - right)
```

```python
# n = 1
simplify(left.subs(k,1).doit()-right.subs(n,1))
```

```python
# n = 5
simplify(left.subs(k,5).doit()-right.subs(n,5))
```

```python
right.subs(n,5)
```

```python
left.subs(k,5).doit()
```

```python
# n = 19
simplify(left.subs(k,19).doit()-right.subs(n,19))
```

```python
a, b = symbols('a b', real=True)
```

```python
a.is_real and b.is_real
```

    True

```python
left = sin(2*a+b)/sin(a)-2*cos(a+b)
```

```python
right = sin(b)/sin(a)
```

```python
simplify(left - right).doit()
```

```python
# %load untitled0.py
"""
Created on Sat Feb 15 22:35:00 2020

@author: NickC
"""
from PIL import Image
from wordcloud import WordCloud, ImageColorGenerator
import matplotlib.pyplot as plt
import numpy as np
import  jieba
def GetWordCloud():
    path_txt = 'C://Users/NickC/all.txt'
    path_img = 'C://Users/NickC/2.jpg'
    f = open(path_txt, 'r', encoding='UTF-8').read()
    background_image = np.array(Image.open(path_img))
   # 结巴分词，生成字符串，如果不通过分词，无法直接生成正确的中文词云,感兴趣的朋友可以去查一下，有多种分词模式
   #Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。
    cut_text = " ".join(jieba.cut(f))

    wordcloud = WordCloud(
        # 设置字体，不然会出现口字乱码，文字的路径是电脑的字体一般路径，可以换成别的
       font_path="C:/Windows/Fonts/simfang.ttf",
       background_color="white",
       # mask参数=图片背景，必须要写上，另外有mask参数再设定宽高是无效的
       mask=background_image).generate(cut_text)
    # 生成颜色值
    image_colors = ImageColorGenerator(background_image)
    # 下面代码表示显示图片
    plt.imshow(wordcloud.recolor(color_func=image_colors), interpolation="bilinear")
    plt.axis("off")
    plt.show()

if __name__ == '__main__':
    GetWordCloud()
```

```python
# coding=utf-8

import turtle
from datetime import *

# 抬起画笔，向前运动一段距离放下
def Skip(step):
    turtle.penup()
    turtle.forward(step)
    turtle.pendown()

def mkHand(name, length):
    # 注册Turtle形状，建立表针Turtle
    turtle.reset()
    Skip(-length * 0.1)
    # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
    turtle.begin_poly()
    turtle.forward(length * 1.1)
    # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
    turtle.end_poly()
    # 返回最后记录的多边形。
    handForm = turtle.get_poly()
    turtle.register_shape(name, handForm)

def Init():
    global secHand, minHand, hurHand, printer
    # 重置Turtle指向北
    turtle.mode("logo")
    # 建立三个表针Turtle并初始化
    mkHand("secHand", 135)
    mkHand("minHand", 90)
    mkHand("hurHand", 45)
    secHand = turtle.Turtle()
    secHand.shape("secHand")
    secHand.color('red')
    minHand = turtle.Turtle()
    minHand.color('purple')
    minHand.shape("minHand")
    hurHand = turtle.Turtle()
    hurHand.color('yellow')
    hurHand.shape("hurHand")

    for hand in secHand, minHand, hurHand:
        hand.shapesize(1, 1, 3)
        hand.speed(0)

    # 建立输出文字Turtle
    printer = turtle.Turtle()
    printer.color('green')
    # 隐藏画笔的turtle形状
    printer.hideturtle()
    printer.penup()

def SetupClock(radius):
    # 建立表的外框
    turtle.reset()
    turtle.bgpic(r'yang.gif')
    turtle.color('green')
    turtle.pensize(7)
    for i in range(60):
        Skip(radius)
        if i % 5 == 0:
            turtle.color('blue')
            turtle.forward(20)
            Skip(-radius - 20)
            Skip(radius + 20)
            if i == 0:
                turtle.write(int(12), align="center", font=("Courier", 14, "bold"))
            elif i == 30:
                Skip(25)
                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
                Skip(-25)
            elif (i == 25 or i == 35):
                Skip(20)
                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
                Skip(-20)
            else:
                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
            Skip(-radius - 20)
        else:
            turtle.dot(5)
            Skip(-radius)
        turtle.right(6)

def Week(t):
    week = ["星期一", "星期二", "星期三",
            "星期四", "星期五", "星期六", "星期日"]
    return week[t.weekday()]

def Date(t):
    y = t.year
    m = t.month
    d = t.day
    return "%s/%d/%d" % (y, m, d)

def Tick():
    # 绘制表针的动态显示
    t = datetime.today()
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second / 60.0
    hour = t.hour + minute / 60.0
    secHand.setheading(6 * second)
    minHand.setheading(6 * minute)
    hurHand.setheading(30 * hour)

    turtle.tracer(False)
    printer.forward(65)
    printer.write(Week(t), align="center",
                  font=("Courier", 14, "bold"))
    turtle.color('purple')
    printer.back(130)
    printer.write(Date(t), align="center",
                  font=("Courier", 14, "bold"))
    printer.back(30)
   # printer.write('大古需要光', align='center',font=("Courier",14,"bold"))
    printer.home()
    turtle.tracer(True)

    # 100ms后继续调用tick
    turtle.ontimer(Tick, 100)

def main():
    # 打开/关闭龟动画，并为更新图纸设置延迟。
    turtle.tracer(False)
    Init()
    SetupClock(160)
    turtle.tracer(True)
    Tick()
    turtle.mainloop()

if __name__ == "__main__":
    main()
```

    Exception in Tkinter callback
    Traceback (most recent call last):
      File "D:\Anaconda\lib\tkinter\__init__.py", line 1705, in __call__
        return self.func(*args)
      File "D:\Anaconda\lib\tkinter\__init__.py", line 749, in callit
        func(*args)
      File "<ipython-input-11-91f74a4f4976>", line 105, in Tick
        hurHand.setheading(30 * hour)
      File "D:\Anaconda\lib\turtle.py", line 1936, in setheading
        self._rotate(angle)
      File "D:\Anaconda\lib\turtle.py", line 3278, in _rotate
        self._update()
      File "D:\Anaconda\lib\turtle.py", line 2660, in _update
        self._update_data()
      File "D:\Anaconda\lib\turtle.py", line 2646, in _update_data
        self.screen._incrementudc()
      File "D:\Anaconda\lib\turtle.py", line 1292, in _incrementudc
        raise Terminator
    turtle.Terminator

```python
file = open('woshicik.txt',encoding='utf-8')
```

```python
file.read?
```

    Signature: file.read(size=-1, /)
    Docstring:
    Read at most n characters from stream.

    Read from underlying buffer until we have n characters or we hit EOF.
    If n is negative or omitted, read until EOF.
    Type:      builtin_function_or_method

```python
while True:
    char = file.read(1) # Read 1 character by 1 character
    if not char: break
    print(char, end='')
```

```python
for char in open('woshicik.txt', encoding = 'utf-8').readline():
    print(char, end='')
```

    爱凑热闹

```python
for i in iter(open('woshicik.txt', encoding = 'utf-8').readline()):
    print(i, end= '')
```

    爱凑热闹

# Loop Coding Techniques
>>> for loop is simpler than while loop. For loop is prior to be used
>>> some special situations: 1. when you need to visit every second or third item in a list;
                             2. Change the list along the way;
    when you encounter these situtations, you should think about using while loop rather than for loop.
>>> Two built-ins provided by Python can help you traverse more than one sequence in parallel, in the same for loop.
>>> Two built-ins: 1. range function; 2. zip function

>>> for loops run quicker than while loops. So, use for loops as much as possible and think about two built-ins, i.e., range function & zip function to help you solve some complex problems.

```python
print(range(5)); print(list(range(5)))
print(range(2,5)); print(list(range(2,5)))
print(range(0,10,2)); print(list(range(0,10,2)))
```

    range(0, 5)
    [0, 1, 2, 3, 4]
    range(2, 5)
    [2, 3, 4]
    range(0, 10, 2)
    [0, 2, 4, 6, 8]

>>> ranges can be negative and descending if you want them to be.

```python
print(list(range(-5,5)))
print(list(range(5,-5,-1)))
```

    [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
    [5, 4, 3, 2, 1, 0, -1, -2, -3, -4]

>>> To print certain number, e.g., n, of lines, try to use range(n)

```python
for i in range(10):
    print(i, file.readline(), end='')
```

    0 暗影陷阱
    1 暗月光斩
    2 暗阵
    3 傲风残花
    4 傲天斗法
    5 把持术
    6 拔刀斩
    7 霸皇拳
    8 拔击
    9 巴雷特狙击

```python
X = 'spam'
for item in X: print(item,end=' ')
```

    s p a m

```python
i = 0
while i < len(X):
    print(X[i],end=' ')
    i+=1
```

    s p a m
>>> Manual indexing

```python
X
```

    'spam'

```python
len(X)
```

    4

```python
list(range(len(X)))
```

    [0, 1, 2, 3]

```python
for i in range(len(X)): print(X[i], end=' ')
```

    s p a m

# Nonexhaustive Traversals: range and Slices
>>> General rule: use for loops instead of while loops whenever possible>>> The simplest form of for loop:

```python
for item in X: print(item,end=' ')
```

    s p a m

```python
S = """However, the coding pattern used in the prior example does allow us to do more specialized sorts of traversals. For instance, we can skip items as we go:"""
```

```python
print(S.split(), end = ' ')
```

    ['However,', 'the', 'coding', 'pattern', 'used', 'in', 'the', 'prior', 'example', 'does', 'allow', 'us', 'to', 'do', 'more', 'specialized', 'sorts', 'of', 'traversals.', 'For', 'instance,', 'we', 'can', 'skip', 'items', 'as', 'we', 'go:']

```python
for i in range(0, len(S.split()), 1): print(S.split()[i], end =' ')
```

    However, the coding pattern used in the prior example does allow us to do more specialized sorts of traversals. For instance, we can skip items as we go:

```python
for c in S.split()[::1]: print(c, end = ' ')
```

    However, the coding pattern used in the prior example does allow us to do more specialized sorts of traversals. For instance, we can skip items as we go:
>>> if you're dealing with very large strings or lsits, try to use range even though it is more complex
than using slice with stride. Because range function will not copy the string or copy a list in Python 3.0, thus saving more memory.
# Changing lists: range. The combination of range wiht for loops
>>> A simple for loop won't work, i.e., to change a list in place.

```python
L = S.split()
```

```python
for x in L:
    x += 'xswl'
```

```python
print(L, end= '' )
```

    ['However,', 'the', 'coding', 'pattern', 'used', 'in', 'the', 'prior', 'example', 'does', 'allow', 'us', 'to', 'do', 'more', 'specialized', 'sorts', 'of', 'traversals.', 'For', 'instance,', 'we', 'can', 'skip', 'items', 'as', 'we', 'go:']

```python
x
```

    'go:xswl'

>>> To change a list in place, use range funciton

```python
for i in range(len(L)):
    L[i] += 'xswl'
```

```python
print(L, end = '')
```

    ['However,xswl', 'thexswl', 'codingxswl', 'patternxswl', 'usedxswl', 'inxswl', 'thexswl', 'priorxswl', 'examplexswl', 'doesxswl', 'allowxswl', 'usxswl', 'toxswl', 'doxswl', 'morexswl', 'specializedxswl', 'sortsxswl', 'ofxswl', 'traversals.xswl', 'Forxswl', 'instance,xswl', 'wexswl', 'canxswl', 'skipxswl', 'itemsxswl', 'asxswl', 'wexswl', 'go:xswl']

```python
S = """印度这次修订可谓大刀阔斧、魄力惊人：制造业月度指数强行绑定于同类商品，比如印度生产了一辆印度国产车，强行与市场上的奥迪车等价。"""
```

```python
from jieba import *
```

```python
L = list(cut(S))
```

    Building prefix dict from the default dictionary ...
    Loading model from cache C:\Users\NickC\AppData\Local\Temp\jieba.cache
    Loading model cost 0.971 seconds.
    Prefix dict has been built successfully.

```python
for i in range(len(L)):
    L[i] += ', xswl'
```

```python
i = 0
L = list(cut(S))
while i < len(L):
    L[i] += ', nb'
    i += 1
```

```python
S = """印度这次修订可谓大刀阔斧、魄力惊人：制造业月度指数强行绑定于同类商品，比如印度生产了一辆印度国产车，强行与市场上的奥迪车等价。"""
```

# Parallel Traversals: zip and map

```python
S = """The last example in the prior section works, but it’s not the fastest option. It’s also
more work than we need to do. """
```

```python
L = S.split()
```

```python
for i in L:
    print(i, end =' ')
```

    The last example in the prior section works, but it’s not the fastest option. It’s also more work than we need to do.

```python
for i in range(0, len(L), 4):
    print(L[i], end = ' ')
```

    The the but fastest more need

```python
L1 = list(range(1,5,1))
L2 = list(range(5,9,1))
```

```python
L1, L2
```

    ([1, 2, 3, 4], [5, 6, 7, 8])

```python
z = list(zip(L1,L2))
```

```python
for x, y in zip(L1, L2): print(x, y, '--', x+y)
```

    1 5 -- 6
    2 6 -- 8
    3 7 -- 10
    4 8 -- 12

```python
(x, y) == (4,8)
```

    True

```python
file = open('woshicik.txt',encoding='utf-8')
```

```python
len(list(enumerate(file.read())))
```

    4115

```python
print(list(zip(list(range(1,4116,1)), file)), end =' ')
```

    [(1, '爱凑热闹\n'), (2, '艾实\n'), (3, '暗杀\n'), (4, '安文逸\n'), (5, '暗无天日\n'), (6, '暗夜斗篷\n'), (7, '暗夜飞影\n'), (8, '暗夜系\n'), (9, '暗影斗篷\n'), (10, '暗影烈焰\n'), (11, '暗影陷阱\n'), (12, '暗月光斩\n'), (13, '暗阵\n'), (14, '傲风残花\n'), (15, '傲天斗法\n'), (16, '把持术\n'), (17, '拔刀斩\n'), (18, '霸皇拳\n'), (19, '拔击\n'), (20, '巴雷特狙击\n'), (21, '霸气雄图\n'), (22, '霸碎\n'), (23, '霸图\n'), (24, '霸王连拳\n'), (25, '八音符\n'), (26, '白光剑\n'), (27, '百花\n'), (28, '百花谷\n'), (29, '百花缭乱\n'), (30, '百流斩\n'), (31, '百龙流星打\n'), (32, '白莫\n'), (33, '白祁\n'), (34, '白庶\n'), (35, '白言飞\n'), (36, '白夜黑昼\n'), (37, '搬山\n'), (38, '半透明\n'), (39, '暴风雪\n'), (40, '包荣兴\n'), (41, '暴射\n'), (42, '爆缩式手雷\n'), (43, '爆炎弹\n'), (44, '爆炎符\n'), (45, '爆炎龙\n'), (46, '爆炎式手雷\n'), (47, '包子\n'), (48, '包子入侵\n'), (49, '暴走\n'), (50, '背灯弹\n'), (51, '贝克克\n'), (52, '背身缚首术\n'), (53, '背摔\n'), (54, '本子\n'), (55, '崩拳\n'), (56, '崩山击\n'), (57, '笔言飞\n'), (58, '鞭挞\n'), (59, '冰创波动剑\n'), (60, '冰创波动阵\n'), (61, '冰弹\n'), (62, '冰封鬼斩\n'), (63, '冰魂\n'), (64, '冰狼\n'), (65, '冰墙\n'), (66, '冰属性炫纹\n'), (67, '冰霜波动剑\n'), (68, '冰霜波动阵\n'), (69, '冰霜鬼神\n'), (70, '冰霜雪球\n'), (71, '冰线\n'), (72, '冰雨\n'), (73, '冰渣\n'), (74, '冰阵\n'), (75, '不动如山\n'), (76, '步法\n'), (77, '步法立冰\n'), (78, '步法逆雷\n'), (79, '步法迎风\n'), (80, '步法转火\n'), (81, '捕猎者\n'), (82, '捕食者\n'), (83, '残忍静默\n'), (84, '残影\n'), (85, '操纵术\n'), (86, '曾信然\n'), (87, '拆迁流\n'), (88, '拆投\n'), (89, '长河落日\n'), (90, '常先\n'), (91, '潮汐\n'), (92, '车前子\n'), (93, '陈果\n'), (94, '晨露\n'), (95, '诚实的风暴反击\n'), (96, '程泰\n'), (97, '橙武\n'), (98, '橙装\n'), (99, '冲锋\n'), (100, '冲拳\n'), (101, '冲撞刺击\n'), (102, '楚云秀\n'), (103, '穿心刺\n'), (104, '春易老\n'), (105, '磁场线圈\n'), (106, '刺弹炮\n'), (107, '刺客\n'), (108, '刺刃风暴\n'), (109, '刺射弹\n'), (110, '刺藤\n'), (111, '脆豆\n'), (112, '催眠\n'), (113, '寸劲\n'), (114, '错手刺\n'), (115, '大回复术\n'), (116, '大戟\n'), (117, '大漠孤烟\n'), (118, '大治愈术\n'), (119, '呆毛周\n'), (120, '戴妍琦\n'), (121, '丹心\n'), (122, '弹药扩充\n'), (123, '弹药专家\n'), (124, '刀刀拔刀\n'), (125, '刀魂\n'), (126, '盗贼\n'), (127, '倒斩\n'), (128, '刀阵\n'), (129, '德里罗\n'), (130, '邓复生\n'), (131, '灯花夜\n'), (132, '地雷震\n'), (133, '地裂波动剑\n'), (134, '地裂斩\n'), (135, '地上拂\n'), (136, '第十区\n'), (137, '地心斩首术\n'), (138, '第一流氓\n'), (139, '巅峰荣耀\n'), (140, '电光波动阵\n'), (141, '电视剧\n'), (142, '点心大大\n'), (143, '电子眼\n'), (144, '定身符\n'), (145, '定时式手雷\n'), (146, '冬虫夏草\n'), (147, '斗破山河\n'), (148, '斗神\n'), (149, '斗者意志\n'), (150, '独活\n'), (151, '杜明\n'), (152, '毒气瓦斯式手雷\n'), (153, '毒云陷阱\n'), (154, '毒针\n'), (155, '断灭\n'), (156, '队长\n'), (157, '盾反\n'), (158, '盾击\n'), (159, '盾墙\n'), (160, '遁身之术\n'), (161, '堕落天使\n'), (162, '耳光\n'), (163, '法不容情\n'), (164, '法师系\n'), (165, '法术反射\n'), (166, '翻滚\n'), (167, '繁花血景\n'), (168, '反坦克炮\n'), (169, '犯罪组合\n'), (170, '防风\n'), (171, '方明华\n'), (172, '方锐\n'), (173, '方世镜\n'), (174, '方士谦\n'), (175, '方学才\n'), (176, '飞刀剑\n'), (177, '飞火流星\n'), (178, '飞龙在天\n'), (179, '废物点心\n'), (180, '飞扬\n'), (181, '焚山\n'), (182, '分水刺\n'), (183, '分烟景\n'), (184, '风暴反击\n'), (185, '风残草尽\n'), (186, '风城烟雨\n'), (187, '封禁符\n'), (188, '风景杀\n'), (189, '风卷流云\n'), (190, '风刻\n'), (191, '锋芒慧剑\n'), (192, '逢山鬼泣\n'), (193, '风梳烟沐\n'), (194, '冯宪君\n'), (195, '冯向明\n'), (196, '风转云流\n'), (197, '副队长\n'), (198, '伏虎腾翔\n'), (199, '复活术\n'), (200, '浮空弹\n'), (201, '伏龙翔天\n'), (202, '腐蚀术\n'), (203, '拂晓\n'), (204, '盖才捷\n'), (205, '感电式手雷\n'), (206, '钢筋铁骨\n'), (207, '高飞脚\n'), (208, '高杰\n'), (209, '高踢\n'), (210, '高英杰\n'), (211, '哥布林\n'), (212, '格挡\n'), (213, '格斗系\n'), (214, '隔河仙\n'), (215, '割喉\n'), (216, '格林机枪\n'), (217, '葛兆蓝\n'), (218, '跟随\n'), (219, '工皮寿\n'), (220, '公正的英勇跳跃\n'), (221, '勾魂\n'), (222, '勾拳\n'), (223, '固守\n'), (224, '顾夕夜\n'), (225, '孤饮\n'), (226, '冠军\n'), (227, '光电环\n'), (228, '光明之证\n'), (229, '鬼步\n'), (230, '鬼灯萤火\n'), (231, '鬼剑士\n'), (232, '鬼刻\n'), (233, '鬼魅才\n'), (234, '鬼迷神疑\n'), (235, '归去来兮\n'), (236, '鬼神盛宴\n'), (237, '鬼影缠身\n'), (238, '鬼斩\n'), (239, '鬼爪\n'), (240, '国家队\n'), (241, '郭明宇\n'), (242, '郭少\n'), (243, '郭阳\n'), (244, '哈里斯\n'), (245, '海无量\n'), (246, '寒冰粉\n'), (247, '寒冰符\n'), (248, '寒冰雨\n'), (249, '韩文清\n'), (250, '寒烟柔\n'), (251, '豪龙破军\n'), (252, '豪炎龙\n'), (253, '贺铭\n'), (254, '何伟堂\n'), (255, '贺武\n'), (256, '黑暗骑士\n'), (257, '黑暗之爪\n'), (258, '红莲天舞\n'), (259, '轰天炮\n'), (260, '吼叫\n'), (261, '弧光闪\n'), (262, '护体真气\n'), (263, '呼啸\n'), (264, '虎之印\n'), (265, '滑铲\n'), (266, '花繁似锦\n'), (267, '花开堪折\n'), (268, '话唠\n'), (269, '幻影龙牙\n'), (270, '幻影无形剑\n'), (271, '黄烦烦\n'), (272, '皇风\n'), (273, '荒火\n'), (274, '黄金一代\n'), (275, '黄金右手\n'), (276, '黄少\n'), (277, '黄少天\n'), (278, '回风式\n'), (279, '回复术\n'), (280, '恢复术\n'), (281, '灰黑色\n'), (282, '毁人不倦\n'), (283, '回旋踢\n'), (284, '回旋折脚\n'), (285, '回云\n'), (286, '灰阵\n'), (287, '混乱之雨\n'), (288, '混乱之爪\n'), (289, '魂御\n'), (290, '火箭拳\n'), (291, '火箭推进器\n'), (292, '火属性炫纹\n'), (293, '火舞流炎\n'), (294, '火焰爆弹\n'), (295, '火焰符\n'), (296, '火焰陷阱\n'), (297, '火炎斩\n'), (298, '火之鸟\n'), (299, '疾风手里剑\n'), (300, '疾光波动剑\n'), (301, '激光炮\n'), (302, '击魂术\n'), (303, '寂静之阵\n'), (304, '疾空踏\n'), (305, '疾空踢\n'), (306, '季冷\n'), (307, '技能\n'), (308, '技能卷轴\n'), (309, '疾跑\n'), (310, '即死领悟\n'), (311, '击退\n'), (312, '极限生存\n'), (313, '机械空投\n'), (314, '机械拳\n'), (315, '机械师\n'), (316, '机械旋翼\n'), (317, '机械追踪\n'), (318, '疾行\n'), (319, '加农炮\n'), (320, '嘉世\n'), (321, '贾世明\n'), (322, '加速符\n'), (323, '加速器\n'), (324, '贾兴\n'), (325, '剑定天下\n'), (326, '坚定意志\n'), (327, '剑客\n'), (328, '剑落长空\n'), (329, '剑刃风暴\n'), (330, '剑圣\n'), (331, '剑士系\n'), (332, '剑武精通\n'), (333, '剑影步\n'), (334, '江波涛\n'), (335, '蒋游\n'), (336, '僵直弹\n'), (337, '蛟龙出海\n'), (338, '交替\n'), (339, '劫风\n'), (340, '截脉\n'), (341, '接投\n'), (342, '街头风暴\n'), (343, '接投级\n'), (344, '杰西卡\n'), (345, '净化\n'), (346, '静寂之阵\n'), (347, '精灵献祭\n'), (348, '精灵召唤\n'), (349, '静默之阵\n'), (350, '精神启示\n'), (351, '静心符\n'), (352, '镜月\n'), (353, '九点水\n'), (354, '飓风\n'), (355, '飓风炮\n'), (356, '绝地风暴\n'), (357, '绝对零度\n'), (358, '绝色\n'), (359, '觉醒\n'), (360, '君莫笑\n'), (361, '卡西本\n'), (362, '空蝉双杀\n'), (363, '空绞杀\n'), (364, '空气压缩机\n'), (365, '空手入白刃\n'), (366, '空跃\n'), (367, '空中灌篮\n'), (368, '狂暴\n'), (369, '狂剑士\n'), (370, '蓝河\n'), (371, '蓝桥春雪\n'), (372, '拦山虎\n'), (373, '蓝溪阁\n'), (374, '蓝雨\n'), (375, '蓝装\n'), (376, '狼头蒜\n'), (377, '老板娘\n'), (378, '乐乐\n'), (379, '雷电贯穿\n'), (380, '雷电光环\n'), (381, '雷光炼狱\n'), (382, '雷鸣电光\n'), (383, '雷霆\n'), (384, '雷霆末日\n'), (385, '雷鹰\n'), (386, '冷暗雷\n'), (387, '立冰\n'), (388, '离恨剑\n'), (389, '李华\n'), (390, '李轩\n'), (391, '李迅\n'), (392, '李艺博\n'), (393, '李亦辉\n'), (394, '李远\n'), (395, '连进\n'), (396, '联盟\n'), (397, '联盟第一直男\n'), (398, '联盟女神\n'), (399, '连突\n'), (400, '连突刺\n'), (401, '梁方\n'), (402, '梁易春\n'), (403, '量子炮\n'), (404, '缭影乱武\n'), (405, '裂波斩\n'), (406, '烈火焰尽\n'), (407, '裂空掌\n'), (408, '裂杀闪瞬灭\n'), (409, '猎寻\n'), (410, '烈炎波动剑\n'), (411, '烈焰波动剑\n'), (412, '烈焰波动阵\n'), (413, '烈焰冲击\n'), (414, '烈焰风暴\n'), (415, '烈焰符\n'), (416, '烈焰红拳\n'), (417, '林暗草惊\n'), (418, '林枫\n'), (419, '临海\n'), (420, '林敬言\n'), (421, '林易\n'), (422, '灵魂语者\n'), (423, '零零柒\n'), (424, '灵猫\n'), (425, '零下九度\n'), (426, '柳非\n'), (427, '六个核桃\n'), (428, '刘皓\n'), (429, '流氓\n'), (430, '流木\n'), (431, '刘小别\n'), (432, '六星光牢\n'), (433, '流星式\n'), (434, '流云\n'), (435, '流云吹\n'), (436, '龙牙\n'), (437, '楼冠宁\n'), (438, '卢瀚文\n'), (439, '鲁洛\n'), (440, '鲁奕宁\n'), (441, '乱魂甲\n'), (442, '乱雷\n'), (443, '鸾辂音尘\n'), (444, '鸾珞音尘\n'), (445, '乱射\n'), (446, '乱身冲\n'), (447, '轮回\n'), (448, '落凤锤\n'), (449, '落凤斩\n'), (450, '落花狼藉\n'), (451, '落花掌\n'), (452, '罗辑\n'), (453, '落雷符\n'), (454, '罗塔\n'), (455, '螺旋念气杀\n'), (456, '螺旋气冲\n'), (457, '落英式\n'), (458, '螺转旋风杀\n'), (459, '吕泊远\n'), (460, '马后炮\n'), (461, '马踏西风\n'), (462, '麻针\n'), (463, '满月斩\n'), (464, '昧光\n'), (465, '猛虎乱舞\n'), (466, '米修远\n'), (467, '灭绝星尘\n'), (468, '灭神的诅咒\n'), (469, '灭天尊\n'), (470, '明青\n'), (471, '莫白\n'), (472, '莫楚晨\n'), (473, '莫楚辰\n'), (474, '魔道学者\n'), (475, '魔法弹\n'), (476, '魔法禁锢\n'), (477, '魔法射线\n'), (478, '莫凡\n'), (479, '莫敢回手\n'), (480, '魔剑士\n'), (481, '魔界之花\n'), (482, '魔镜\n'), (483, '魔术师\n'), (484, '木恩\n'), (485, '沐沐\n'), (486, '牧师\n'), (487, '沐雨橙风\n'), (488, '暮云深\n'), (489, '逆风刺\n'), (490, '逆光的十字星\n'), (491, '逆雷\n'), (492, '逆流\n'), (493, '念龙波\n'), (494, '念气罩\n'), (495, '宁远\n'), (496, '怒龙穿心\n'), (497, '怒龙穿心破\n'), (498, '怒血狂涛\n'), (499, '潘林\n'), (500, '胖大海\n'), (501, '抛沙\n'), (502, '抛投\n'), (503, '破甲炮\n'), (504, '破精\n'), (505, '破空式\n'), (506, '破力\n'), (507, '破灭斩\n'), (508, '破敏\n'), (509, '破魔斩\n'), (510, '破体\n'), (511, '破天\n'), (512, '破智\n'), (513, '气波弹\n'), (514, '气波盾\n'), (515, '气冲云水\n'), (516, '气定神闲\n'), (517, '气功爆破\n'), (518, '气功师\n'), (519, '气贯长虹\n'), (520, '气流直下\n'), (521, '气刃\n'), (522, '骑士\n'), (523, '骑士精神\n'), (524, '七叶一枝花\n'), (525, '汽油瓶\n'), (526, '气转流云\n'), (527, '千成\n'), (528, '前方隔海\n'), (529, '浅花迷人\n'), (530, '千机伞\n'), (531, '千斤坠\n'), (532, '千念怒放\n'), (533, '浅生离\n'), (534, '前踢\n'), (535, '钱文举\n'), (536, '潜行\n'), (537, '千叶离若\n'), (538, '强力膝袭\n'), (539, '枪淋弹雨\n'), (540, '强龙压\n'), (541, '枪炮师\n'), (542, '枪炮武术\n'), (543, '枪手系\n'), (544, '枪术精通\n'), (545, '枪王\n'), (546, '乔一帆\n'), (547, '切割术\n'), (548, '秦牧云\n'), (549, '轻裁\n'), (550, '清扫\n'), (551, '青之驱\n'), (552, '邱非\n'), (553, '秋葵\n'), (554, '秋木苏\n'), (555, '驱魔师\n'), (556, '驱散粉\n'), (557, '曲射\n'), (558, '拳法家\n'), (559, '拳皇\n'), (560, '全神贯注\n'), (561, '全透明\n'), (562, '雀落\n'), (563, '却邪\n'), (564, '燃烧弹\n'), (565, '燃烧箭矢\n'), (566, '燃烧之箭\n'), (567, '绕岸垂杨\n'), (568, '热感飞弹\n'), (569, '忍法\n'), (570, '忍法百流斩\n'), (571, '忍法爆炎龙\n'), (572, '忍法背身缚首术\n'), (573, '忍法地心斩首术\n'), (574, '忍法断灭\n'), (575, '忍法遁身之术\n'), (576, '忍法火炎斩\n'), (577, '忍法火焰斩\n'), (578, '忍法空蝉双杀\n'), (579, '忍法乱身冲\n'), (580, '忍法雀落\n'), (581, '忍法缩地术\n'), (582, '忍法替身\n'), (583, '忍法替身术\n'), (584, '忍法影分身术\n'), (585, '忍法樱杀碎月\n'), (586, '忍法影舞\n'), (587, '忍具撒菱\n'), (588, '忍具烟玉\n'), (589, '任俊驰\n'), (590, '人去也\n'), (591, '忍者\n'), (592, '熔岩烧瓶\n'), (593, '荣耀\n'), (594, '荣耀第一拳法家\n'), (595, '荣耀第一人\n'), (596, '荣耀第一阵鬼\n'), (597, '荣耀教科书\n'), (598, '荣耀世界邀请赛\n'), (599, '荣誉挑衅\n'), (600, '柔道\n'), (601, '入夜寒\n'), (602, '如影随形\n'), (603, '阮永彬\n'), (604, '撒菱\n'), (605, '赛丁昆\n'), (606, '三段斩\n'), (607, '三界六道\n'), (608, '三零一度\n'), (609, '散人\n'), (610, '三石\n'), (611, '扫把旋风\n'), (612, '扫把掌握\n'), (613, '扫地焚香\n'), (614, '森罗\n'), (615, '沙暴陷阱\n'), (616, '杀意波动\n'), (617, '闪存\n'), (618, '闪电光速拳\n'), (619, '山逢地裂\n'), (620, '闪光百裂\n'), (621, '闪光弹\n'), (622, '闪烁突刺\n'), (623, '闪影\n'), (624, '伤疤之痛\n'), (625, '上挑\n'), (626, '韶光换\n'), (627, '舍命一击\n'), (628, '申建\n'), (629, '神奇\n'), (630, '神枪手\n'), (631, '神圣之火\n'), (632, '神说要有光\n'), (633, '沈万河\n'), (634, '神佑之光\n'), (635, '神之领域\n'), (636, '圣盾术\n'), (637, '圣光打\n'), (638, '圣光摸你\n'), (639, '圣回复术\n'), (640, '圣诫之光\n'), (641, '生灵灭\n'), (642, '升龙斩\n'), (643, '生命的白银\n'), (644, '生命激活\n'), (645, '生命陷落\n'), (646, '升天阵\n'), (647, '圣言回复\n'), (648, '圣言治愈\n'), (649, '圣职系\n'), (650, '圣治愈术\n'), (651, '石不转\n'), (652, '噬魂血手\n'), (653, '使君子\n'), (654, '十六叶\n'), (655, '十六夜\n'), (656, '嗜血\n'), (657, '嗜血奋战\n'), (658, '失血奋战\n'), (659, '十字军审判\n'), (660, '十字军制裁\n'), (661, '十字斩\n'), (662, '守护美德\n'), (663, '守护使者\n'), (664, '守护天使\n'), (665, '手雷\n'), (666, '手里剑\n'), (667, '守灵者\n'), (668, '兽王四元素阵\n'), (669, '首席枪炮师\n'), (670, '束缚术\n'), (671, '曙光旋冰\n'), (672, '舒可欣\n'), (673, '舒可怡\n'), (674, '术士\n'), (675, '摔投\n'), (676, '双重控制\n'), (677, '双虎掌\n'), (678, '双手飞\n'), (679, '双月牙\n'), (680, '谁不低头\n'), (681, '瞬间移动\n'), (682, '瞬身刺\n'), (683, '四大心脏\n'), (684, '死灵纠缠\n'), (685, '四轮天舞\n'), (686, '死亡墓碑\n'), (687, '死亡骑士\n'), (688, '死亡之门\n'), (689, '死亡之手\n'), (690, '宋奇英\n'), (691, '宋晓\n'), (692, '苏合香\n'), (693, '苏妹子\n'), (694, '苏沐橙\n'), (695, '苏沐秋\n'), (696, '速射\n'), (697, '酸雨干冰\n'), (698, '酸雨乾冰\n'), (699, '碎风波动剑\n'), (700, '碎踝\n'), (701, '碎霜\n'), (702, '碎随风\n'), (703, '孙浩文\n'), (704, '孙亮\n'), (705, '孙明进\n'), (706, '孙翔\n'), (707, '孙哲平\n'), (708, '缩地术\n'), (709, '锁喉\n'), (710, '索克萨尔\n'), (711, '踏破虚空\n'), (712, '踏射\n'), (713, '叹息之壁\n'), (714, '探爪狼\n'), (715, '唐昊\n'), (716, '唐礼升\n'), (717, '唐柔\n'), (718, '唐三打\n'), (719, '涛落沙明\n'), (720, '逃脱\n'), (721, '藤刺\n'), (722, '替身草人\n'), (723, '天击\n'), (724, '天雷地火\n'), (725, '天链\n'), (726, '天南星\n'), (727, '田七\n'), (728, '田森\n'), (729, '天使威光\n'), (730, '天使之护\n'), (731, '天使之翼\n'), (732, '天翔龙闪\n'), (733, '跳刀\n'), (734, '跳雷\n'), (735, '挑衅\n'), (736, '通背拳\n'), (737, '佟林\n'), (738, '头槌\n'), (739, '头上拂\n'), (740, '偷袭\n'), (741, '涂毒\n'), (742, '推云掌\n'), (743, '吞日\n'), (744, '吞噬术\n'), (745, '脱逃\n'), (746, '万剑归一\n'), (747, '王不留行\n'), (748, '王不留行治痛经不含糖\n'), (749, '王池轩\n'), (750, '忘川\n'), (751, '王大眼\n'), (752, '王杰希\n'), (753, '望山云雾\n'), (754, '网游\n'), (755, '王泽\n'), (756, '微草\n'), (757, '魏琛\n'), (758, '猥琐大师\n'), (759, '卫星射线\n'), (760, '稳定炮架\n'), (761, '文客北\n'), (762, '闻理\n'), (763, '温柔天使\n'), (764, '瘟阵\n'), (765, '窝心脚\n'), (766, '伍晨\n'), (767, '悟道君\n'), (768, '无敌最俊朗\n'), (769, '巫毒术\n'), (770, '无锋\n'), (771, '吴钩霜月\n'), (772, '无极\n'), (773, '无浪\n'), (774, '吴女士\n'), (775, '吴启\n'), (776, '无属性炫纹\n'), (777, '吴霜钩月\n'), (778, '吴雪峰\n'), (779, '吴羽策\n'), (780, '夕火\n'), (781, '膝击\n'), (782, '牺牲吼叫\n'), (783, '膝踢\n'), (784, '希望祷言\n'), (785, '膝袭\n'), (786, '吸血术\n'), (787, '喜之羊\n'), (788, '系舟\n'), (789, '膝撞\n'), (790, '夏仲天\n'), (791, '陷阱扣\n'), (792, '仙人指路\n'), (793, '向元纬\n'), (794, '小飞龙\n'), (795, '笑歌自若\n'), (796, '小回复术\n'), (797, '啸凌火\n'), (798, '小卢\n'), (799, '晓枪\n'), (800, '肖时钦\n'), (801, '小事情\n'), (802, '小手冰凉\n'), (803, '小唐\n'), (804, '小天使\n'), (805, '小叶子\n'), (806, '肖云\n'), (807, '小治愈术\n'), (808, '小周\n'), (809, '辛露\n'), (810, '星辰剑\n'), (811, '星落\n'), (812, '星落锤\n'), (813, '兴欣\n'), (814, '兴欣公会\n'), (815, '星星射线\n'), (816, '星星折射\n'), (817, '星云波动剑\n'), (818, '修鲁鲁\n'), (819, '修鲁鲁改良\n'), (820, '许斌\n'), (821, '徐汇柳\n'), (822, '徐景熙\n'), (823, '虚空\n'), (824, '续雷腰带\n'), (825, '蓄能火炮\n'), (826, '悬磁炮\n'), (827, '旋风\n'), (828, '旋风腿\n'), (829, '旋风斩\n'), (830, '旋流万丈\n'), (831, '玄奇\n'), (832, '旋投\n'), (833, '血疯\n'), (834, '血祭绝魂\n'), (835, '血气唤醒\n'), (836, '血气之剑\n'), (837, '雪纹\n'), (838, '血影狂刀\n'), (839, '巡游者\n'), (840, '亚军\n'), (841, '压力山大\n'), (842, '烟花式打法\n'), (843, '炎噬满月\n'), (844, '烟雾弹\n'), (845, '焰影\n'), (846, '烟玉\n'), (847, '烟雨\n'), (848, '烟雨苍苍\n'), (849, '烟雨楼\n'), (850, '烟雨锁楼\n'), (851, '炎阵\n'), (852, '杨聪\n'), (853, '杨昊轩\n'), (854, '羊习习\n'), (855, '妖刀\n'), (856, '要害攻击\n'), (857, '遥控式手雷\n'), (858, '夜八荒\n'), (859, '叶不羞\n'), (860, '夜度寒潭\n'), (861, '叶落乌啼\n'), (862, '叶秋\n'), (863, '夜未央\n'), (864, '夜汐\n'), (865, '叶下红\n'), (866, '叶修\n'), (867, '夜雨声烦\n'), (868, '一寸灰\n'), (869, '移动施法\n'), (870, '依诺\n'), (871, '一枪穿云\n'), (872, '以牙还牙\n'), (873, '一叶之秋\n'), (874, '义斩\n'), (875, '银光落刃\n'), (876, '印记\n'), (877, '银武\n'), (878, '银装\n'), (879, '影分身术\n'), (880, '迎风\n'), (881, '迎风布阵\n'), (882, '迎风一刀斩\n'), (883, '樱杀碎月\n'), (884, '硬身符\n'), (885, '鹰踏\n'), (886, '影舞\n'), (887, '英勇冲锋\n'), (888, '英勇飞跃\n'), (889, '英勇跳跃\n'), (890, '游峰电\n'), (891, '幽魂缠绕\n'), (892, '忧郁小猫猫\n'), (893, '于锋\n'), (894, '欲盖弥彰\n'), (895, '雨来了\n'), (896, '愈灵者\n'), (897, '羽美人\n'), (898, '于念\n'), (899, '于天\n'), (900, '喻文州\n'), (901, '鱼文粥\n'), (902, '袁柏青\n'), (903, '元素法师\n'), (904, '元素之力\n'), (905, '圆舞棍\n'), (906, '圆旋波动剑\n'), (907, '月光斩\n'), (908, '越云\n'), (909, '月中眠\n'), (910, '运动施法\n'), (911, '云归\n'), (912, '云山乱\n'), (913, '云身\n'), (914, '云体风身\n'), (915, '云霄\n'), (916, '再睡一夏\n'), (917, '葬花\n'), (918, '脏心杰\n'), (919, '战斗法师\n'), (920, '战斗格式\n'), (921, '斩锋\n'), (922, '斩楼兰\n'), (923, '战术大师\n'), (924, '沾衣乱飞\n'), (925, '张佳乐\n'), (926, '张家兴\n'), (927, '张简\n'), (928, '张奇\n'), (929, '张伟\n'), (930, '张新杰\n'), (931, '张益玮\n'), (932, '昭华\n'), (933, '召唤暗精灵\n'), (934, '召唤哥布林\n'), (935, '召唤火精灵\n'), (936, '召唤雷精灵\n'), (937, '召唤师\n'), (938, '召唤兽强化\n'), (939, '召唤雪精灵\n'), (940, '赵禹哲\n'), (941, '赵子杨\n'), (942, '折云靴\n'), (943, '郑乘风\n'), (944, '郑复升\n'), (945, '郑轩\n'), (946, '治疗术\n'), (947, '执天之行\n'), (948, '职业\n'), (949, '织影\n'), (950, '治愈术\n'), (951, '知月倾城\n'), (952, '中草堂\n'), (953, '中国\n'), (954, '重火力控制\n'), (955, '重击\n'), (956, '重力加速拍\n'), (957, '钟千离\n'), (958, '钟叶离\n'), (959, '周光义\n'), (960, '肘击\n'), (961, '肘落\n'), (962, '周烨柏\n'), (963, '周泽楷\n'), (964, '竹沥\n'), (965, '主席\n'), (966, '诛仙\n'), (967, '朱效平\n'), (968, '朱校平\n'), (969, '逐烟霞\n'), (970, '转火\n'), (971, '砖袭\n'), (972, '转职\n'), (973, '专注\n'), (974, '撞击式手雷\n'), (975, '追魂\n'), (976, '捉云手\n'), (977, '子不语\n'), (978, '子母刺\n'), (979, '紫武\n'), (980, '紫装\n'), (981, '自走火炮\n'), (982, '邹远\n'), (983, '邹云海\n'), (984, '诅咒之箭\n'), (985, '钻臂冲击\n'), (986, '最佳搭档\n'), (987, '最佳新人')]

```python
L3 = list(range(9,13,1))
```

```python
print(L1)
print(L2)
print(L3)
```

    [1, 2, 3, 4]
    [5, 6, 7, 8]
    [9, 10, 11, 12]

```python
list(zip(L1,L2,L3))
```

    [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

```python
S1 = 'abc'
S2 = 'xyz123'
zip(S1, S2)
```

    <zip at 0x19bc7e51088>

```python
list(_)
```

    [('a', 'x'), ('b', 'y'), ('c', 'z')]

```python
list(map(ord, S1))
```

    [97, 98, 99]

```python
res = []
for c in 'spam': res.append(ord(c))
res
```

    [115, 112, 97, 109]

# Dictionary construction with zip
>>> coding a dictionary literal

```python
D1 = {'spam':1 , 'eggs': 3, 'toast': 5}
D1
```

    {'spam': 1, 'eggs': 3, 'toast': 5}

>>> assigning to keys over time

```python
D1 = {}
D1['spam'] = 1
D1['eggs'] = 3
D1['toast'] = 5
```

```python
S = """The built-in name dict is really a type name in Python (you’ll learn more about type
names, and subclassing them, in Chapter 31). Calling it achieves something like a listto-dictionary conversion, but it’s really an object construction request. In the next
chapter we’ll explore a related but richer concept, the list comprehension, which builds
lists in a single expression; we’ll also revisit 3.0 dictionary comprehensions an alternative
to the dict cal for zipped key/value pairs."""
L = S.split()
```

```python
keys = S.split()
vals = list(range(1, len(keys)+1, 1))
```

```python
D2 = {}
for k, v in zip(keys, vals): D2[k] = v
```

```python
D3 = dict(zip(keys, vals))
```

```python
D3 == D2
```

    True

```python
[i**2 for i in range(10)] # List comprehension
```

    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

```python
{v: k for v, k in zip(keys, vals)} # dictionary comprehension
```

    {'The': 1,
     'built-in': 2,
     'name': 9,
     'dict': 70,
     'is': 5,
     'really': 34,
     'a': 57,
     'type': 16,
     'in': 56,
     'Python': 11,
     '(you’ll': 12,
     'learn': 13,
     'more': 14,
     'about': 15,
     'names,': 17,
     'and': 18,
     'subclassing': 19,
     'them,': 20,
     'Chapter': 22,
     '31).': 23,
     'Calling': 24,
     'it': 25,
     'achieves': 26,
     'something': 27,
     'like': 28,
     'listto-dictionary': 30,
     'conversion,': 31,
     'but': 47,
     'it’s': 33,
     'an': 66,
     'object': 36,
     'construction': 37,
     'request.': 38,
     'In': 39,
     'the': 69,
     'next': 41,
     'chapter': 42,
     'we’ll': 60,
     'explore': 44,
     'related': 46,
     'richer': 48,
     'concept,': 49,
     'list': 51,
     'comprehension,': 52,
     'which': 53,
     'builds': 54,
     'lists': 55,
     'single': 58,
     'expression;': 59,
     'also': 61,
     'revisit': 62,
     '3.0': 63,
     'dictionary': 64,
     'comprehensions': 65,
     'alternative': 67,
     'to': 68,
     'cal': 71,
     'for': 72,
     'zipped': 73,
     'key/value': 74,
     'pairs.': 75}

# Generating Both offsets and items: enumerate

```python
file = open('woshicik.txt',encoding='utf-8')
```

```python
S = 'spam'
offset = 0
for item in S:
    print(item, 'appears at offset', offset)
    offset +=1
```

    s appears at offset 0
    p appears at offset 1
    a appears at offset 2
    m appears at offset 3

```python
S = 'spam'
for offset, item in enumerate(S):
    print(item, 'appears at offset', offset)
```

    s appears at offset 0
    p appears at offset 1
    a appears at offset 2
    m appears at offset 3

```python
type(enumerate(S))
```

    enumerate

```python
E = enumerate(file)
```

```python
next(E)
```

    (4, '暗夜飞影\n')

```python
next(enumerate(file)) #为什么
```

    (0, '暗夜斗篷\n')

```python

```


```python
import sympy
from sympy import *
print(sympy.__version__)
init_printing(use_unicode=True)
```

    0.7.6.1

```python
from sympy import S, Eq, solve
m, mt, k, a, b = symbols('m mt k a b')

equations = [
...     Eq(vf, vi+a*t),
...     Eq(d, vi*t + a*t**2/2),
...     Eq(a, 10),
...     Eq(d, 60),
...     Eq(vi, 5)]

p = m**2/k**2
E = a*sinh(b*m)

pd = 2*m/k**2
Ed = a*b*cosh(b*m)
sols = solve([E, Ed], [p, pd])

print("p = {}".format((sols[].factor()))
```

```python
solve([Eq(a*sinh(b*1), 1**2/1**2)], [a, b])
```

```python
solve([Eq(a*b*cosh(b*1), 2*1/0.4**2)], [a,b])
```

```python
solve([Eq(a*sinh(b*0.3), 0.3**2/1**2), Eq(a*b*cosh(b*0.3), 2*0.3/1**2)], [a, b])
```

```python
solve(Eq(a*sinh(b*x)), x)
```

```python

```