tags: Lesson Notes Python Fundamentals PythonFlask2021

Python basics

  • So last chapter I talked about setting things up and how to print strings. But as I am sure you have already guessed that is not all we will be doing. So on to the next.

List:

  • A list you say? What this is you ask? Well here is what they look like
aList = ['Item 1', 'Item 2', 'Item 3']
emptyList = []
numberList = [1,4,5,6]

I bet your looking at me and saying hey wait thats just an array from JavaScript. Well your not totally wrong.

  • We can add, remove, print all sorts of things to this list.
  • So lets print some things from these lists
aList = ['Item 1', 'Item 2', 'Item 3']
emptyList = []
numberList = [1,4,5,6]

print(aList)
print(numberList)
print(emptyList)

# Output:
['Item 1', 'Item 2', 'Item 3']
[1, 4, 5, 6]
[]
  • Ok not to bad. Lets add stuff to that empty list of ours.
emptyList = []

print(emptyList)

emptyList.append(aList)
print(emptyList)

emptyList.append(numberList)
print(emptyList)

# Output:
[]
[['Item 1', 'Item 2', 'Item 3']]
[['Item 1', 'Item 2', 'Item 3'], [1, 4, 5, 6]]
  • Ok that wasn't to hard either. Guess what pop works to take off the last item. I will add it to the final code linked through pythontutor at the end

Tuples:

  • So these are flowers right? Pop up every spring? Nope. It is another type of data set.
  • However this one is immutable - As in can't be changed
  • They also use () not []
  • So lets take a look
myTuple = ('coding', 'fun', 'foo', 'bar')
numTuple = (1,4,6,8)
  • We print these just the same way. A link to some code is at the bottom as well.

Dictionaries:

  • Now this one this is the one that gets a lot of folks. It's JavaScript equivalent is an Object. But guess what? I have a better way to look at this.
  • This of these as well as a dictionary. A word and its definition. Before I go further lets show you what these look like
myDict = {"name": "Melissa", "job": "Instructor", "numKids": 4}
  • Ok so what we have is a key value pair. Name is the key (or the word we want to look up) and Melissa is the value (or the definition of the word)
  • Ok so how would we look these up? The same as lists and tuples? Not exactly. Let me show you
myDict = {"name": "Melissa", "job": "Instructor", "numKids": 4}

print(myDict['name'])

# Output:
Melissa
  • So we need to say ok look in the dictionary called myDict and please look up the value for name and print that back.
  • What about adding information? Can we do that? Yup!
myDict['kidsNames'] = ["Shannon", "Nathan", "Aiden", "Hayden"]
print(myDict)

myDict['location'] = 'PA'
print(myDict)
print(myDict['kidsNames'])

# Output:
{'name': 'Melissa', 'job': 'Instructor', 'numKids': 4, 'kidsNames': ['Shannon', 'Nathan', 'Aiden', 'Hayden']}
{'name': 'Melissa', 'job': 'Instructor', 'numKids': 4, 'kidsNames': ['Shannon', 'Nathan', 'Aiden', 'Hayden'], 'location': 'PA'}
['Shannon', 'Nathan', 'Aiden', 'Hayden']
  • Check the link below to see the full code

Tips and Tricks for printing:

First let me just say that this works for JavaScript too and I highly recommend it, whenever you are printing or console.logging

aDict = {}
aList = []
aTuple = ('Foo', 'Bar')

print("Printing aDict line1: ", aDict)
print("Printing aList line2: ", aList)
print("Printing aTuple line2: ", aTuple)

aList.append("List Item")
aList.append("Another Item")
print("Added to the aList lines9-10: ", aList)

aDict['foo'] = 'Bar'
aDict['coding'] = "Is Fun"
print("Added to aDict lines 13-14: ", aDict)
  • So what I've done here is leave myself and other developers a message. Not only what the prints statement is supposed to be printing but also where that information is coming from.
  • Why would we want to do that? Thats easy. When you get a bug in your code (and we all know you will at some point) this will help a lot in debugging.
  • How? Well all the print statements that do print will tell you that the code above them worked. So the next print statement along with the error it's self can point you to where it is. You can always comment out these print statements at a later time.

So for now thats all I got Links are below for all the code I used.

Code Links