Exercise 34: Accessing the elements of a list
The list is useful, but it only works when you have access to the content inside. You've learned to read the contents of a list sequentially, but what if you want to get the 5th element? You need to know how to access the elements in the list. The way to access the first element is this:
Animals = ['Bear ''tiger'penguin' 'zebra'= animals[0]
You define a list of animals, and then you use 0 to get the first element?! What the hell is going on here? Because this is the case in mathematics, the Python list starts with 0. Although it looks strange, this definition actually has its benefits, and actually designed to start with 0 or 1 actually,
The best way to explain this is to compare the way you normally use numbers to the way programmers use numbers.
Let's say you're watching a race in the list of four animals ([' Bear ', ' Tiger ', ' Penguin ', ' zebra '), And they match the nouns exactly as they are in the list. It was a very exciting game because the animals were not going to eat each other, and the game really held up. As a result, your friend is late, he wants to know who won the game, he will ask you "Hey, who is the No. 0 place"? No, he's going to ask, "Hey, who's 1th place?" ”
This is because the order of animals is very important. Without the first one there is no second, no second and no third. The 0th one does not exist, because the meaning of zero is nothing. "Nothing" How to win the game is completely illogical. Such numbers are called "ordinal number" because they represent the order of things.
Programmers cannot think in this way because they can take an element out of the list at any point. For programmers, the list above is more like a stack of cards. If they want tiger, grab it and crawl it if you want zebra. To randomly crawl the contents of a list, each element of the listing should have an address, or an "index", and the best way to do this is to use an index starting with 0. Believe what I'm saying, it's easier to get elements in this way. This type of number is called "cardinality", which means that you can grab the element arbitrarily, so we need a number No. 0 element Cardinal.
So, how does this knowledge help you with your list operations? Very simply, every time you say "I want a 3rd animal" to yourself, you need to convert the "ordinal" to "base", as long as the former minus 1 is OK. The index of the 3rd animal is 2, which is penguin. Since you've been dealing with ordinals all your life, you need to get the cardinality in this way, as long as you've lost 1.
Remember: ordinal = = ordered, starting at 1; cardinal = = Randomly selected, starting at 0.
Let's practice a bit. Define a list of animals and then do the following exercises, where you need to write the names of the animals that you are referring to. If I use "1st, 2nd" and so on, that means I use the ordinal, so you need to subtract 1. If I give you a base (0, 1, 2), you just use it directly ... Code-block:: Python
animals = [' bear ', ' python ', ' peacock ', ' kangaroo ', ' whale ', ' platypus ']
- The animal at 1.
- The 3rd animal.
- The 1st animal.
- The animal at 3.
- The 5th animal.
- The animal at 2.
- The 6th animal.
- The animal at 4.
For each of these, write a complete sentence in such a format: "The 1st animal is at 0 and was a bear." And then read upside down: "The animal at 0 are the 1st animal and is a bear."
Use Python to check your answers.
Bonus points Exercise
- Search the Internet for knowledge of the ordinal number (ordinal number) and cardinality (cardinal number) and read it.
- With your understanding of these different types of numbers, explain why "January 1, 2010" is 2010 instead of 2009? (Hint: You can't pick the year randomly.) )
- Write a list and index it the same way, confirming that you can translate between the two numbers.
- Use Python to check your answers.
Warning
A programmer will tell you to read a topic about numbers written by someone called "Dijkstra." I suggest that you do not read as well. Unless you like to listen to a person who stops programming when the programming line is just starting to scream.
Exercise Exercises
1.
Stupid way to learn Python (34)