7. Syntax similarities

7.1. dict and set

  • Both set and dict keys must be hashable

  • Both set and dict uses the same { and } braces

  • Despite similar syntax, they are different types

{}                # dict
{1}               # set

{1, 2}            # set
{1: 2}            # dict

{1, 2,}           # set
{1: 2,}           # dict

{1: 2, 3: 4}      # dict
{1, 2, 3, 4}      # set

7.1.1. Empty dict

my_data = {1: 1}
print(my_data)
# {1:1}

my_data.pop(1)
print(my_data)
# {}

7.1.2. Empty set

my_data = {1}
print(my_data)
# {1}

my_data.pop()
print(my_data)
# set()

7.1.3. Differences

my_data = {}
isinstance(my_data, dict)         # True
isinstance(my_data, set)          # False

my_data = {1}
isinstance(my_data, dict)         # False
isinstance(my_data, set)          # True

my_data = {1: 1}
isinstance(my_data, dict)         # True
isinstance(my_data, set)          # False

7.2. tuple vs. str

what = 'foo'      # str
what = 'foo',     # tuple with str
what = 'foo'.     # SyntaxError: invalid syntax
what = ('foo')    # str
what = ('foo',)   # tuple with str
what = ('foo'.)   # SyntaxError: invalid syntax

7.2.1. tuple vs. float and int

what = 1.2        # float
what = 1,2        # tuple with two int

what = (1.2)      # float
what = (1,2)      # tuple with two int
what = 1.2,       # tuple with float
what = 1,2.3      # tuple with int and float

what = (1.2,)     # tuple with float
what = (1,2.3)    # tuple with int and float
what = 1.         # float
what = .5         # float
what = 1.0        # float
what = 1          # int

what = (1.)       # float
what = (.5)       # float
what = (1.0)      # float
what = (1)        # int
what = 10.5       # float
what = 10,5       # tuple with two ints
what = 10.        # float
what = 10,        # tuple with int
what = 10         # int

what = (10.5)     # float
what = (10,5)     # tuple with two ints
what = (10.)      # float
what = (10,)      # tuple with int
what = (10)       # int
what = 1.,1.      # tuple with two floats
what = .5,.5      # tuple with two floats
what = 1.,.5      # tuple with two floats

what = (1.,1.)    # tuple with two floats
what = (.5,.5)    # tuple with two floats
what = (1.,.5)    # tuple with two floats

7.3. Assignments

Todo

Create Assignments