Python Programming 6
The 6th chapter already? I'm pretty exhausted. And hungry. Ok, more so hungry. I need some food. But I have nothing to eat. Let's go to the store. But oh wait, we should make a list of things we need.
Get it. Like a Python list. Which we're about to learn. If you didn't find this funny you can go take health or something.
So what is a list? So far, out variables can hold whole numbers, complex numbers, letters, strings, characters, true/false variables, etc. A list is a variable that can hold many different values. so:
car = "Honda"
The variable car is holding the value "Honda". But what if I'm looking to store several values and I don't want to make several variables. Let's face it, if you had a variable for every time you stored data, there would literally be a variable for every text you've ever sent. Every email you've ever delivered. That's a lot of messy variables. Instead you can do this:
car = ["Honda", "Toyota", "Ford", "Hyundai", "Nissan"]
Notice how this is being saved. I initialize the variable car and then set it equal to []'s (brackets) with several different strings between them. This format can also be considered comma separated variables or CSV. Now we can store more things in one variable which saves space and creates organization. How do we access them though? How would we go about printing out "Ford" without worrying about the other stuff? It looks like this:
print(car[2])
Pretty easy actually. We basically just tell the computer which number in the list we want printed out. But hold on.... Why not 3? After all, "Ford" is the 3rd string in the list of 5. The reason is because all lists start with an index of 0. So:
print(car[0])
Honda
print(car[1])
Toyota
print(car[2])
Ford
print(car[3])
Hyundai
print(car[4])
Nissan
You can also reassign properties inside the list. Just the same as you can set a variable equal to something else, you can say:
car = ["Honda", "Toyota", "Ford", "Hyundai", "Nissan"]
car[3] = "Ferrari"
The list will now have "Ferrari where "Hyundai" once was. If we wanted, we could have left out the [3] and the console would have printed the entire list with the []'s. Try this on your own. Lists do not have to remain one type. They can have multiple kinds of data.
stuff = ["cars", 64, "Mount Carmel", True, 9.293]
We can put strings, integers, more strings, booleans, and float numbers in. There are different kinds of list operations we can perform. Deleting specific values:
stuff = ["cars", 64, "Mount Carmel", True, 9.293]
del stuff[0]
The list no longer has the value "cars". We can concat (combine) lists by adding them:
car = ["Honda", "Toyota", "Ford", "Hyundai", "Nissan"]
del car[3]
man = ["Tom"]
print car + man
The output will be ['Honda', 'Toyota', 'Ford', 'Nissan', 'Tom']. By adding the single list item in man to car, it appends the second list to the end. Say the list is really big (we'll use five here but imagine bigger) and you wanted to figure out how many items were in the list. You could do this:
theList = ['Honda', 'Toyota', 'Ford', 'Nissan', 'Tom']
print len(theList)
The len function returns the number of items in the list. This example would print the number 5. You can check to see if an item is in a list. Maybe you have a list of cities you've been to (imagine its too long to search through) and you want to figure out if a particular city is in the list. You can do this:
cities = ["Chicago", "Boston", "Dublin", "New York", "Berlin", "Orlando"]
print ("Orlando" in cities)
print ("Tokyo" in cities)
So we made a list of cities and then we are printing out two lines. The first is the answer to the condition as to whether or not "Orlando" is in our list of cities. Because it is, the result is True. So True will be printed on the line. Because Tokyo is not in our list, below the True, False will be printed.
What if we wanted to print out each individual item in a list instead of the list as a whole. Imagine you are writing facebook's chat code. Every single message you and a friend send are stored in an array, aka list, of messages between the two of you. Which are printed to the screen when you open the chat. Let's see how we can do that with Python.
messages = ["Hello there friend", "How are things going", "I hope they're going well", "Would you like to overthrow the bourgeois with me?", "I mean get ice cream.", "Want to get ice cream?"]
for message in messages:
____print(message)
Try this on your own in PyCharm. What we are doing is taking our list of messages and looping through each one in the array and printing them out one by one. We created the temporary variable message to be set equal to each individual message in messages. Then we print it out. This is understandably confusing.
You can also make simple additions to lists without having to redefine them. Let's use the same messages list from the previous example and pretend we want to add on another message as though we just finished typing and submitting one.
messages.append("Let's get ice cream")
This new message is appended to the end of the list.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Video tutorials Video 1 Video 2
Write a program that acts as a todo list. The user can type in an item describing what chore or task they need to do. It displays all tasks on the screen. When the user is done with the task, they can remove it from the list.