Session 17 – Python: Data Types

Common data types in Python include: Booleans, Numbers, Strings, Bytes, Lists, Tuples, Sets and Dictionaries.

17.1 Booleans

Booleans have two possible values: True of False. You can think of booleans as simple lie detectors.

#boolean, test if true
bee_allergy = True
if bee_allergy == True:
 print('patient is allergic to bees')
else:
 print('patient is not allergic to bees')

#short-hand boolean condition
bee_allergy = True
if bee_allergy:
    print('patient is allergic to bees')
else:
    print('patient is not allergic to bees')

#one-liner!
print('patient is allergic to bees') if bee_allergy else print('patient is not allergic to bees')

17.2 Numbers: int, float and complex

It is important to note that floats will only be precise up to 15 decimal places. Any smaller should be abbreviated accordingly.

#using type and instance to decipher number type
type(2)

type(2.0)

type(2+3x)

type(2+3j)

isinstance(2+3j, complex)

#what does j mean??
#create a complex number
complex(2,3)

17.3 Strings

Strings are sequences of characters between double or single quotes.

#simple string
print("the store is out of apples")

#simple string 2
print('the store is out of apples')

#multiline string
print("""the store 
is out of 
apples
""")

#One-liner, multiline string?!
print("the store \n is out of \n apples")

#One-liner, multiline string?! fixed for spacing issues
print("the store \nis out of \napples")

17.4 Bytes

Byte can store bytes ranging from 0 - 255. We will not go over bytes as we will not use them directly in this setting. Called with ‘bytes()’

17.5 Lists

Lists are an array that stores typed objects in a specific sequence. Index in lists will start at 0 not at 1. Lists are contained in brackets.

#lets make a list containing different classes
my_list = [True, False, 1, 1.1, 1+2j, 'Learn', b'Python']

print(my_list)

print(my_list[0])

print(my_list[-1])

#lets print the class of each item in the list
for item in my_list:
    print(type(item))

#you can target and change items in the list
my_list[3] = 4
print(my_list)

#you can slice a list
print(my_list[0:3])

17.6 Tuples

Unlike lists, tuples are contained within parentheses. Tuples can contain objects of different types. Objects are still maintained in an order. Tuples cannot be modfied after creation. The objects themselves can be altered if they are alterable however, like identifiers.

#empty tuple

tuple_1 = ()
tuple_1

#tuple in tuple; nested tuple

tuple_1 = (1,2,3)
tuple_2 = ('apple', 'bannana')
tuple_3 = (tuple_1, tuple_2)
tuple_3

#modify tuples

tuple_na = ("na",)*3
tuple_na

tuple_bat = (tuple_na, 'batman') 
tuple_bat

#slicing tuples

tuple_num = (1,2,3,4)
tuple_num[1:]

17.7 Sets

Sets in python are used for mathematical operations and are incased in braces.

#Simple set
simple_set = set("strawberry mango")
type(simple_set)
simple_set

#Another Simple Set
simpler_set = {'strawberry', 'mango', 'grapefruit'}
type(simpler_set)
simpler_set

#Frozenset
food = {'fish', 'shrimp'}
food
frozen_food = frozenset(food)
type(frozen_food)
frozen_food

17.8 Dictionary

A dictionary is an unordered pair of key-value pairs. Highly optimized to store large datasets. Dictionaries use braces to define keys and their respective values.

#dictionary, below are random dates
worst_days_of_year = {'key':'value', 'feb':28, 'oct':2, 'dec':8}
type(worst_days_of_year)
worst_days_of_year

#drawing information from the dictionary
worst_days_of_year['feb']

#key filtering
worst_days_of_year.keys()

#value filtering
worst_days_of_year.values()

#item listing
worst_days_of_year.items()

#update a dictionary
worst_days_of_year.update({'mar':4})
worst_days_of_year

#reassign data
worst_days_of_year['mar'] = 5
worst_days_of_year

#delete data
del worst_days_of_year['mar']
worst_days_of_year