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
Copy file name to clipboardExpand all lines: python_introduction/README.md
+31Lines changed: 31 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -148,6 +148,16 @@ Awesome, right? Of course, variables can be anything, so numbers too! Try this:
148
148
>>> a * b
149
149
24
150
150
151
+
But what if we used the wrong name? Can you guess what would happen? Let's try!
152
+
153
+
>>> name = "Maria"
154
+
>>> names
155
+
Traceback (most recent call last):
156
+
File "<stdin>", line 1, in <module>
157
+
NameError: name 'names' is not defined
158
+
159
+
An error! As you can see, Python has different types of errors and this one is called a **NameError**. Python will give you this error if you try to use a variable that hasn't been defined yet. If you encounter this error later, check your code to see if you've mistyped any names.
160
+
151
161
Play with this for a while and see what you can do!
152
162
153
163
@@ -216,6 +226,8 @@ If you want to show only the first number, you can do this by using __indexes__.
216
226
217
227
As you can see, you can access different objects in your list by using the list's name and the object's index inside of square brackets.
218
228
229
+
For extra fun, try some other indexes: 6, 7, 1000, -1, -6 or -1000. See if you can predict the result before trying the command. Do the results make sense?
230
+
219
231
You can find a list of all available list methods in this chapter of the Python documentation: https://docs.python.org/3/tutorial/datastructures.html
220
232
221
233
## Dictionaries
@@ -327,6 +339,16 @@ You can give Python as many numbers to compare as you want, and it will give you
327
339
-__and__ - if you use the `and` operator, both comparisons have to be True in order for the whole command to be True
328
340
-__or__ - if you use the `or` operator, only one of the comparisons has to be True in order for the whole command to be True
329
341
342
+
Have you heard of the expression "comparing apples to oranges"? Let's try the Python equivalent:
343
+
344
+
>>> 1 > 'django'
345
+
Traceback (most recent call last):
346
+
File "<stdin>", line 1, in <module>
347
+
TypeError: unorderable types: int() > str()
348
+
349
+
Here you see that just like in the expression, Python is not able to compare a number (`int`) and a string (`str`).
350
+
Instead, it shows a **TypeError** and tells us the two types can't be compared together.
351
+
330
352
## Boolean
331
353
332
354
Accidently, you just learned about a new type of object in Python. It's called a __Boolean__ -- and it probably is the easiest type there is.
@@ -471,6 +493,15 @@ As you can see, we now gave our function a parameter that we called `name`:
471
493
472
494
As you can see, we needed to put two spaces before the `print` function, because `if` needs to know what should happen when the condition is met. Let's see how it works now:
Oops, an error. Luckily, Python gives us a pretty useful error message.
502
+
It tells us that the function `hi()` (the one we defined) has one required argument (called `name`) and that we forgot to pass it when calling the function.
0 commit comments