User Tools

Site Tools


clubs:python_club:python_club_lists_tuples_sets
Home | clubs :: cloud club :: python_club :: 3D-Printing | projects :: Proxmox | Kubernetes | scripting | utilities | games

About the Club

Python Club Topics - Lists, Tuples, & Sets

Lists

  1. Multiple values under 1 variable name
  2. Used to store collections of data
  3. Created using square brackets ( eg. [ ] )
  4. Values can be duplicated and order changed
  5. Create a list
    fruit = ["apples", "bananas", "oranges"]
    print(fruit)
  6. Use the len() function
    fruit = ["apples", "bananas", "oranges"]
    print(len(fruit))
  7. Counting items starts at 0. Get the 2nd item.
    fruit = ["apples", "bananas", "oranges"]
    print(fruit[1])
  8. Change a value of an item in the list
    fruit = ["apples", "bananas", "oranges"]
    print(fruit)
    fruit[2] = "cherries"
    print(fruit)

Tuples

  1. Multiple values under 1 variable name
  2. Used to store collections of data
  3. Values CANNOT be changed
  4. Created using parentheses ( eg. ( ) )
  5. Values can be duplicated and indexed, but the order CANNOT be changed
  6. Create a tuple
    fruit = ("apples", "bananas", "oranges")
    print(fruit)
  7. You cannot change values in a tuple
    fruit = ("apples", "bananas", "oranges")
    fruit[2] = "cherries"
    print(fruit)
  8. Can have a single value, but must include a comma at the end
    this_tuple = ("apples",)
    print(this_tuple)
  9. Use the len() function
    tuple = ("apples", "bananas", "oranges")
    print(len(tuple))

Sets

  1. Multiple values under 1 variable name
  2. Used to store collections of data
  3. Values are NOT ordered and NOT indexed
  4. Values CANNOT be duplicated - they will be ignored
  5. Created using curley braces ( eg. { } )
  6. Create a set
    fruit = {"apples", "bananas", "oranges", "apples"}
    print(fruit)
  7. Use the len() function
    fruit = {"apples", "bananas", "oranges"}
    print(len(fruit))
  8. You cannot change values in a set
    fruit = {"apples", "bananas", "oranges", "apples"}
    print(fruit)
    fruit[1] = "grapes"
    print(fruit)
clubs/python_club/python_club_lists_tuples_sets.txt · Last modified: by 127.0.0.1