How to take indices and name of a variable in Sage? I define the following in Sage.
rank=8
R=PolynomialRing(QQ, 'a' ,rank+1)
a=R.gens()

I would like to define a function f which returns the index of a[3]: f(a[3])=3 and define another function g such that g(a[3])=a. How to do this in Sage? Thank you very much!

This is something generic about lists in python so
a.index(a[3])

for the first (you can write f = a.index if you like), and for the second if you really want a function rather than using [3] you can do
def g(n):
    return a[n]

