Lists
Lists are like Tuples that can be modified. Some of the ways to use Lists are exactly the same as Tuples.
A list is assigned a name just like a variable and has a similar format but it is flanked by square brackets [ ].
Type in the following program (line 19 was a blank line)
**NOTE: if you see raw_input, please replace it with just input. raw_input is from Python 2.8.
Note on "if in"
Lets look at the statement:
if "milk" in fridge:
This means ... if the item "milk" is in the list "fridge" then do everything after the colon.
There is also the opposite of this:
if "milk" not in fridge:
This means exactly the opposite and just as it sounds. If "milk" is NOT in the list "fridge" then do everything after the colon!
Note on Indexing
fridge[2] will output the 3rd value (remember it starts at 0) in your list.
You can also replace specific spots in the list with indexing.
fridge[2] = "bananas" will replace spot 3 in the list fridge with the string "bananas".
Note on List Items
List items are in quotes " " but they can also be variables! So instead of item "bread", if you had a = "bread", you can have the list: fridge = [a]
From this program you should know the following:
Note on cloning lists.
To clone a list, use line 2 below where "clone" and "original" are the names of the lists you are using (they can be anything you want)
Making a list equal to another doesn't actually clone the list. It only assigns another pointer to the values.
So if I have:
a = [1,2]
b = a
If a changes, it also changes variable b!
That's why you should use x = list(y) or some variation of that to clone a list properly.
Note on adding strings to a list
Normally an input takes in a string, but to add it properly to a list you must add str ( ) aroudn the input so the list does not break it into individual letters.
So if I want to add a name to a list I have to use an input like this:
name = str(input("What's your name: " ))
Then I can do this to add the name to a list (note the brackets around the variable name to make a list added to a list!)
namelist += [name]
Assignment 13
Create a program that asks the user to enter 5 values and stores them in a list
The program will randomly shuffle the order of those 5 values after you press enter and show you the new list.
It will look exactly like the following example.
Bonus: Do this in under 21 lines including your name in line 1 as a comment without using break.
Hint: If you're having trouble with this algorithm, write down the steps you would use to do this in real life if you had a sheet of paper and pencil in front of you. Pretend someone gives you a list of 5 things. How would you reorder them? What are the steps? Once you have the steps, convert them into code. This "English" version of your program is called pseudocode and it is an important part of programming to help solve problems. The order of your steps is very important as it tells you the logic your program will need to go through to solve things. Think of it as a recipe of instructions that you follow to solve this problem.
YOU MAY NOT USE LIST METHODS TO SOLVE THIS