Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Python Data Types Specific Index value

Help! How do you do this?

I don't understand...

Noam Kantor
Noam Kantor
3,742 Points

I'll try to simplify it for you, hope you get it :) Try to make a variable named 'var_one' 'var_one' needs to hold some data (a list or a string) And the letter 't' at index 5 For example -> the word sunset v S U N S E T - string 0 1 2 3 4 5 - index ^

1 Answer

I just answered on your question a while back. Perhaps I wasn't clear enough, but let me start from beginning.

Lets understand something called Arrays.

Arrays are nothing but a continuos collection of similar type of values.

Example:

variable1= ['A', 'B', 'C', 'd', 'e']

Variable1 is collection of letters, and how do you use this Array and why would you use it.

Well, to answer that question, i'll tell you to think of a situation where you have to store a collection of similar value but use them independently as well, storing in many different variable doesn't make sense. Now, for how to use it... simply variable1[0] will give you 'A'.(indexing starts from zero.) similarly variable1[1]='B'... and so on.

Now there could be a collection of any type of value, string, integer, float, arrays or arraysOfArrays... you name it.

In python everything is an Object.

Lists are nothing but arrays in Python. A string can also be treated like a list in python.

But what is different in Python is its Reverse-Index or Backward Index. The front index starts from 0(zero) as seen in above example, but if you need to access the last value???

Simple start indexing from -1, so...

#variable1[-1] is nothing but  'e'
#Similarly, variable1[-2] is 'd'
# and so on...

Hope this helps. If you find it helpful, upvote it for others and if not then please do comment you doubts below.

Have fun coding.