Created
September 17, 2014 17:04
-
-
Save stenof/a97b741f33488dfdbfa6 to your computer and use it in GitHub Desktop.
Learning python: Using strings in lists in functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Define a function called join_strings accepts an argument called words. It will be a list. | |
| # Inside the function, create a variable called result and set it to "", an empty string. | |
| # Iterate through the words list and append each word to result. | |
| # Finally, return the result. | |
| # Don't add spaces between the joined strings! | |
| n = ["Michael", "Lieberman"] | |
| def join_strings(words): | |
| result = "" | |
| for i in range(0, len(words)): | |
| result = result + words[i] | |
| return result | |
| print join_strings(n) | |
| ## Create a function that joins two lists together. | |
| ## Define a function called join_lists that has two arguments, x and y. They will both be lists. | |
| ## Inside that function, return the result of concatenating x and y together. | |
| m = [1, 2, 3] | |
| n = [4, 5, 6] | |
| def join_lists(x,y): | |
| return x + y | |
| print join_lists(m, n) | |
| # You want this to print [1, 2, 3, 4, 5, 6] | |
| n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]] | |
| def flatten(lists): | |
| results = [] | |
| for numbers in lists: | |
| for i in numbers: | |
| results.append(i) | |
| return results | |
| print flatten(n) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment