9...using element


def list_function(x):
return [1]

n = [3, 5, 7]
print list_function(n)

wat wrong with this???????


What actually happend?
Because looking at your code, it seems to work, if you fix the indentation.

Edit:Found the problem, explaning on the next post.


still its nt working ..


Ugh, got it!
The problem is: in your function you're returing some element at position [1] but you're not telling where that element is.
You need return the value at position [1] of the list that was passed as a parameter.

Check the examples, at your left on section 9, to see how it's supposed to be.


still ny get:/ plse tell properly...:frowning:


Hi try it like that if its not workin try ro refresh ythe page

def list_function(x):
    return x[1]

n = [3, 5, 7]
print list_function(n)


def list_function(x):
    return x[1]

You were missing that.


garcias..:)..Thank you:)


why does

return x[1]

work, but

return n[1]

doesn't work?


HI you have a function name list_function that has as parameter x so you should use the parameter x so that when you call the function with the argument n it will return 5

print list_function(n)

Yes you could do that

n = [3, 5, 7]
def list_function():
	return n[1]

print list_function()

it will return the same thing but the lesson want you to use the other way


ahhh that makes sense! Thank you!


Hi @wizmarco, how do you know which brackets to use? Because I was trying it with () and it wouldn't work but when I tried with the square brackets [] it worked... :confused:


HI do you mean this line?(which should be)

return x[1]

why do I know that I should use [] or () because if you did the the lesson Access by Index
they taught how to access things by index..

#A list planet
planet = ["Earth", "Mars"; "Neptune"]
#if you want to access to Mars you will do it like that
print planet[1]

So that why it works with [] and not ()


Ah, that makes sense. Thanks! :thumbsup:


In Python we have these 3 types of brackets we use while coding ==>

  • Parentheses ( ) are used to define Tuples, Function , Loops, Order of operations
    eg:- def ( ):
    for item in range(...):

      * Different ways to define a Tuple
        a_tuple = ('amy', 'bar') 
        another_tuple = 'hello world',
        empty_tupe = ( )
        not_a_tuple = ('banana')
  • Curly braces { } are used to create Dictionaries or Sets
    eg:- dict = { 'apple' : 40, 'pear' : 30 }
    dict_2 = { }
    dict_3 = {'kiwi' : 120, 'cherry' : 720}

  • Squared brackets [ ] are used to define mutable data types(where the contents can be changed) like List
    eg:- list_animals = ['panda', 'lion', 'tiger']
    empty_list = [ ]
    list_of_dictionaries = [dict, dict_2, dict_3 ]



Thank you for that lovely summary. However, I do have one question:

What's a Tuple?


whitespace at return and retrun x at index 1.


Your code does not show where you added 3 to the index. How did you add 3?