Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (2)

Powered by Vanilla. Made with Bootstrap.
Python indexing
  • Indexing in Python is very useful. Before I continue with the guide take a look at this source:

    alist = ['hello', 'iexploit', 'whats', 'up']
    print alist[1]
    sys.exit()

    What do you think this will do? Run it and find out. If you guessed it would print 'iexploit' then you were correct. Let's have a look at that source. First we create the list "alist". This holds the strings:
    'hello', 'iexploit', 'whats' and 'up'. Then we print alist[1]. So what's that little [1]? Thats the index number of 'iexploit'. When you create a list, everything in the list gets assigned an index number. It starts from left to right with 0. So hello is 0, iexploit is 1, whats is 2 and up is 3. So we print whatever is assigned the index value of 1(iexploit). This can be used in things such as command line arguments. Try this:

    alist = ['a', 'b', 'c']
    print alist[1:3]

    This is the output:
    ['b', 'c']

    This is because we used 1:3. This starts at one and ends at the value right before 3. So it prints out 1 and 2. What happens if we do this:

    alist = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
    print alist[1:10:2]

    This creates a list running numbers 1 through 10. Then we use a step. This is the third number followed by the second colon. This means it prints out every 2. So we get the following output:
    ['2', '4', '6', '8', '10']

    As you see it printed out all even numbers from 1 to 10.

    That's pretty much it for basic python indexing. Hope you learned a bit about it.

    --chroniccommand
  • Xin
    Posts: 3,251
    Nice guide chronic :)
    Xin
  • undead
    Posts: 822
    Nice guide chroniccommand!