Class 5 Lab 2 ‐ Returning Lists - Justin-Boyd/Python-Class GitHub Wiki

Step 1

  • Create a new Python file in PyCharm by right-clicking the project you created and selecting New > Python File.

Step 2

  • Declare a function that accepts two parameters.
def returned_list(first, second):

Step 3

  • Make the function return a list of the parameters.
return [first, second]

Step 4

  • Declare a second function that accepts two parameters.
def returned_dict(first, second):

Step 5

  • Make the function return a dictionary of the parameters.
return {first: second}

Step 6

  • Declare a third function that accepts two parameters.
def returned_tup(first, second):

Step 7

  • Make the function return a tuple of the parameters.
return {first: second}

Step 8

  • Declare a fourth function that accepts two parameters.
def returned_none(first, second):

Step 9

  • Make the function perform an addition operation on the parameters.
res = first + second

Step 10

  • Create the main function to handle the program.
def main():

Step 11

  • Create two variables that request inputs of values from a user
first_val = input("Please enter a value: ")
second_val = input("Please enter another value: ")

Step 12

  • Save the first function’s result to a variable.
list_res = returned_list(first_val, second_val)

Step 13

  • Save the second function’s result to a variable
dict_res = returned_list(first_val, second_val)

Step 14

  • Save the third function’s result to a variable.
tup_res = returned_list(first_val, second_val)

Step 15

  • Save the fourth function’s result to a variable.
none_res = returned_list(first_val, second_val)

Step 16

  • Print the results and the types saved in the variables
print("{} and its type is: {}".format(list_res, type(list_res)))
print("{} and its type is: {}".format(dict_res, type(dict_res)))
print("{} and its type is: {}".format(tup_res, type(tup_res)))
print("{} and its type is: {}".format(none_res, type(none_res)))

Step 17

  • Invoke the main function to run the program.
main()

Final Code

def returned_list(first, second):
    return [first, second]

def returned_dict(first, second):
    return {first: second}

def returned_tup(first, second):
    return first, second

def returned_none(first, second):
    res = first + second

def main():
    first_val = input("Please enter a value: ")
    second_val = input("Please enter another value: ")
    list_res = returned_list(first_val, second_val)
    dict_res = returned_dict(first_val, second_val)
    tup_res = returned_tup(first_val, second_val)
    none_res = returned_none(first_val, second_val)
    print("{} and its type is: {}".format(list_res, type(list_res)))
    print("{} and its type is: {}".format(dict_res, type(dict_res)))
    print("{} and its type is: {}".format(tup_res, type(tup_res)))
    print("{} and its type is: {}".format(none_res, type(none_res)))

main()

# prevents program from closing upon execution
input("\nPress 'Enter' to exit the program")