|
| 1 | +{% set warning_icon = '<span class="glyphicon glyphicon-exclamation-sign" style="color: red;" aria-hidden="true" data-toggle="tooltip" title="An error is expected when you run this command!" ></span>' %} |
| 2 | + |
1 | 3 | # Introduction to Python |
2 | 4 |
|
3 | 5 | > Part of this chapter is based on tutorials by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots). |
@@ -134,15 +136,17 @@ These are the basics of every programming language you learn. Ready for somethin |
134 | 136 |
|
135 | 137 | Let's try something new. Can we get the length of a number the same way we could find out the length of our name? Type in `len(304023)` and hit `enter`: |
136 | 138 |
|
137 | | -{% filename %}command-line{% endfilename %} |
| 139 | +{% filename %}{{ warning_icon }} command-line{% endfilename %} |
138 | 140 | ```python |
139 | 141 | >>> len(304023) |
140 | 142 | Traceback (most recent call last): |
141 | 143 | File "<stdin>", line 1, in <module> |
142 | 144 | TypeError: object of type 'int' has no len() |
143 | 145 | ``` |
144 | 146 |
|
145 | | -We got our first error! It says that objects of type "int" (integers, whole numbers) have no length. So what can we do now? Maybe we can write our number as a string? Strings have a length, right? |
| 147 | +We got our first error! The {{ warning_icon }} icon is our way of giving you a heads up that the code you are about to run won't work as expected. Making mistakes (even intentional ones) are an important part of learning! |
| 148 | + |
| 149 | + It says that objects of type "int" (integers, whole numbers) have no length. So what can we do now? Maybe we can write our number as a string? Strings have a length, right? |
146 | 150 |
|
147 | 151 | {% filename %}command-line{% endfilename %} |
148 | 152 | ```python |
@@ -207,7 +211,7 @@ Awesome, right? Of course, variables can be anything – numbers too! Try this: |
207 | 211 |
|
208 | 212 | But what if we used the wrong name? Can you guess what would happen? Let's try! |
209 | 213 |
|
210 | | -{% filename %}command-line{% endfilename %} |
| 214 | +{% filename %}{{ warning_icon }} command-line{% endfilename %} |
211 | 215 | ```python |
212 | 216 | >>> city = "Tokyo" |
213 | 217 | >>> ctiy |
@@ -372,7 +376,7 @@ See, it's similar to a list. But you don't need to remember the index – just t |
372 | 376 |
|
373 | 377 | What happens if we ask Python the value of a key that doesn't exist? Can you guess? Let's try it and see! |
374 | 378 |
|
375 | | -{% filename %}command-line{% endfilename %} |
| 379 | +{% filename %}{{ warning_icon }} command-line{% endfilename %} |
376 | 380 | ```python |
377 | 381 | >>> participant['age'] |
378 | 382 | Traceback (most recent call last): |
@@ -498,7 +502,7 @@ You can give Python as many numbers to compare as you want, and it will give you |
498 | 502 |
|
499 | 503 | Have you heard of the expression "comparing apples to oranges"? Let's try the Python equivalent: |
500 | 504 |
|
501 | | -{% filename %}command-line{% endfilename %} |
| 505 | +{% filename %}{{ warning_icon }} command-line{% endfilename %} |
502 | 506 | ```python |
503 | 507 | >>> 1 > 'django' |
504 | 508 | Traceback (most recent call last): |
@@ -663,7 +667,7 @@ if 3 > 2: |
663 | 667 |
|
664 | 668 | If we were to save and run this, we'd see an error like this: |
665 | 669 |
|
666 | | -{% filename %}command-line{% endfilename %} |
| 670 | +{% filename %}{{ warning_icon }} command-line{% endfilename %} |
667 | 671 | ``` |
668 | 672 | $ python3 python_intro.py |
669 | 673 | File "python_intro.py", line 2 |
|
849 | 853 |
|
850 | 854 | Remember: The `print` function is indented four spaces within the `if` statement. This is because the function runs when the condition is met. Let's see how it works now: |
851 | 855 |
|
852 | | -{% filename %}command-line{% endfilename %} |
| 856 | +{% filename %}{{ warning_icon }} command-line{% endfilename %} |
853 | 857 | ``` |
854 | 858 | $ python3 python_intro.py |
855 | 859 | Traceback (most recent call last): |
|
0 commit comments