Funtion return, list and dictionary

Study notes about how to return a function, how to get elements from list or dictionary.

Return

Only when I want to quit my functions, multiple returns is acceptable.

def get_class_average(students):
    results = []
    for each in students:
        result=get_average(each)
        results.append(result)
    return average(results)

Get elements from list

  • list-name[Serial number] remember serial numbers start from 0
a = [1,2,3]
print a[2]
output: 3
  • the tpye of list-name[1] is the element it self.
a = [1,"h",3]
print type(a[2])
output: <type 'int'>
  • the type of list-name [1:4] is a list
a = [1,"h",3]
print type(a[2:3])
output: <type 'list'>

Get element from dictionary

  • dictionary-name[key]
a = {"Jing":"girl", "Zhe":"boy"}
print a["Zhe"]
output: boy

links of Data Structures in the The Python Tutorial https://docs.python.org/3/tutorial/datastructures.html

April 1, 2017