Codecademy Python Answers Lists And Functions

Also Read: How To Chat With Your Friend Using Command Prompt

Lists And Functions 1/18  – List Accessing

n = [1, 3, 5]

# Add your code below
print n[1]

Lists And Functions 2/18  – List Element Modification

n = [1, 3, 5]
# Do your multiplication her
n[1] *= 5
print n

Lists And Functions 3/18  – Appending To a List

n = [1, 3, 5]
# Append the number 4 here
n.append(4)
print n

Lists And Functions 4/18  – Removing Elements from Lists

n = [1, 3, 5]
# Remove the first item in the list here
n.pop(0)
print n

Lists And Functions 5/18  – Changing The Functionality of a Function

number = 5

def my_function(x):
    return x * 3

print my_function(number)

Lists And Functions 6/18  – More than One Argument

m = 5
n = 13
# Add add_function here!
def add_function(x,y):
    return x+y


print add_function(m, n)

Lists And Functions 7/18  –  Strings in Functions

n = "Hello"
# Your function here!

def string_function(s):
    return s+"world"

print string_function(n)

Lists And Functions 8/18  – Passing a List to a function

def list_function(x):
    return x

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

Lists And Functions 9/18  – Using an Element From a List in a Function

def list_function(x):
     return x

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

Lists And Functions 10/18  – Modifying an Element of a List in a Function

def list_function(x):
     x[1] += 3
     return x

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

Lists And Functions 11/18  – List Manipulation in Functions

n = [3, 5, 7]
# Add your function here
def list_extender(lst):
    lst.append(9)
    return lst


print list_extender(n)

Lists And Functions 12/18  – Printing Out a List item by item in a Function

n = [3, 5, 7]
def print_list(x):
    for i in range(0, len(x)):
        print x[i]
print print_list(n)

Lists And Functions 13/18  – Modifying each element in a list in a function

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    results = []
    for numbers in lists:
        results += numbers
    return results
print flatten(n)

Lists And Functions 14/18  – Passing a range into Function

def my_function(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
    return x

print my_function(range(0,3)) # Add your range between the parentheses!

Lists And Functions 15/18  – Iterating Over a List in a Function

n = [3, 5, 7]

def total(numbers):
    result = 0
    for i in range(len(numbers)):
        result = result + numbers[i]
    return result

print total(n)

Lists And Functions 16/18  – Using Strings in lists in function

n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
    result = ""
    for i in words:
       result += i
    return result


print join_strings(n)

Lists And Functions 17/18  – Using Two Lists as Two Arguments

m = [1, 2, 3]
n = [4, 5, 6]

# Add your code here!

def join_lists(x,y):
    return x + y



print join_lists(m, n)
# You want this to print [1, 2, 3, 4, 5, 6]

Lists And Functions 18/18  – Using a Lists of Lists in a Function

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    results = []
    for numbers in lists:
        results += numbers
    return results
print flatten(n)
Latest Comments
  1. Kendall April 1, 2017
  2. spencer April 3, 2017
  3. john wick May 10, 2017
    • prathap May 10, 2017
  4. Garrett Harms May 27, 2017
    • prathap May 27, 2017
      • Garrett Harms May 27, 2017
  5. RayCodeGrind June 27, 2017
    • prathap June 27, 2017

Leave a Reply

Your email address will not be published. Required fields are marked *