clubs:python_club:python_club_lists_tuples_sets
Home | clubs :: cloud club :: python_club :: 3D-Printing | projects :: Proxmox | Kubernetes | scripting | utilities | games
Table of Contents
Python Club Topics - Lists, Tuples, & Sets
Lists
- Multiple values under 1 variable name
- Used to store collections of data
- Created using square brackets ( eg. [ ] )
- Values can be duplicated and order changed
- Create a list
fruit = ["apples", "bananas", "oranges"] print(fruit)
- Use the len() function
fruit = ["apples", "bananas", "oranges"] print(len(fruit))
- Counting items starts at 0. Get the 2nd item.
fruit = ["apples", "bananas", "oranges"] print(fruit[1])
- Change a value of an item in the list
fruit = ["apples", "bananas", "oranges"] print(fruit) fruit[2] = "cherries" print(fruit)
Tuples
- Multiple values under 1 variable name
- Used to store collections of data
- Values CANNOT be changed
- Created using parentheses ( eg. ( ) )
- Values can be duplicated and indexed, but the order CANNOT be changed
- Create a tuple
fruit = ("apples", "bananas", "oranges") print(fruit)
- You cannot change values in a tuple
fruit = ("apples", "bananas", "oranges") fruit[2] = "cherries" print(fruit)
- Can have a single value, but must include a comma at the end
this_tuple = ("apples",) print(this_tuple)
- Use the len() function
tuple = ("apples", "bananas", "oranges") print(len(tuple))
Sets
- Multiple values under 1 variable name
- Used to store collections of data
- Values are NOT ordered and NOT indexed
- Values CANNOT be duplicated - they will be ignored
- Created using curley braces ( eg. { } )
- Create a set
fruit = {"apples", "bananas", "oranges", "apples"} print(fruit)
- Use the len() function
fruit = {"apples", "bananas", "oranges"} print(len(fruit))
- 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
