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: en/python_introduction/README.md
+15-3Lines changed: 15 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -216,7 +216,7 @@ Easy, right? If you want to add something to your list, you can do this by typin
216
216
>>> print(lottery)
217
217
[59, 42, 30, 19, 12, 3, 199]
218
218
219
-
If you want to show only the first number, you can do this by using __indexes__. An index is the number that says where in a list an item occurs. Computer people like to start counting at 0, so the first object in your list is at index 0, the next one is at 1, and so on. Try this:
219
+
If you want to show only the first number, you can do this by using __indexes__. An index is the number that says where in a list an item occurs. Programmers prefer to start counting at 0, so the first object in your list is at index 0, the next one is at 1, and so on. Try this:
220
220
221
221
>>> print(lottery[0])
222
222
59
@@ -225,6 +225,18 @@ If you want to show only the first number, you can do this by using __indexes__.
225
225
226
226
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.
227
227
228
+
To delete something from your list you will need to use __indexes__ as we learnt above and the __del__ statement (del is an abbreviation for delete). Let's try an example and reinforce what we learnt previously; we will be deleting the first number of our list.
229
+
230
+
>>> print(lottery)
231
+
[59, 42, 30, 19, 12, 3, 199]
232
+
>>> print(lottery[0])
233
+
59
234
+
>>> del lottery[0]
235
+
>>> print(lottery)
236
+
[42, 30, 19, 12, 3, 199]
237
+
238
+
That worked like a charm!
239
+
228
240
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?
229
241
230
242
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
@@ -628,7 +640,7 @@ Congratulations! You just learned how to write functions! :)
628
640
629
641
This is the last part already. That was quick, right? :)
630
642
631
-
As we mentioned, programmers are lazy, they don't like to repeat themselves. Programming is all about automating things, so we don't want to greet every person by their name manually, right? That's where loops come in handy.
643
+
Programmers don't like to repeat themselves. Programming is all about automating things, so we don't want to greet every person by their name manually, right? That's where loops come in handy.
632
644
633
645
Still remember lists? Let's do a list of girls:
634
646
@@ -687,7 +699,7 @@ Which would print:
687
699
688
700
`range` is a function that creates a list of numbers following one after the other (these numbers are provided by you as parameters).
689
701
690
-
Note that the second of these two numbers is not included in the list that is output by Python (meaning `range(1, 6)` counts from 1 to 5, but does not include the number 6).
702
+
Note that the second of these two numbers is not included in the list that is output by Python (meaning `range(1, 6)` counts from 1 to 5, but does not include the number 6). That is because "range" is half-open, and with that we mean it includes the first value, but not the last.
0 commit comments