Python 2.7 Tutorial Pt 2

Python Picture

I continue with my Python 2.7 tutorial here. If you missed part one (How Dare You) you must watch it first at Python 2.7 Tutorial. Otherwise you’ll be confused. I specifically explain how to use Python Lists, Tuples and Dictionaries. These work very similar to arrays in other languages.

An array is a variable that contains multiple values. Think of an array as a box that contains many other boxes, that all contain values. These values can be a mixture of different data types (ints, floats, strings). Here is how Python Lists, Tuples, and Dictionaries differ:

  • List: Holds multiple values, that can be changed and manipulated with many Python methods.
  • Tuple: Like a list that won’t allow you to change the values
  • Dictionary: A list in which there is a key associated with every value stored in it. These values also can’t be changed like tuples. (The video will explain this)

Here is all of the code for the upcoming Python 2.7 Tutorial. If you have any questions leave them below. (I’ll answer them)

Code from the Video

Here I will list all of the code. I will include the output in a box on screen, where appropriate.

#! /usr/bin/python

tupleEx = (‘Derek’, 35, ‘Pittsburgh’, ‘PA’) # Creating a Python Tuple

for i in tupleEx: # Cycling through all the values in the Tuple and printing them to screen


print(“First element in the tuple is “, tupleEx[0])

(‘First element in the tuple is ‘, ‘Derek’)

smTuple = (66,)
print smTuple


tupleFunc = tuple(‘abcde’)
print tupleFunc

(‘a’, ‘b’, ‘c’, ‘d’, ‘e’)

# That’s it tuples don’t have methods

listEx = [‘Derek’, 35, ‘Pittsburgh’, ‘PA’]

for i in listEx:


# Slicing Up Lists

print listEx[0:2] # Prints out the values starting at index 0 to but not including the value at index 2
print listEx[-1] # Prints the last value in the List
listNum = [1,2,3,4,5,6,7,8,9,10]
print listNum[-3:] # Prints the last 3 values in the List
print listNum[:3] # Prints the first 3 values in the List

[‘Derek’, 35]
[8, 9, 10]
[1, 2, 3]

# Prints out the values starting at the first index to but not up to index 10 while skipping every other value
print listNum[1:10:2]
print len(listNum) # Prints the number of values in the List
print min(listNum) # Prints the smallest value in the List
print max(listNum) # Prints the largest value in the List

[2, 4, 6, 8, 10]

listName = list(‘Fred’) # Creates a list with the letters F r e and d in it
print listName
listName[4:] = list(‘dy’) # Adds the letters d and y to the list
print listName

[‘F’, ‘r’, ‘e’, ‘d’]
[‘F’, ‘r’, ‘e’, ‘d’, ‘d’, ‘y’]

listEx3 =  [1,2,3,4]
listEx3[1] = 5 # Assigns the value 5 to the second index in the list
print listEx3
del listEx3[1] # Deletes the second value from the list
print listEx3

[1, 5, 3, 4]
[1, 3, 4]

# List Methods

listEx.append(“Joy”) # Adds the string Joy to the end of the List


[‘Derek’, 35, ‘Pittsburgh’, ‘PA’, ‘Joy’]

listEx.remove(“Joy”) # Removes the first value that is equal to “Joy” from the List


[‘Derek’, 35, ‘Pittsburgh’, ‘PA’]

listEx.remove(listEx[3]) # Removes the 4th indexed value in the List


[‘Derek’, 35, ‘Pittsburgh’]

listEx.insert(2, ‘PA’) # Inserts ‘PA’ into the List at the second index position


[‘Derek’, 35, ‘PA’, ‘Pittsburgh’]

listEx2 = [‘f’, ‘e’, ‘c’, ‘d’, ‘a’, ‘b’]

listEx2.sort() # Sorts List alphabetically


listEx2.reverse() # Sorts list in reverse alphabetical order


[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
[‘f’, ‘e’, ‘d’, ‘c’, ‘b’, ‘a’]

# Create a multi-dimension List

listEx3 = [



# Dictionary Example Code

dictEx = ({“Age”:35, “Height”:”6’3″, “Weight”:169}) # Creates a Python Dictionary


{‘Age’: 35, ‘Weight’: 169, ‘Height’: “6’3”}

print(dictEx.get(“Height”)) # Prints the value stored with the key ‘Height’

print(dictEx.items()) # Prints out all keys and values in the Dictionary

print(dictEx.values()) # Prints out just the values in the Dictionary

dictEx.pop(“Height”) # Deletes the key and value, where the key equals ‘Height’


[(‘Age’, 35), (‘Weight’, 169), (‘Height’, “6’3”)]
[35, 169, “6’3”]
{‘Age’: 35, ‘Weight’: 169}

5 Responses to “Python 2.7 Tutorial Pt 2”

  1. Gregor says:

    Be careful everyone:
    listEx.remove(listEx[3]) # Removes FIRST occurrence of value listEx[3]

    >>> q = [‘a’, ‘b’, ‘a’]
    >>> q = [‘a’, ‘b’, ‘a’]
    >>> q.remove(q[2])
    >>> print q
    [‘b’, ‘a’]

    >>> q = [‘a’, ‘b’, ‘a’]
    >>> del(q[2])
    >>> print q
    [‘a’, ‘b’]

  2. pjordan says:

    hi–i am not sure what command you are using in eclipse to print? are you evalualating just the latest print line–or the entire file. thanks

    • admin says:

      I’m sorry but I don’t understand the question

    • pjordan says:

      hi–thanks for responding. at 2:24–you are executing a print command. My question is 1)what are you doing (keyboard shortcut?) to execute this command; and 2) are you exucuting just the last command or
      both print commands. It almost seems like you are working in a ‘idle’ interactive fashion. thanks

      • admin says:

        Oh, I have execute or run assigned to my F5 button. So, I’m typing in code and then hitting F5. That’s why it looks like it is executing instantly 🙂

Leave a Reply to pjordan Cancel reply

Your email address will not be published.