Skip to content

Instantly share code, notes, and snippets.

@jamesdehart
Created August 28, 2017 17:45
Show Gist options
  • Save jamesdehart/c6f48c8ea7cdf1e7d61513cf3cc5332f to your computer and use it in GitHub Desktop.
Save jamesdehart/c6f48c8ea7cdf1e7d61513cf3cc5332f to your computer and use it in GitHub Desktop.
python data types

Data Types

List

List: is ordered in the order its placed in. It's values can be changed.

grades = [77, 80, 90, 95, 100]

Tuple

Tuple: is a list that can't be changed.

tuple_grades = (77, 80, 90, 95, 100)

Set

Sets: is a unique & unordered list. 2nd 100 will get removed.

set_grades = {77, 80, 90, 100, 100}
print(set_grades)

# output
# {80, 90, 100, 77}

Dictionaries

Dictionaries: is a key value pair.

my_dict = {'name': 'Jose', 'age': 90}

Accessing values

my_dict = {'name': 'Jose', 'age': 90}
print(my_dict['name'])

# Output
# Jose

Complex Data Structures

List of dictionaries

universities = [ # List
    { # Dictionary
        'name': 'Oxford',
        'location': 'UK'
    },
    { # Dictionary
        'name': 'MIT',
        'location': 'USA'
    }

]

Dictionary with a Tuple

lottery_player = { # Dictionary
    'name': 'Rolf',
    'numbers': (12, 45, 66, 52, 22) # Tuple
}

Dictionary with a List

some_dict = { # Dictionary
    'name': 'Rolf',
    'age': 90,
    'grades': [13, 50, 30, 12, 88]
}

Sum operations

lottery_player = { # Dictionary
    'name': 'Rolf',
    'numbers': (12, 45, 66, 52, 22) # Tuple
}
sum(lottery_player['numbers'])

# Output
# 197   # Sum of the

Dictionary in a Dictionary

another_dict_in_dict = {
    'key': {
        'name': 'Jose'
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment